For this question I have created the following demo Next.js application using the App directory:
- It has a standard
RootLayout
inapp/layout.tsx
. - It has a nested
Layout
in a route groupapp/(app)/layout.tsx
. This layout contains<nav />
. - It has two server-side rendered pages:
app/(app)/a/page.tsx
andapp/(app)/b/page.tsx
. - Each page calls
getCurrentTime()
(defined onapp/functions.ts
) and then display the render time.
I want each navigation to a page to cause a re-render and run getCurrentTime()
again, but I couldn’t make the pages not cache. I’ve tried adding both export const revalidate = 0
and export const dynamic = "force-dynamic"
(docs), but couldn’t make it work.
I’m sure it’s pretty basic, but I still couldn’t get the hang of it.
2
Answers
According to the Next.js docs, seems like the mechanism responsible for caching pages on the client is the Router Cache and its purpose is to reduce server requests on navigation (source).
The documentation says: (source)
Since
revalidatePath
andrevalidateTag
can only be used in a server action, I ended with this creating a pseudo client component that will refresh the page on any navigation to it.It's not the prettiest solution but it works.
I would just move time to a client component
Check out related topic How can I disable cache on a specific page only in Next.JS