Loading...

Memory — Nothing Outlives a Removed Element

RULES.md

An element that is removed from the page must leave nothing reachable behind; long editing sessions otherwise grow without bound (haxtheweb/issues#3237). Four patterns caused every leak found so far, and each has a fix: (1) MobX `autorun`/`reaction` created in a `constructor` — create it in `connectedCallback`, keep the disposer, dispose it in `disconnectedCallback` (a constructor runs once, so it can neither be disposed when the element never connects nor recreated after a reconnect); (2) a class that fills `this.__disposer` without a `disconnectedCallback` that runs it — add one; (3) a `responsive-utility` registration (`"responsive-element"` event or `registerResponsiveElement()`) without `unregisterResponsiveElement()` on disconnect — its ResizeObserver keeps the element alive; (4) markup parsed in the live document only to be read back (`document.createElement("div").innerHTML = …`, `document.createRange().createContextualFragment()`) — custom elements in it upgrade and register globally; parse in `document.implementation.createHTMLDocument("")` or a `<template>`. The `haxtheweb-leaks/*` ESLint rules flag all four (existing hits are suppressed, so only new code is reported). `yarn test:leaks` (`scripts/leak-check.mjs`) proves it at runtime: it builds each HAX-capable element in the folders you changed from its first `demoSchema` entry, attaches and removes it repeatedly in Chromium, and fails on anything that grows with the cycles (global listeners, observers, timers, animation-frame loops, globalThis stores, uncollected instances) that `scripts/leak-check.baseline.json` does not already list for that tag; `--tags a-b,c-d` or `--all` widen it. When a fix removes an element's leak, refresh its baseline in the same PR (`yarn test:leaks:baseline --tags <tag>`) so the leak can't come back. When a fix removes a lint hit, prune its suppression (`npx eslint <files> --prune-suppressions`): ESLint fails on suppressions that no longer occur. In PRAW RULES.md (scope: webcomponents). Enforced by Memory leaks.

Connects to

Links