I have recently migrated to Nextjs 14 app router. My current folder structure looks like this:
When navigating between pages inside the dashboard layout it sometimes completely refreshes the page for no reason. This only happens in production. I could not reproduce this in development.
I discovered that stricter caching rules apply in production. It seems there is a default value, possibly around 15 seconds, which causes a page refresh when routing between pages if the cache has expired. Once refreshed, the page remains stable for the cache duration.
Increasing the stale time in the Next.js configuration resolved my issue. In fact, having the ability to revalidate the cache at specific intervals is quite beneficial.
// next.config.js
experimental: {
staleTimes: {
// Forces page refresh on route change every 24hours. Because cache gets invalidated
dynamic: 86400,
static: 86400,
},
},