Next.js Discord

Discord Forum

Weird Cache Behaviour

Answered
Arinji posted this in #help-forum
Open in Discord
I have 2 json files, /data/contentDownloads.json and /data/links.json

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 closure
View full answer

5 Replies