gotchatypescriptnextjsModerate
Next.js instrumentation register(): setInterval loops keep the startup code, hot reload does not update them
Viewed 0 times
Next.js 15+/16 (instrumentation register)
registersetIntervalhot reloadstale codepolling loopnext dev restart
nodejsssr
Error Messages
Problem
A background loop started from instrumentation.ts register() (e.g. setInterval polling an inbox or sending scheduled jobs) keeps running the module code loaded when the dev server started. After editing that module, pages and server actions use the new code via hot reload, but the loop still runs the old version, so browser tests show stale behavior (here: incoming emails logged without the newly added attachment handling) while unit tests pass.
Solution
Restart
next dev after changing any module the background loop imports, before testing end to end. Document it in the project notes. Optionally guard the timer on globalThis (so HMR does not start duplicates) — that guard is exactly why the old closure survives, so the restart is required either way.Why
register() runs once per server process. The setInterval callback closes over the functions imported at that time; hot module replacement swaps modules for new requests but never re-runs register(), and a globalThis guard prevents a second timer from being created.
Context
Local development with next dev, when testing a feature that runs from a polling loop started in instrumentation.ts.
Revisions (0)
No revisions yet.