Skip to content

Commit 66359d5

Browse files
authored
fix: clean up all event listeners in +layout.svelte onMount (open-webui#20913)
Extract anonymous touch event handlers into named functions and add proper cleanup for all event listeners (message, touchstart, touchmove, touchend, visibilitychange) in the onMount return function. Previously only the resize listener was being cleaned up, causing memory leaks during navigation and hot-reloads.
1 parent c111fa0 commit 66359d5

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/routes/+layout.svelte

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -629,28 +629,32 @@
629629
return nav && (el === nav || nav.contains(el));
630630
}
631631
632-
document.addEventListener('touchstart', (e) => {
632+
const touchstartHandler = (e) => {
633633
if (!isNavOrDescendant(e.target)) return;
634634
touchstartY = e.touches[0].clientY;
635-
});
635+
};
636636
637-
document.addEventListener('touchmove', (e) => {
637+
const touchmoveHandler = (e) => {
638638
if (!isNavOrDescendant(e.target)) return;
639639
const touchY = e.touches[0].clientY;
640640
const touchDiff = touchY - touchstartY;
641641
if (touchDiff > 50 && window.scrollY === 0) {
642642
showRefresh = true;
643643
e.preventDefault();
644644
}
645-
});
645+
};
646646
647-
document.addEventListener('touchend', (e) => {
647+
const touchendHandler = (e) => {
648648
if (!isNavOrDescendant(e.target)) return;
649649
if (showRefresh) {
650650
showRefresh = false;
651651
location.reload();
652652
}
653-
});
653+
};
654+
655+
document.addEventListener('touchstart', touchstartHandler);
656+
document.addEventListener('touchmove', touchmoveHandler, { passive: false });
657+
document.addEventListener('touchend', touchendHandler);
654658
655659
if (typeof window !== 'undefined') {
656660
if (window.applyTheme) {
@@ -842,11 +846,15 @@
842846
843847
return () => {
844848
window.removeEventListener('resize', onResize);
849+
window.removeEventListener('message', windowMessageEventHandler);
850+
document.removeEventListener('touchstart', touchstartHandler);
851+
document.removeEventListener('touchmove', touchmoveHandler);
852+
document.removeEventListener('touchend', touchendHandler);
853+
document.removeEventListener('visibilitychange', handleVisibilityChange);
845854
};
846855
});
847856
848857
onDestroy(() => {
849-
window.removeEventListener('message', windowMessageEventHandler);
850858
bc.close();
851859
});
852860
</script>

0 commit comments

Comments
 (0)