mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-08 20:42:43 +00:00
A production build instantiates a module imported from both a Server Component and a route handler once per bundler layer. The cache registry sat in plain module state, so the home page's server render held its own set of Maps and every API route held another. Recording a dose evicted the route handlers' copy; the home page then rendered from a copy nothing had touched and painted the dose as still due until the entry aged out on its own, two or three minutes later. Because that render is dehydrated into the client query cache, it also overwrote the fresh value the browser had already fetched. Measured on `next build && next start`, single process: the page render and the snapshot route reported different cache instances, and after an interactive write the route handlers' copy dropped to zero entries while the page's copy stayed full. The registry now lives on globalThis under a Symbol.for slot, which is the only scope both layers share. db.ts guards its own global assignment with a non-production check because it is only defending against dev reloads; this one has to hold in production, so it is unconditional.