Weird Cache Behaviour
Answered
Arinji posted this in #help-forum
ArinjiOP
I have 2 json files, /data/contentDownloads.json and /data/links.json
This is my helper to get the json files
I need the tags so I can revalidate them. Now if I call the function with /contentDownloads, "content" I get the correct data and can also revalidate correctly, but doing "links", "link" provides the same data as the content one, any idea why this seems to be happening?
This is my helper to get the json files
import { unstable_cache } from "next/cache";
export async function getJson(
fileName: string,
tag?: string,
revalidate?: number
) {
const content = unstable_cache(
async () => {
const file = await fs.readFile(
process.cwd() + `/data${fileName}.json`,
"utf8"
);
const locContent = JSON.parse(file);
return locContent;
},
["json"],
{
tags: tag ? [tag] : ["data"],
revalidate: revalidate ? revalidate : false,
}
)();
return content;
}I need the tags so I can revalidate them. Now if I call the function with /contentDownloads, "content" I get the correct data and can also revalidate correctly, but doing "links", "link" provides the same data as the content one, any idea why this seems to be happening?
Answered by aardani
you need to put
fileName in the second parameter of unstablecache since its a closure5 Replies
Answer
so "content" and "link" uses the same cache since the stringified version of the async function is the same
either put
fileName in the second parameter, or pass it as an argument again into the inner function@aardani either put `fileName` in the second parameter, or pass it as an argument again into the inner function
ArinjiOP
So the cache key has to have the filename?
I thought that param didn't matter lol