I’m trying to use Map objects in an NextJS app as a kind of caching layer. The problem is that when running the app in dev mode (npm run dev), every incoming HTTP request causes the modules to load again, which resets the cache.
Example:
-
Middleware file is setup to invoke the “redirectToCognito” function, which is defined in the “redirectToCognito.ts” module/file.
-
This module has the following code in it:
import ExpiryMap from 'expiry-map';
console.log('***Stupid module is loading again!****');
const stateMap = new ExpiryMap(180000, []);
export default async function RedirectToCognitoAuthorization(subdomain: string) {
const state = '12345';
stateMap.set(state, 'abcde');
console.log(stateMap.size);
}
return;
- Every time an HTTP request comes in, I see the console.log(‘Stupid module is loading again!*’) message being printed.
This problem doesn’t happen if I deploy to prod, so my first thought was that it could be related to Strict Mode. However, it seems to be disabled by default in the Mantine template that I’m using… From my “next.config.mjs” file:
export default withBundleAnalyzer({
**reactStrictMode: false,**
eslint: {
ignoreDuringBuilds: true,
},
experimental: {
optimizePackageImports: ['@mantine/core', '@mantine/hooks'],
},
});
This is very annoying… I have another module that loads records from a DB when the app starts. Every time a new request comes in, it loads all records again!
Any ideas on why this is happening?