Unverified Commit 1794f59a authored by Sebastian Markbåge's avatar Sebastian Markbåge Committed by GitHub

Set Next-Router-Stale-Time header from collected stale times (#71279)

That way the client router can use it to inform its cache.

This is only done during prerender which is typically the only things we
prefetch (other than loading.tsx).

I add it to `.meta` but this applies to both the RSC and HTML payloads I
believe. I'm not sure if there's away to only add it to the RSC payload.

For static parts of the HTML document we need a different strategy than
the head so this doesn't work for initial load.
parent 481dee6e
......@@ -25,5 +25,6 @@ export const FLIGHT_HEADERS = [
export const NEXT_RSC_UNION_QUERY = '_rsc' as const
export const NEXT_ROUTER_STALE_TIME_HEADER = 'x-nextjs-stale-time' as const
export const NEXT_DID_POSTPONE_HEADER = 'x-nextjs-postponed' as const
export const NEXT_IS_PRERENDER_HEADER = 'x-nextjs-prerender' as const
......@@ -43,6 +43,7 @@ import {
NEXT_HMR_REFRESH_HEADER,
NEXT_ROUTER_PREFETCH_HEADER,
NEXT_ROUTER_STATE_TREE_HEADER,
NEXT_ROUTER_STALE_TIME_HEADER,
NEXT_URL,
RSC_HEADER,
} from '../../client/components/app-router-headers'
......@@ -1114,6 +1115,12 @@ async function renderToHTMLOrFlightImpl(
metadata.fetchTags = response.collectedTags.join(',')
}
// Let the client router know how long to keep the cached entry around.
const staleHeader = String(response.collectedStale)
res.setHeader(NEXT_ROUTER_STALE_TIME_HEADER, staleHeader)
metadata.headers ??= {}
metadata.headers[NEXT_ROUTER_STALE_TIME_HEADER] = staleHeader
// If force static is specifically set to false, we should not revalidate
// the page.
if (workStore.forceStatic === false || response.collectedRevalidate === 0) {
......
......@@ -188,6 +188,7 @@ function generateCacheEntryWithCacheContext(
'A default cacheLife profile must always be provided. This is a bug in Next.js.'
)
}
// Initialize the Store for this Cache entry.
const cacheStore: UseCacheStore = {
type: 'cache',
......@@ -239,9 +240,15 @@ function propagateCacheLifeAndTags(
outerTags.push(tag)
}
}
if (workUnitStore.stale > entry.stale) {
workUnitStore.stale = entry.stale
}
if (workUnitStore.revalidate > entry.revalidate) {
workUnitStore.revalidate = entry.revalidate
}
if (workUnitStore.expire > entry.expire) {
workUnitStore.expire = entry.expire
}
}
}
......
......@@ -6,6 +6,7 @@ const nextConfig = {
dynamicIO: true,
cacheLife: {
frequent: {
stale: 19,
revalidate: 100,
},
},
......
......@@ -98,6 +98,16 @@ describe('use-cache', () => {
}
)
itSkipTurbopack(
'should match the expected stale config in the page header',
async () => {
const meta = JSON.parse(
await next.readFile('.next/server/app/cache-life.meta')
)
expect(meta.headers['x-nextjs-stale-time']).toBe('19')
}
)
itSkipTurbopack(
'should propagate unstable_cache tags correctly',
async () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment