Next.js Discord

Discord Forum

revalidateTag / revalidatePath only working for second fetch/page load.

Answered
Arboreal ant posted this in #help-forum
Open in Discord
Arboreal antOP
Hey all,

I'm probably still not understanding something correctly, but I can't seem to get revalidation working how I expect it to.

I have a fetch request with a tag:
return fetch(`${baseUrl}/organisations/${encodeURIComponent(pathParams.organisationId)}/profile`, {
    method: 'GET',
    cache: 'no-cache',
    next: {
      tags: ['organisation']
    },
  });


and a server action which makes a patch request and revalidates the tag

await <do patch stuff>
revalidateTag(`organisation`);


But when the server component which fetches the organisation profile runs it has the old data

  const organisationProfile = await organisationsOrganisationIdProfileGet({
    pathParams: {
      organisationId: params.organisationId,
    },
  });
  
  console.log(organisationProfile.bio);


Looking at the console logs. The page does reload after a successful patch, but the console.log shows the old stale data. Navigating away from the page and back to it doesn't trigger a console.log (it must be cached on the client side still), but refreshing the page causes it to log the correct value.

I've also tried revalidatePath with the server component page but it's the same result.

Other pages / components which read the organisationProfile are correct when you navigate to them after the patch, but the page with the form on still has stale data. This means that if you navigate back to the component with the form it still has the stale old data in the fields.

How can I fix this?
Answered by Arboreal ant
After digging through the nextjs issues related to invalidation it seems the recommended solution is to call router.refresh() in the affected client components after the server actions have run and called revalidate<Tag/Path>. This worked for me at least.
View full answer

1 Reply

Arboreal antOP
After digging through the nextjs issues related to invalidation it seems the recommended solution is to call router.refresh() in the affected client components after the server actions have run and called revalidate<Tag/Path>. This worked for me at least.
Answer