All TIL notes

Friday, 14 August 2026 · 1 min read

Svelte actions should return a destroy function

An IntersectionObserver created in an action outlives the element unless the action returns `{ destroy }`.

svelte

export const reveal: Action<HTMLElement> = (node) => {
  const observer = new IntersectionObserver(/* … */);
  observer.observe(node);
  return { destroy: () => observer.disconnect() };
};

Without the destroy, client-side navigation leaves observers attached to detached nodes. Harmless at first, a leak after fifty page views.