Skip to content

Commit e0f272a

Browse files
mydeabitsandfoxes
authored andcommitted
fix(platform): Avoid IntersectionObserver issues (#13420)
We have a lot of issues like this: https://sentry.sentry.io/issues/5543047544/?project=1267915 TBH not sure how that happens, but I guess we can try to be defensive here. Maybe inner height is missing somehow?
1 parent 129f0c0 commit e0f272a

File tree

1 file changed

+6
-5
lines changed
  • src/components/sidebarTableOfContents

1 file changed

+6
-5
lines changed

src/components/sidebarTableOfContents/index.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,16 +157,17 @@ export function SidebarTableOfContents() {
157157

158158
// Mark the active item based on the scroll position
159159
useEffect(() => {
160-
if (!tocItems.length) {
160+
const innerHeight = window.innerHeight;
161+
if (!tocItems.length || !innerHeight) {
161162
return () => {};
162163
}
163164
// account for the header height
164165
const rootMarginTop = 100;
165166
// element is consiered in view if it is in the top 1/3 of the screen
166-
const rootMarginBottomRaw = (2 / 3) * window.innerHeight - rootMarginTop;
167-
const rootMarginBottom = Math.floor(rootMarginBottomRaw);
167+
const rootMarginBottomRaw = (2 / 3) * innerHeight - rootMarginTop;
168+
const rootMarginBottom = Math.floor(rootMarginBottomRaw) * -1;
168169
const observerOptions = {
169-
rootMargin: `${rootMarginTop}px 0px -${rootMarginBottom}px 0px`,
170+
rootMargin: `${rootMarginTop}px 0px ${rootMarginBottom}px 0px`,
170171
threshold: 1,
171172
};
172173
const observer = new IntersectionObserver(entries => {
@@ -183,7 +184,7 @@ export function SidebarTableOfContents() {
183184
}, observerOptions);
184185
const headings = tocItems.map(item => item.element);
185186
headings.forEach(heading => observer.observe(heading));
186-
return () => headings.forEach(heading => observer.unobserve(heading));
187+
return () => observer.disconnect();
187188
}, [tocItems]);
188189

189190
return (

0 commit comments

Comments
 (0)