Files
Hendrik LiebauandGitHub 3ae9d38247 Propagate expire time to cache-control header and prerender manifest (#76207)
For the `stale-while-revalidate` value in the `cache-control` response header, Next.js currently uses the configured
[`expireTime`](https://nextjs.org/docs/app/api-reference/config/next-config-js/expireTime), which applies to all routes, and defaults to one year.

With the introduction of `"use cache"` and granular [cache lifetimes](https://nextjs.org/docs/app/api-reference/functions/cacheLife), users can now set expire times per route — based on the minimum expire time of all cached functions used by that route.

This PR updates the `stale-while-revalidate` value to reflect the route's cache lifetime, using the difference between the minimum expire time and the minimum revalidate time. If no explicit expire time is defined in cache profiles, the globally configured `expireTime` is used.

Additionally, the collected expire time is added to the prerender manifest as `initialExpireSeconds` (and `fallbackExpire`), analogous to `initialRevalidateSeconds` (or `fallbackRevalidate`).

closes NAR-100
2025-02-28 21:10:26 +01:00

45 lines
971 B
JavaScript

import fs from 'fs'
import path from 'path'
// eslint-disable-next-line
import next from 'next' // force a warning during `next build`
import { useRouter } from 'next/router'
export async function getServerSideProps({ res }) {
res.setHeader('cache-control', 's-maxage=1, stale-while-revalidate=31535999')
const data = await fs.promises.readFile(
path.join(process.cwd(), 'data.txt'),
'utf8'
)
if (data.trim() === 'hide') {
return {
notFound: true,
}
}
return {
props: {
hello: 'world',
data,
random: Math.random(),
// make sure fetch if polyfilled
example: await fetch('https://example.vercel.sh').then((res) =>
res.text()
),
},
}
}
export default function Page(props) {
const router = useRouter()
return (
<>
<p id="gssp">getServerSideProps page</p>
<p id="router">{JSON.stringify(router)}</p>
<p id="props">{JSON.stringify(props)}</p>
</>
)
}