Unverified Commit 2839982a authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Stop the browser from restoring stale pages in development (#97505)

Development responses used `no-store, must-revalidate` until #88182
tried `no-cache, must-revalidate` behind
`experimental.devCacheControlNoCache`, and #91503 removed that option
and hard-coded the `no-cache` value everywhere. That was right for
static assets and wrong for documents. A browser may reuse a stored
response for a history navigation without revalidating it, and
development documents are streamed without an `ETag`, so there is
nothing to revalidate against. Going back therefore restored the
document the browser had stored earlier and showed output from before
the latest edit, and it is also what forced the debug channel
persistence workarounds in #92892, #93486 and #94243.

Documents and RSC or data responses now use `no-store` again, set in
`app-page-runtime.ts` for app pages, in `pages-handler.ts` for pages,
and in the legacy render pipe in `base-server.ts` so that the three do
not drift apart. None of them ever serves a static asset, so assets keep
`no-cache, must-revalidate` from the `nextStaticFolder` branch in
`router-server.ts` and stay cacheable: they are revalidated against the
`ETag` that `serveStatic` adds and reused from a `304` instead of being
downloaded again on every page load. `must-revalidate` is left off the
document value, because it only governs reuse of an already stale stored
response and nothing is stored any more.

A back navigation is no longer instant, since the document is fetched
again instead of being restored locally.
`test/development/dev-cache-control` covers both sides of that
trade-off: an edit that is visible after a back navigation, and
unchanged assets that still come back as `304`. It replaces
`dev-cache-control-no-cache` and asserts the header for both routers as
well, so there is one suite instead of two with nearly the same name.

A development document is now never restored from the HTTP cache, so the
debug channel persistence has no remaining trigger and its `IndexedDB`
write on every page load is no longer needed. Removing it is a follow-up
on top of this change. The pruning and recovery test in
`bfcache-regression` is the one case whose premise disappears entirely,
and it is skipped here with a note to delete it along with the
persistence.

closes #96503
parent da50acde
...@@ -50,7 +50,6 @@ import { ...@@ -50,7 +50,6 @@ import {
NEXT_IS_PRERENDER_HEADER, NEXT_IS_PRERENDER_HEADER,
NEXT_DID_POSTPONE_HEADER, NEXT_DID_POSTPONE_HEADER,
RSC_CONTENT_TYPE_HEADER, RSC_CONTENT_TYPE_HEADER,
NEXT_HMR_REFRESH_HEADER,
} from '../../client/components/app-router-headers' with { 'turbopack-transition': 'next-server-utility' } } from '../../client/components/app-router-headers' with { 'turbopack-transition': 'next-server-utility' }
import { getBotType } from '../../shared/lib/router/utils/is-bot' with { 'turbopack-transition': 'next-server-utility' } import { getBotType } from '../../shared/lib/router/utils/is-bot' with { 'turbopack-transition': 'next-server-utility' }
import { import {
...@@ -1623,21 +1622,14 @@ export function createAppPageEntrypoint({ ...@@ -1623,21 +1622,14 @@ export function createAppPageEntrypoint({
) )
} }
// Dev responses use `no-cache` so the browser can restore them from the // Documents and RSC payloads must not be stored in development.
// HTTP cache on back/forward instead of reloading. HMR refresh responses // Browsers reuse a stored response for a history navigation without
// opt out into `no-store` because a superseded refresh's fetch is aborted // revalidating it, so a back navigation would restore a page from
// mid-write: under `no-cache` the response is stored, so the abort leaves // before the latest edit. Static assets never reach this code. They
// the cache entry shared with the superseding refresh (same URL) // keep a revalidatable `Cache-Control`, so the browser caches them
// half-written; Chromium then discards it and reissues the superseding // between page loads.
// refresh on a second connection as a duplicate request. `no-store` keeps
// that entry from being created.
if (routeModule.isDev) { if (routeModule.isDev) {
res.setHeader( res.setHeader('Cache-Control', 'no-store')
'Cache-Control',
req.headers[NEXT_HMR_REFRESH_HEADER] === '1'
? 'no-store'
: 'no-cache, must-revalidate'
)
} }
if (!cacheEntry) { if (!cacheEntry) {
......
...@@ -102,7 +102,6 @@ import { ...@@ -102,7 +102,6 @@ import {
NEXT_URL, NEXT_URL,
NEXT_ROUTER_STATE_TREE_HEADER, NEXT_ROUTER_STATE_TREE_HEADER,
NEXT_INSTANT_TEST_COOKIE, NEXT_INSTANT_TEST_COOKIE,
NEXT_HMR_REFRESH_HEADER,
} from '../client/components/app-router-headers' } from '../client/components/app-router-headers'
import { nanoid } from 'next/dist/compiled/nanoid' import { nanoid } from 'next/dist/compiled/nanoid'
import { LocaleRouteNormalizer } from './normalizers/locale-route-normalizer' import { LocaleRouteNormalizer } from './normalizers/locale-route-normalizer'
...@@ -2140,21 +2139,14 @@ export default abstract class Server< ...@@ -2140,21 +2139,14 @@ export default abstract class Server<
if (!res.sent) { if (!res.sent) {
const { generateEtags, poweredByHeader } = this.renderOpts const { generateEtags, poweredByHeader } = this.renderOpts
// Dev responses use `no-cache` so the browser can restore them from the // Documents and data responses must not be stored in development.
// HTTP cache on back/forward instead of reloading. HMR refresh responses // Browsers reuse a stored response for a history navigation without
// opt out into `no-store` because a superseded refresh's fetch is aborted // revalidating it, so a back navigation would restore a page from before
// mid-write: under `no-cache` the response is stored, so the abort leaves // the latest edit. Static assets never reach this code. They keep a
// the cache entry shared with the superseding refresh (same URL) // revalidatable `Cache-Control`, so the browser caches them between page
// half-written; Chromium then discards it and reissues the superseding // loads.
// refresh on a second connection as a duplicate request. `no-store` keeps
// that entry from being created.
if (this.dev) { if (this.dev) {
res.setHeader( res.setHeader('Cache-Control', 'no-store')
'Cache-Control',
req.headers[NEXT_HMR_REFRESH_HEADER] === '1'
? 'no-store'
: 'no-cache, must-revalidate'
)
cacheControl = undefined cacheControl = undefined
} }
......
...@@ -638,6 +638,10 @@ export async function initialize(opts: { ...@@ -638,6 +638,10 @@ export async function initialize(opts: {
res.setHeader('Cache-Control', 'public, max-age=0, must-revalidate') res.setHeader('Cache-Control', 'public, max-age=0, must-revalidate')
res.setHeader('Service-Worker-Allowed', config.basePath || '/') res.setHeader('Service-Worker-Allowed', config.basePath || '/')
} else if (opts.dev && !isNextFont(parsedUrl.pathname)) { } else if (opts.dev && !isNextFont(parsedUrl.pathname)) {
// Development assets stay revalidatable. `serveStatic` adds an
// `ETag`, so the browser sends a conditional request and reuses the
// stored body when the server answers `304`. This keeps the browser
// from downloading every chunk again on each page load.
res.setHeader('Cache-Control', 'no-cache, must-revalidate') res.setHeader('Cache-Control', 'no-cache, must-revalidate')
} else { } else {
res.setHeader( res.setHeader(
......
...@@ -743,7 +743,8 @@ export async function setupFsCheck(opts: { ...@@ -743,7 +743,8 @@ export async function setupFsCheck(opts: {
const fsPath = staticMetadataFiles.get(itemPath) const fsPath = staticMetadataFiles.get(itemPath)
if (fsPath) { if (fsPath) {
return { return {
// "nextStaticFolder" sets Cache-Control "no-store" on dev. // "nextStaticFolder" sets Cache-Control
// "no-cache, must-revalidate" on dev.
type: 'nextStaticFolder', type: 'nextStaticFolder',
fsPath, fsPath,
itemPath: fsPath, itemPath: fsPath,
......
...@@ -687,9 +687,14 @@ export const getHandler = ({ ...@@ -687,9 +687,14 @@ export const getHandler = ({
) )
} }
// In dev, we should not cache pages for any reason. // Documents and data responses must not be stored in development.
// Browsers reuse a stored response for a history navigation without
// revalidating it, so a back navigation would restore a page from
// before the latest edit. Static assets never reach this code. They
// keep a revalidatable `Cache-Control`, so the browser caches them
// between page loads.
if (routeModule.isDev) { if (routeModule.isDev) {
res.setHeader('Cache-Control', 'no-cache, must-revalidate') res.setHeader('Cache-Control', 'no-store')
} }
// Draft mode should never be cached // Draft mode should never be cached
......
export default function AppRoute() {
return <div>App Route</div>
}
import { nextTestSetup } from 'e2e-utils'
describe('dev Cache-Control header', () => {
const { next } = nextTestSetup({
files: __dirname,
})
it('should use no-cache 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 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 = {}
export default function Page() {
return <p id="about">About</p>
}
export default function RootLayout({ children }) { import type { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return ( return (
<html lang="en"> <html>
<body>{children}</body> <body>{children}</body>
</html> </html>
) )
......
import { value } from './value'
export default function Page() {
return (
<>
<p id="value">{value}</p>
<a id="to-about" href="/about">
About
</a>
</>
)
}
import type * as Playwright from 'playwright'
import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
import { readFile, writeFile } from 'fs/promises'
import { join } from 'path'
describe('dev Cache-Control', () => {
const { next } = nextTestSetup({
files: __dirname,
})
it('sends no-store for an app router document', async () => {
const res = await next.fetch('/')
expect(res.headers.get('Cache-Control')).toBe('no-store')
})
it('sends no-store for a pages router document', async () => {
const res = await next.fetch('/pages-route')
expect(res.headers.get('Cache-Control')).toBe('no-store')
})
it('keeps serving static assets from the browser cache', async () => {
const browser = await next.browser('/')
const assetStatusCodes: number[] = []
browser.on('response', (response: Playwright.Response) => {
const url = new URL(response.url())
// The webpack dev bundler adds a `v` query to some of its own chunks to
// bust the browser cache on every page load. Those are never cache hits
// by design.
if (
url.pathname.startsWith('/_next/static/') &&
!url.searchParams.has('v')
) {
assetStatusCodes.push(response.status())
}
})
// Only the responses of the second page load are of interest.
assetStatusCodes.length = 0
await browser.refresh()
await retry(async () => {
expect(assetStatusCodes.length).toBeGreaterThan(0)
})
// The dev server answers the revalidation of an unchanged asset with 304,
// so the browser reuses the body from its cache instead of downloading it
// again. `no-store` would force a full download on every page load.
expect([...new Set(assetStatusCodes)]).toEqual([304])
})
// Runs last because it edits a file that the other test cases rely on.
it('serves an edited page after a back navigation', async () => {
const browser = await next.browser('/')
expect(await browser.elementByCss('#value').text()).toBe('Value A')
// The debug channel is persisted once the main thread is idle. Before that
// has happened, a document that is restored from the HTTP cache falls back
// to `location.reload()`, which would hide a stale response.
await retry(async () => {
expect(
await browser.eval(() => (self as any).__NEXT_DEBUG_CHANNEL_PERSISTED)
).toBe(true)
})
// A plain anchor triggers a document navigation, so the browser can keep
// the page it navigates away from in its HTTP cache.
await browser.elementByCss('#to-about').click()
await browser.waitForElementByCss('#about')
const valueFile = join(next.testDir, 'app/value.ts')
const value = await readFile(valueFile, 'utf8')
await writeFile(valueFile, value.replace('Value A', 'Value B'))
// The dev server must serve the edited value before going back, so that a
// stale page can only come from the browser.
await retry(async () => {
const $ = await next.render$('/')
expect($('#value').text()).toBe('Value B')
})
await browser.back({ waitUntil: 'commit' })
await retry(async () => {
expect(await browser.elementByCss('#value').text()).toBe('Value B')
})
})
})
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
// Writing the agent rules files would trigger an unrelated Fast Refresh.
agentRules: false,
}
module.exports = nextConfig
...@@ -154,7 +154,12 @@ describe('bfcache-regression', () => { ...@@ -154,7 +154,12 @@ describe('bfcache-regression', () => {
if (isNextDev) { if (isNextDev) {
// Persistence only exists in dev. // Persistence only exists in dev.
it('should reload to recover when a debug channel entry was pruned by newer page loads', async () => { // TODO: Remove this test together with the debug channel persistence.
// Documents are served with `no-store`, so a back navigation always
// re-fetches the page and never restores it from the HTTP cache. The
// persisted entries that this test prunes are therefore never read, and
// the recovery reload it asserts no longer happens.
it.skip('should reload to recover when a debug channel entry was pruned by newer page loads', async () => {
// The debug channel for the initial document is buffered and persisted to // The debug channel for the initial document is buffered and persisted to
// IndexedDB so it can be restored when the browser serves the page from // IndexedDB so it can be restored when the browser serves the page from
// the HTTP cache (back-forward navigation). Persistence is bounded to a // the HTTP cache (back-forward navigation). Persistence is bounded to a
......
...@@ -15,14 +15,14 @@ describe('custom-cache-control', () => { ...@@ -15,14 +15,14 @@ describe('custom-cache-control', () => {
it('should have custom cache-control for app-ssg prerendered', async () => { it('should have custom cache-control for app-ssg prerendered', async () => {
const res = await next.fetch('/app-ssg/first') const res = await next.fetch('/app-ssg/first')
expect(res.headers.get('cache-control')).toBe( expect(res.headers.get('cache-control')).toBe(
isNextDev ? 'no-cache, must-revalidate' : 's-maxage=30' isNextDev ? 'no-store' : 's-maxage=30'
) )
}) })
it('should have custom cache-control for app-ssg lazy', async () => { it('should have custom cache-control for app-ssg lazy', async () => {
const res = await next.fetch('/app-ssg/lazy') const res = await next.fetch('/app-ssg/lazy')
expect(res.headers.get('cache-control')).toBe( expect(res.headers.get('cache-control')).toBe(
isNextDev ? 'no-cache, must-revalidate' : 's-maxage=31' isNextDev ? 'no-store' : 's-maxage=31'
) )
}) })
;(process.env.__NEXT_CACHE_COMPONENTS ? it.skip : it)( ;(process.env.__NEXT_CACHE_COMPONENTS ? it.skip : it)(
...@@ -31,9 +31,7 @@ describe('custom-cache-control', () => { ...@@ -31,9 +31,7 @@ describe('custom-cache-control', () => {
const res = await next.fetch('/app-ssg/another') const res = await next.fetch('/app-ssg/another')
// eslint-disable-next-line jest/no-standalone-expect // eslint-disable-next-line jest/no-standalone-expect
expect(res.headers.get('cache-control')).toBe( expect(res.headers.get('cache-control')).toBe(
isNextDev isNextDev ? 'no-store' : 's-maxage=120, stale-while-revalidate=31535880'
? 'no-cache, must-revalidate'
: 's-maxage=120, stale-while-revalidate=31535880'
) )
} }
) )
...@@ -41,44 +39,42 @@ describe('custom-cache-control', () => { ...@@ -41,44 +39,42 @@ describe('custom-cache-control', () => {
it('should have custom cache-control for app-ssr', async () => { it('should have custom cache-control for app-ssr', async () => {
const res = await next.fetch('/app-ssr') const res = await next.fetch('/app-ssr')
expect(res.headers.get('cache-control')).toBe( expect(res.headers.get('cache-control')).toBe(
isNextDev ? 'no-cache, must-revalidate' : 's-maxage=32' isNextDev ? 'no-store' : 's-maxage=32'
) )
}) })
it('should have custom cache-control for auto static page', async () => { it('should have custom cache-control for auto static page', async () => {
const res = await next.fetch('/pages-auto-static') const res = await next.fetch('/pages-auto-static')
expect(res.headers.get('cache-control')).toBe( expect(res.headers.get('cache-control')).toBe(
isNextDev ? 'no-cache, must-revalidate' : 's-maxage=33' isNextDev ? 'no-store' : 's-maxage=33'
) )
}) })
it('should have custom cache-control for pages-ssg prerendered', async () => { it('should have custom cache-control for pages-ssg prerendered', async () => {
const res = await next.fetch('/pages-ssg/first') const res = await next.fetch('/pages-ssg/first')
expect(res.headers.get('cache-control')).toBe( expect(res.headers.get('cache-control')).toBe(
isNextDev ? 'no-cache, must-revalidate' : 's-maxage=34' isNextDev ? 'no-store' : 's-maxage=34'
) )
}) })
it('should have custom cache-control for pages-ssg lazy', async () => { it('should have custom cache-control for pages-ssg lazy', async () => {
const res = await next.fetch('/pages-ssg/lazy') const res = await next.fetch('/pages-ssg/lazy')
expect(res.headers.get('cache-control')).toBe( expect(res.headers.get('cache-control')).toBe(
isNextDev ? 'no-cache, must-revalidate' : 's-maxage=35' isNextDev ? 'no-store' : 's-maxage=35'
) )
}) })
it('should have default cache-control for pages-ssg another', async () => { it('should have default cache-control for pages-ssg another', async () => {
const res = await next.fetch('/pages-ssg/another') const res = await next.fetch('/pages-ssg/another')
expect(res.headers.get('cache-control')).toBe( expect(res.headers.get('cache-control')).toBe(
isNextDev isNextDev ? 'no-store' : 's-maxage=120, stale-while-revalidate=31535880'
? 'no-cache, must-revalidate'
: 's-maxage=120, stale-while-revalidate=31535880'
) )
}) })
it('should have default cache-control for pages-ssr', async () => { it('should have default cache-control for pages-ssr', async () => {
const res = await next.fetch('/pages-ssr') const res = await next.fetch('/pages-ssr')
expect(res.headers.get('cache-control')).toBe( expect(res.headers.get('cache-control')).toBe(
isNextDev ? 'no-cache, must-revalidate' : 's-maxage=36' isNextDev ? 'no-store' : 's-maxage=36'
) )
}) })
}) })
...@@ -192,7 +192,7 @@ describe.skip('ppr-full', () => { ...@@ -192,7 +192,7 @@ describe.skip('ppr-full', () => {
if (isNextDeploy) { if (isNextDeploy) {
expect(cacheControl).toEqual('public, max-age=0, must-revalidate') expect(cacheControl).toEqual('public, max-age=0, must-revalidate')
} else if (isNextDev) { } else if (isNextDev) {
expect(cacheControl).toEqual('no-cache, must-revalidate') expect(cacheControl).toEqual('no-store')
} else if (dynamic === false || dynamic === 'force-static') { } else if (dynamic === false || dynamic === 'force-static') {
expect(cacheControl).toEqual( expect(cacheControl).toEqual(
revalidate === undefined revalidate === undefined
......
...@@ -94,9 +94,7 @@ describe('SSG notFound revalidate', () => { ...@@ -94,9 +94,7 @@ describe('SSG notFound revalidate', () => {
let $ = await next.render$('/fallback-blocking/hello') let $ = await next.render$('/fallback-blocking/hello')
expect(res.headers.get('cache-control')).toBe( expect(res.headers.get('cache-control')).toBe(
isNextDev isNextDev ? 'no-store' : 's-maxage=1, stale-while-revalidate=31535999'
? 'no-cache, must-revalidate'
: 's-maxage=1, stale-while-revalidate=31535999'
) )
expect(res.status).toBe(404) expect(res.status).toBe(404)
expect(JSON.parse($('#props').text()).notFound).toBe(true) expect(JSON.parse($('#props').text()).notFound).toBe(true)
...@@ -106,7 +104,7 @@ describe('SSG notFound revalidate', () => { ...@@ -106,7 +104,7 @@ describe('SSG notFound revalidate', () => {
$ = await next.render$('/fallback-blocking/hello') $ = await next.render$('/fallback-blocking/hello')
expect(res.headers.get('cache-control')).toBe( expect(res.headers.get('cache-control')).toBe(
isNextDev isNextDev
? 'no-cache, must-revalidate' ? 'no-store'
: 's-maxage=1, stale-while-revalidate=31535999' : 's-maxage=1, stale-while-revalidate=31535999'
) )
expect(res.status).toBe(200) expect(res.status).toBe(200)
...@@ -124,7 +122,7 @@ describe('SSG notFound revalidate', () => { ...@@ -124,7 +122,7 @@ describe('SSG notFound revalidate', () => {
const p = JSON.parse($r('#props').text()) const p = JSON.parse($r('#props').text())
expect(r.headers.get('cache-control')).toBe( expect(r.headers.get('cache-control')).toBe(
isNextDev isNextDev
? 'no-cache, must-revalidate' ? 'no-store'
: 's-maxage=1, stale-while-revalidate=31535999' : 's-maxage=1, stale-while-revalidate=31535999'
) )
expect(r.status).toBe(200) expect(r.status).toBe(200)
...@@ -143,7 +141,7 @@ describe('SSG notFound revalidate', () => { ...@@ -143,7 +141,7 @@ describe('SSG notFound revalidate', () => {
const res = await next.fetch('/fallback-true/world') const res = await next.fetch('/fallback-true/world')
expect(res.headers.get('cache-control')).toBe( expect(res.headers.get('cache-control')).toBe(
isNextDev isNextDev
? 'no-cache, must-revalidate' ? 'no-store'
: 's-maxage=1, stale-while-revalidate=31535999' : 's-maxage=1, stale-while-revalidate=31535999'
) )
expect(res.status).toBe(404) expect(res.status).toBe(404)
...@@ -157,7 +155,7 @@ describe('SSG notFound revalidate', () => { ...@@ -157,7 +155,7 @@ describe('SSG notFound revalidate', () => {
const props = JSON.parse($('#props').text()) const props = JSON.parse($('#props').text())
expect(res.headers.get('cache-control')).toBe( expect(res.headers.get('cache-control')).toBe(
isNextDev isNextDev
? 'no-cache, must-revalidate' ? 'no-store'
: 's-maxage=1, stale-while-revalidate=31535999' : 's-maxage=1, stale-while-revalidate=31535999'
) )
expect(res.status).toBe(200) expect(res.status).toBe(200)
...@@ -175,7 +173,7 @@ describe('SSG notFound revalidate', () => { ...@@ -175,7 +173,7 @@ describe('SSG notFound revalidate', () => {
const props3 = JSON.parse($r('#props').text()) const props3 = JSON.parse($r('#props').text())
expect(r.headers.get('cache-control')).toBe( expect(r.headers.get('cache-control')).toBe(
isNextDev isNextDev
? 'no-cache, must-revalidate' ? 'no-store'
: 's-maxage=1, stale-while-revalidate=31535999' : 's-maxage=1, stale-while-revalidate=31535999'
) )
expect(r.status).toBe(200) expect(r.status).toBe(200)
......
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