Skip to content

Commit 021afe0

Browse files
committed
fix: Fix throwing intersectionObserver is not defined error in SSR mode
1 parent 89aee9b commit 021afe0

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-notion-x-code-block",
3-
"version": "0.3.0",
3+
"version": "0.3.1",
44
"description": "",
55
"type": "module",
66
"module": "dist/index.js",

src/manager.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
type HandlerFunction = (element: Element) => void;
22

33
class ObserverManager {
4-
private observer: IntersectionObserver;
4+
private observer?: IntersectionObserver;
55
private handlers: Map<Element, HandlerFunction>;
66

77
constructor() {
88
this.handlers = new Map();
99

10+
// Skip when SSR
11+
if (typeof window === "undefined") {
12+
return;
13+
}
14+
1015
this.observer = new IntersectionObserver(
1116
(entries) => {
1217
entries.forEach((entry) => {
@@ -26,14 +31,14 @@ class ObserverManager {
2631
// Return unobserve callback directly
2732
observe(element: Element, handler: HandlerFunction): () => void {
2833
this.handlers.set(element, handler);
29-
this.observer.observe(element);
34+
this.observer?.observe(element);
3035

3136
return () => this.unobserve(element);
3237
}
3338

3439
unobserve(element: Element): void {
3540
this.handlers.delete(element);
36-
this.observer.unobserve(element);
41+
this.observer?.unobserve(element);
3742
}
3843
}
3944

0 commit comments

Comments
 (0)