node-cache icon indicating copy to clipboard operation
node-cache copied to clipboard

Item written to cache by Nextjs middleware doesn't exist when reading from the cache

Open dconlisk opened this issue 2 years ago • 2 comments

In a nextjs app (classic /pages setup, I'm not using the new /app directory), I want to set an item in the cache in middleware, which can then be read in my pages, components etc in the app. Unfortunately, when I try to read the value back from the cache, it is null.

i.e.:

// middleware.ts
console.log("Setting token cache entry for", guid, JSON.stringify(entry));
cache.put(guid, entry);
// index.tsx getServerSideProps method
console.log("Getting token cache entry for", guid, cache.get(guid));
const entry = cache.get(guid); // ---> entry is null

Console output:

Setting token cache entry for deab99d1-f688-4e9b-a9e49d63b049895a { ... entry object ... } Getting token cache entry for deab99d1-f688-4e9b-a9e49d63b049895a null

dconlisk avatar Apr 14 '23 12:04 dconlisk

Software versions used (latest of everything AFAIK):

"memory-cache": "^0.2.0", "next": "13.2.4", "react": "18.2.0", "react-dom": "18.2.0",

dconlisk avatar Apr 14 '23 12:04 dconlisk

For what it's worth, I was having a similar issue, and found that I was setting the timeout to 1 thinking it meant "one hour" 🤦‍♂️

It's running great in NextJS production builds, and yes I'm also in the old Pages Router.

EricWVGG avatar Nov 27 '23 17:11 EricWVGG