I'm using the data cache and passing tags to the fetch call. In an API route handler I call revalidateTag to clear tags associated with those fetch requests. However, locally, I'm not seeing new data come through. Where should I start with checking what I'm doing wrong? I don't know if the same behaviour happens in Vercel as I haven't tested that yet.
#Fetch data cache not clearing when using revalidateTag
1 messages · Page 1 of 1 (latest)
Hi @barren inlet. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
There could be several reasons why you're not seeing new data come through after calling revalidateTag. Here are some things you can check:
- Check your
fetchrequest: Make sure you're correctly tagging yourfetchrequests. The tags should be passed in thenextoption of thefetchrequest like this:
fetch('<https://...',> { next: { tags: ['your-tag'] } })
(Source: Next.js Docs (https://nextjs.org/docs/app/building-your-application/caching))
- Check your
revalidateTagcall: Ensure that you're correctly callingrevalidateTagwith the correct tag. The tag passed torevalidateTagshould match the one used in yourfetchrequest.
revalidateTag('your-tag')
(Source: Next.js Docs (https://nextjs.org/docs/app/api-reference/functions/revalidateTag))
- Check your Route Handler: If you're using a Route Handler, make sure you're correctly handling the request and response. The
revalidateTagfunction should be called with the correct tag, and the response should be handled correctly. Here's an example:
import { NextRequest, NextResponse } from 'next/server'
import { revalidateTag } from 'next/cache'
export async function GET(request: NextRequest) {
const tag = request.nextUrl.searchParams.get('tag')
revalidateTag(tag)
return NextResponse.json({ revalidated: true, now: Date.now() })
}
(Source: Next.js Docs (https://nextjs.org/docs/app/api-reference/functions/revalidateTag))
-
Understand Cache Interactions: It's important to understand that revalidating the Data Cache in a Route Handler will not immediately invalidate the Router Cache as the Router Handler isn't tied to a specific route. This means Router Cache will continue to serve the previous payload until a hard refresh, or the automatic invalidation period has elapsed. To immediately invalidate the Data Cache and Router cache, you can use
revalidatePathorrevalidateTagin a Server Action.
(Source: Next.js Docs (https://nextjs.org/docs/app/building-your-application/caching)) -
Check your local environment: If you're testing this locally, make sure your local environment is correctly set up to handle caching and revalidation. The default cache handler in Next.js uses the filesystem cache, which requires no configuration. However, if you've customized the cache handler using the
incrementalCacheHandlerPathfield innext.config.js, make sure it's correctly implemented.
(Source: Next.js Docs (https://nextjs.org/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath))