fix: clean up all event listeners in +layout.svelte onMount (#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.
This commit is contained in:
7. Sun
2026-01-29 16:50:25 +00:00
committed by GitHub
parent c111fa0837
commit 66359d5815

View File

@@ -629,12 +629,12 @@
return nav && (el === nav || nav.contains(el));
}
document.addEventListener('touchstart', (e) => {
const touchstartHandler = (e) => {
if (!isNavOrDescendant(e.target)) return;
touchstartY = e.touches[0].clientY;
});
};
document.addEventListener('touchmove', (e) => {
const touchmoveHandler = (e) => {
if (!isNavOrDescendant(e.target)) return;
const touchY = e.touches[0].clientY;
const touchDiff = touchY - touchstartY;
@@ -642,15 +642,19 @@
showRefresh = true;
e.preventDefault();
}
});
};
document.addEventListener('touchend', (e) => {
const touchendHandler = (e) => {
if (!isNavOrDescendant(e.target)) return;
if (showRefresh) {
showRefresh = false;
location.reload();
}
});
};
document.addEventListener('touchstart', touchstartHandler);
document.addEventListener('touchmove', touchmoveHandler, { passive: false });
document.addEventListener('touchend', touchendHandler);
if (typeof window !== 'undefined') {
if (window.applyTheme) {
@@ -842,11 +846,15 @@
return () => {
window.removeEventListener('resize', onResize);
window.removeEventListener('message', windowMessageEventHandler);
document.removeEventListener('touchstart', touchstartHandler);
document.removeEventListener('touchmove', touchmoveHandler);
document.removeEventListener('touchend', touchendHandler);
document.removeEventListener('visibilitychange', handleVisibilityChange);
};
});
onDestroy(() => {
window.removeEventListener('message', windowMessageEventHandler);
bc.close();
});
</script>