TL;DR
Three independent sites in the runtime attach listeners (DOM event, store subscription, monkey-patched method) without a corresponding cleanup path. Each is fine in single-page-load scenarios but compounds in SPA navigation, HMR cycles, and component re-mounts — exactly the lifecycle stx is built for.
Three findings, same anti-pattern:
1. loading-indicator.ts re-init duplicates DOM listeners
File: packages/stx/src/loading-indicator.ts:292-332
document.addEventListener('click', (e) => {
const target = e.target as HTMLElement
const link = target.closest('a')
// ...
})
// Also attaches popstate, load — no removeEventListener pathinitLoadingIndicator() is callable multiple times (SPA navigation, HMR reinjection, the dev server's hot-reload). Each call attaches a fresh set of click / popstate / load listeners. The previous set never gets removed. After 10 navigations, every link click runs 10 handlers; after a few dozen HMR cycles, the page is paying real CPU on every click.
Fix shape: capture handlers into module-level variables on first init; on re-init, removeEventListener the previous ones before adding new ones. Or expose a cleanupLoadingIndicator() and call it from the router's pre-nav hook.
2. reactive-bindings.ts store subscriptions never unsubscribe
File: packages/stx/src/reactive-bindings.ts:340-343
code += ` if (stores.${storeName}) {
stores.${storeName}.subscribe(function(state) {
$state.${storeName} = state;
updateBindings();
});
}
`This is generated code that runs on element mount. There's no paired .unsubscribe() in any teardown path. When a data-stx-scope element is removed (typical SPA fragment-swap), its bindings still hold a subscription that walks the (now-detached) DOM on every store update. Memory bloat plus wasted updateBindings() work.
Fix shape: capture the disposer from subscribe(), push into a cleanup array on the scope element, run on scope teardown (the runtime already has _cleanupContainer).
3. hot-reload.ts console monkey-patch never restored
File: packages/stx/src/hot-reload.ts:444-469
Hot-reload replaces console.log / console.warn / console.error with wrappers that forward to the dev server. The originals aren't saved, and the patch is reapplied on each HMR cycle. After N reloads, you have N layers of wrapping — each method call walks N functions deep.
Fix shape: save the originals on first patch (const origLog = console.log). On re-patch, restore before re-wrapping. Or guard if (console.log.__stxPatched) return.
Why this matters
These are textbook "the bug compounds with session length" issues. In dev they look fine — fresh refresh fixes everything. In production SPAs that stay open for hours, listener counts grow until tabs feel sluggish, and instrumentation overhead shows up in CPU profiles.
Found during a deep-scan audit. Same root pattern (attach without cleanup) in three different places.
🤖 Filed via Claude Code