Design Patterns · Middle · Question 6 of 75
What's dangerous about a module-level singleton (export const cache = new Map()) in an SSR app?
- ✓Correct answer: On the server the module is shared by all requests, so users' data gets mixed
- 2During SSR modules aren't cached, so the singleton is recreated on every import
- 3SSR doesn't support ES modules, so such code won't build
Explanation
In the browser, a module variable lives within one tab. A server process serves thousands of users, and a shared module-level cache or store can serve one user's data to another. Request state is created per request (e.g. React's cache(), a QueryClient per request).