Unverified Commit 41e3f93e authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Add experimental option to use no-cache instead of no-store in dev (#88182)

## What?

Adds `experimental.devCacheControlNoCache` to test `no-cache` vs
`no-store` for development.

Note: This is for testing only right now. If it's an improvement we'll
change the default.
parent fd5817d0
...@@ -999,7 +999,12 @@ export async function handler( ...@@ -999,7 +999,12 @@ export async function handler(
// In dev, we should not cache pages for any reason. // In dev, we should not cache pages for any reason.
if (routeModule.isDev) { if (routeModule.isDev) {
res.setHeader('Cache-Control', 'no-store, must-revalidate') res.setHeader(
'Cache-Control',
nextConfig.experimental.devCacheControlNoCache
? 'no-cache, must-revalidate'
: 'no-store, must-revalidate'
)
} }
if (!cacheEntry) { if (!cacheEntry) {
......
...@@ -1782,7 +1782,12 @@ export default abstract class Server< ...@@ -1782,7 +1782,12 @@ export default abstract class Server<
// In dev, we should not cache pages for any reason. // In dev, we should not cache pages for any reason.
if (dev) { if (dev) {
res.setHeader('Cache-Control', 'no-store, must-revalidate') res.setHeader(
'Cache-Control',
this.nextConfig.experimental.devCacheControlNoCache
? 'no-cache, must-revalidate'
: 'no-store, must-revalidate'
)
cacheControl = undefined cacheControl = undefined
} }
......
...@@ -352,6 +352,7 @@ export const experimentalSchema = { ...@@ -352,6 +352,7 @@ export const experimentalSchema = {
lockDistDir: z.boolean().optional(), lockDistDir: z.boolean().optional(),
hideLogsAfterAbort: z.boolean().optional(), hideLogsAfterAbort: z.boolean().optional(),
runtimeServerDeploymentId: z.boolean().optional(), runtimeServerDeploymentId: z.boolean().optional(),
devCacheControlNoCache: z.boolean().optional(),
} }
export const configSchema: zod.ZodType<NextConfig> = z.lazy(() => export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
......
...@@ -873,6 +873,18 @@ export interface ExperimentalConfig { ...@@ -873,6 +873,18 @@ export interface ExperimentalConfig {
* @default false * @default false
*/ */
runtimeServerDeploymentId?: boolean runtimeServerDeploymentId?: boolean
/**
* Use 'no-cache' instead of 'no-store' in the Cache-Control header for development.
* This allows conditional requests to the server, which can help with development
* workflows that benefit from caching validation.
*
* When enabled, the Cache-Control header changes from 'no-store, must-revalidate'
* to 'no-cache, must-revalidate'.
*
* @default false
*/
devCacheControlNoCache?: boolean
} }
export type ExportPathMap = { export type ExportPathMap = {
...@@ -1599,6 +1611,7 @@ export const defaultConfig = Object.freeze({ ...@@ -1599,6 +1611,7 @@ export const defaultConfig = Object.freeze({
turbopackFileSystemCacheForDev: true, turbopackFileSystemCacheForDev: true,
turbopackFileSystemCacheForBuild: false, turbopackFileSystemCacheForBuild: false,
turbopackInferModuleSideEffects: !isStableBuild(), turbopackInferModuleSideEffects: !isStableBuild(),
devCacheControlNoCache: false,
}, },
htmlLimitedBots: undefined, htmlLimitedBots: undefined,
bundlePagesRouterDependencies: false, bundlePagesRouterDependencies: false,
...@@ -1695,6 +1708,7 @@ export interface NextConfigRuntime { ...@@ -1695,6 +1708,7 @@ export interface NextConfigRuntime {
| 'testProxy' | 'testProxy'
| 'runtimeServerDeploymentId' | 'runtimeServerDeploymentId'
| 'maxPostponedStateSize' | 'maxPostponedStateSize'
| 'devCacheControlNoCache'
> & { > & {
// Pick on @internal fields generates invalid .d.ts files // Pick on @internal fields generates invalid .d.ts files
/** @internal */ /** @internal */
...@@ -1752,6 +1766,7 @@ export function getNextConfigRuntime( ...@@ -1752,6 +1766,7 @@ export function getNextConfigRuntime(
testProxy: ex.testProxy, testProxy: ex.testProxy,
runtimeServerDeploymentId: ex.runtimeServerDeploymentId, runtimeServerDeploymentId: ex.runtimeServerDeploymentId,
maxPostponedStateSize: ex.maxPostponedStateSize, maxPostponedStateSize: ex.maxPostponedStateSize,
devCacheControlNoCache: ex.devCacheControlNoCache,
trustHostHeader: ex.trustHostHeader, trustHostHeader: ex.trustHostHeader,
isExperimentalCompile: ex.isExperimentalCompile, isExperimentalCompile: ex.isExperimentalCompile,
......
...@@ -497,7 +497,12 @@ export async function initialize(opts: { ...@@ -497,7 +497,12 @@ export async function initialize(opts: {
matchedOutput.type === 'nextStaticFolder' matchedOutput.type === 'nextStaticFolder'
) { ) {
if (opts.dev && !isNextFont(parsedUrl.pathname)) { if (opts.dev && !isNextFont(parsedUrl.pathname)) {
res.setHeader('Cache-Control', 'no-store, must-revalidate') res.setHeader(
'Cache-Control',
config.experimental.devCacheControlNoCache
? 'no-cache, must-revalidate'
: 'no-store, must-revalidate'
)
} else { } else {
res.setHeader( res.setHeader(
'Cache-Control', 'Cache-Control',
......
...@@ -684,7 +684,12 @@ export const getHandler = ({ ...@@ -684,7 +684,12 @@ export const getHandler = ({
// In dev, we should not cache pages for any reason. // In dev, we should not cache pages for any reason.
if (routeModule.isDev) { if (routeModule.isDev) {
res.setHeader('Cache-Control', 'no-store, must-revalidate') res.setHeader(
'Cache-Control',
nextConfig.experimental.devCacheControlNoCache
? 'no-cache, must-revalidate'
: 'no-store, must-revalidate'
)
} }
// Draft mode should never be cached // Draft mode should never be cached
......
export default function AppRoute() {
return <div>App Route</div>
}
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
import { nextTestSetup } from 'e2e-utils'
describe('experimental.devCacheControlNoCache disabled (default)', () => {
const { next } = nextTestSetup({
files: __dirname,
})
it('should use no-store for pages router by default', async () => {
const res = await next.fetch('/pages-route')
expect(res.headers.get('Cache-Control')).toBe('no-store, must-revalidate')
})
it('should use no-store for app router by default', async () => {
const res = await next.fetch('/app-route')
expect(res.headers.get('Cache-Control')).toBe('no-store, must-revalidate')
})
})
/** @type {import('next').NextConfig} */
module.exports = {}
export default function PagesRoute() {
return <div>Pages Route</div>
}
export default function AppRoute() {
return <div>App Route</div>
}
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
import { nextTestSetup } from 'e2e-utils'
describe('experimental.devCacheControlNoCache', () => {
describe('when enabled', () => {
const { next } = nextTestSetup({
files: __dirname,
})
it('should use no-cache instead of no-store for pages router', async () => {
const res = await next.fetch('/pages-route')
expect(res.headers.get('Cache-Control')).toBe('no-cache, must-revalidate')
})
it('should use no-cache instead of no-store for app router', async () => {
const res = await next.fetch('/app-route')
expect(res.headers.get('Cache-Control')).toBe('no-cache, must-revalidate')
})
})
})
/** @type {import('next').NextConfig} */
module.exports = {
experimental: {
devCacheControlNoCache: true,
},
}
export default function PagesRoute() {
return <div>Pages Route</div>
}
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