mirror of
https://github.com/hanzoai/next.js.git
synced 2026-10-30 14:46:38 +00:00
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
45 lines
971 B
JavaScript
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>
|
|
</>
|
|
)
|
|
}
|