Unverified Commit 90090ba4 authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Include HMR refresh hash in `"use cache"` cache keys (#75474)

Today, when editing server components that use `"use cache"` functions,
only updates to the uncached parts of the component will be reflected in
the rendered result after the HMR refresh was applied. Any cached
functions/components will show the same content as before the edit.

This is problematic when a function with a `"use cache"` directive is
edited. In this case, the edits won't be applied, and stale content is
shown in the browser, until the dev server is restarted.

With this PR, we are now including an HMR refresh hash in the cache key
for all `"use cache"` functions. This ensures that those functions are
revalidated when a server component is edited, which avoids showing
stale content.

However, while the `"use cache"` functions are revalidated during the
HMR refresh, we will still restore any fetches inside of `"use cache"`
from the [server components HMR
cache](https://nextjs.org/docs/app/api-reference/config/next-config-js/serverComponentsHmrCache)
(unless their input changed because of the edit).

For Webpack, a real content hash is used, which means that if an edit is
reverted to a prior edit, the previously cached data will be retrieved
from the cache. For Turbopack, the HMR hash is currently just an
incrementing counter, which may be improved in a follow-up PR.

closes NAR-65
parent 3e5abe56
...@@ -481,13 +481,20 @@ function processMessage( ...@@ -481,13 +481,20 @@ function processMessage(
JSON.stringify({ JSON.stringify({
event: 'server-component-reload-page', event: 'server-component-reload-page',
clientId: __nextDevClientId, clientId: __nextDevClientId,
hash: obj.hash,
}) })
) )
// Store the latest hash in a session cookie so that it's sent back to the
// server with any subsequent requests.
document.cookie = `__next_hmr_refresh_hash__=${obj.hash}`
if (RuntimeErrorHandler.hadRuntimeError) { if (RuntimeErrorHandler.hadRuntimeError) {
if (reloading) return if (reloading) return
reloading = true reloading = true
return window.location.reload() return window.location.reload()
} }
startTransition(() => { startTransition(() => {
router.hmrRefresh() router.hmrRefresh()
dispatcher.onRefresh() dispatcher.onRefresh()
......
...@@ -160,6 +160,9 @@ export type UseCacheStore = { ...@@ -160,6 +160,9 @@ export type UseCacheStore = {
explicitExpire: undefined | number // server expiration time explicitExpire: undefined | number // server expiration time
explicitStale: undefined | number // client expiration time explicitStale: undefined | number // client expiration time
tags: null | string[] tags: null | string[]
readonly hmrRefreshHash: string | undefined
readonly isHmrRefresh: boolean
readonly serverComponentsHmrCache: ServerComponentsHmrCache | undefined
} & PhasePartial } & PhasePartial
export type UnstableCacheStore = { export type UnstableCacheStore = {
...@@ -243,3 +246,13 @@ export function getRenderResumeDataCache( ...@@ -243,3 +246,13 @@ export function getRenderResumeDataCache(
return null return null
} }
export function getHmrRefreshHash(
workUnitStore: WorkUnitStore
): string | undefined {
return workUnitStore.type === 'cache'
? workUnitStore.hmrRefreshHash
: workUnitStore.type === 'request'
? workUnitStore.cookies.get('__next_hmr_refresh_hash__')?.value
: undefined
}
...@@ -467,7 +467,8 @@ export async function createHotReloaderTurbopack( ...@@ -467,7 +467,8 @@ export async function createHotReloaderTurbopack(
includeIssues: boolean, includeIssues: boolean,
endpoint: Endpoint, endpoint: Endpoint,
makePayload: ( makePayload: (
change: TurbopackResult change: TurbopackResult,
hash: string
) => Promise<HMR_ACTION_TYPES> | HMR_ACTION_TYPES | void, ) => Promise<HMR_ACTION_TYPES> | HMR_ACTION_TYPES | void,
onError?: ( onError?: (
error: Error error: Error
...@@ -486,7 +487,8 @@ export async function createHotReloaderTurbopack( ...@@ -486,7 +487,8 @@ export async function createHotReloaderTurbopack(
for await (const change of changed) { for await (const change of changed) {
processIssues(currentEntryIssues, key, change, false, true) processIssues(currentEntryIssues, key, change, false, true)
const payload = await makePayload(change) // TODO: Get an actual content hash from Turbopack.
const payload = await makePayload(change, String(++hmrHash))
if (payload) { if (payload) {
sendHmr(key, payload) sendHmr(key, payload)
} }
...@@ -910,6 +912,7 @@ export async function createHotReloaderTurbopack( ...@@ -910,6 +912,7 @@ export async function createHotReloaderTurbopack(
await clearAllModuleContexts() await clearAllModuleContexts()
this.send({ this.send({
action: HMR_ACTIONS_SENT_TO_BROWSER.SERVER_COMPONENT_CHANGES, action: HMR_ACTIONS_SENT_TO_BROWSER.SERVER_COMPONENT_CHANGES,
hash: String(++hmrHash),
}) })
} }
}, },
......
...@@ -81,6 +81,7 @@ export interface ReloadPageAction { ...@@ -81,6 +81,7 @@ export interface ReloadPageAction {
interface ServerComponentChangesAction { interface ServerComponentChangesAction {
action: HMR_ACTIONS_SENT_TO_BROWSER.SERVER_COMPONENT_CHANGES action: HMR_ACTIONS_SENT_TO_BROWSER.SERVER_COMPONENT_CHANGES
hash: string
} }
interface MiddlewareChangesAction { interface MiddlewareChangesAction {
......
...@@ -407,9 +407,10 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface { ...@@ -407,9 +407,10 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {
} }
} }
protected async refreshServerComponents(): Promise<void> { protected async refreshServerComponents(hash: string): Promise<void> {
this.send({ this.send({
action: HMR_ACTIONS_SENT_TO_BROWSER.SERVER_COMPONENT_CHANGES, action: HMR_ACTIONS_SENT_TO_BROWSER.SERVER_COMPONENT_CHANGES,
hash,
// TODO: granular reloading of changes // TODO: granular reloading of changes
// entrypoints: serverComponentChanges, // entrypoints: serverComponentChanges,
}) })
...@@ -1359,7 +1360,7 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface { ...@@ -1359,7 +1360,7 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {
} }
) )
this.multiCompiler.hooks.done.tap('NextjsHotReloaderForServer', () => { this.multiCompiler.hooks.done.tap('NextjsHotReloaderForServer', (stats) => {
const reloadAfterInvalidation = this.reloadAfterInvalidation const reloadAfterInvalidation = this.reloadAfterInvalidation
this.reloadAfterInvalidation = false this.reloadAfterInvalidation = false
...@@ -1401,7 +1402,7 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface { ...@@ -1401,7 +1402,7 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {
reloadAfterInvalidation reloadAfterInvalidation
) { ) {
this.resetFetch() this.resetFetch()
this.refreshServerComponents() this.refreshServerComponents(stats.hash)
} }
changedClientPages.clear() changedClientPages.clear()
......
...@@ -106,7 +106,8 @@ export type StartChangeSubscription = ( ...@@ -106,7 +106,8 @@ export type StartChangeSubscription = (
includeIssues: boolean, includeIssues: boolean,
endpoint: Endpoint, endpoint: Endpoint,
makePayload: ( makePayload: (
change: TurbopackResult change: TurbopackResult,
hash: string
) => Promise<HMR_ACTION_TYPES> | HMR_ACTION_TYPES | void, ) => Promise<HMR_ACTION_TYPES> | HMR_ACTION_TYPES | void,
onError?: (e: Error) => Promise<HMR_ACTION_TYPES> | HMR_ACTION_TYPES | void onError?: (e: Error) => Promise<HMR_ACTION_TYPES> | HMR_ACTION_TYPES | void
) => Promise<void> ) => Promise<void>
...@@ -341,7 +342,7 @@ export async function handleRouteType({ ...@@ -341,7 +342,7 @@ export async function handleRouteType({
key, key,
true, true,
route.rscEndpoint, route.rscEndpoint,
(change) => { (change, hash) => {
if (change.issues.some((issue) => issue.severity === 'error')) { if (change.issues.some((issue) => issue.severity === 'error')) {
// Ignore any updates that has errors // Ignore any updates that has errors
// There will be another update without errors eventually // There will be another update without errors eventually
...@@ -351,11 +352,13 @@ export async function handleRouteType({ ...@@ -351,11 +352,13 @@ export async function handleRouteType({
readyIds?.delete(pathname) readyIds?.delete(pathname)
return { return {
action: HMR_ACTIONS_SENT_TO_BROWSER.SERVER_COMPONENT_CHANGES, action: HMR_ACTIONS_SENT_TO_BROWSER.SERVER_COMPONENT_CHANGES,
hash,
} }
}, },
() => { (e) => {
return { return {
action: HMR_ACTIONS_SENT_TO_BROWSER.SERVER_COMPONENT_CHANGES, action: HMR_ACTIONS_SENT_TO_BROWSER.RELOAD_PAGE,
data: `error in ${page} app-page subscription: ${e}`,
} }
} }
) )
......
...@@ -15,10 +15,7 @@ import { markCurrentScopeAsDynamic } from '../app-render/dynamic-rendering' ...@@ -15,10 +15,7 @@ import { markCurrentScopeAsDynamic } from '../app-render/dynamic-rendering'
import { makeHangingPromise } from '../dynamic-rendering-utils' import { makeHangingPromise } from '../dynamic-rendering-utils'
import type { FetchMetric } from '../base-http' import type { FetchMetric } from '../base-http'
import { createDedupeFetch } from './dedupe-fetch' import { createDedupeFetch } from './dedupe-fetch'
import type { import type { WorkUnitAsyncStorage } from '../app-render/work-unit-async-storage.external'
WorkUnitAsyncStorage,
RequestStore,
} from '../app-render/work-unit-async-storage.external'
import { import {
CachedRouteKind, CachedRouteKind,
IncrementalCacheKind, IncrementalCacheKind,
...@@ -537,14 +534,15 @@ export function createPatchedFetcher( ...@@ -537,14 +534,15 @@ export function createPatchedFetcher(
let cacheKey: string | undefined let cacheKey: string | undefined
const { incrementalCache } = workStore const { incrementalCache } = workStore
const requestStore: undefined | RequestStore = const useCacheOrRequestStore =
workUnitStore !== undefined && workUnitStore.type === 'request' workUnitStore?.type === 'request' || workUnitStore?.type === 'cache'
? workUnitStore ? workUnitStore
: undefined : undefined
if ( if (
incrementalCache && incrementalCache &&
(isCacheableRevalidate || requestStore?.serverComponentsHmrCache) (isCacheableRevalidate ||
useCacheOrRequestStore?.serverComponentsHmrCache)
) { ) {
try { try {
cacheKey = await incrementalCache.generateCacheKey( cacheKey = await incrementalCache.generateCacheKey(
...@@ -631,7 +629,7 @@ export function createPatchedFetcher( ...@@ -631,7 +629,7 @@ export function createPatchedFetcher(
incrementalCache && incrementalCache &&
cacheKey && cacheKey &&
(isCacheableRevalidate || (isCacheableRevalidate ||
requestStore?.serverComponentsHmrCache) useCacheOrRequestStore?.serverComponentsHmrCache)
) { ) {
const normalizedRevalidate = const normalizedRevalidate =
finalRevalidate >= INFINITE_CACHE finalRevalidate >= INFINITE_CACHE
...@@ -701,7 +699,7 @@ export function createPatchedFetcher( ...@@ -701,7 +699,7 @@ export function createPatchedFetcher(
url: cloned1.url, url: cloned1.url,
} }
requestStore?.serverComponentsHmrCache?.set( useCacheOrRequestStore?.serverComponentsHmrCache?.set(
cacheKey, cacheKey,
fetchedData fetchedData
) )
...@@ -753,11 +751,11 @@ export function createPatchedFetcher( ...@@ -753,11 +751,11 @@ export function createPatchedFetcher(
let cachedFetchData: CachedFetchData | undefined let cachedFetchData: CachedFetchData | undefined
if ( if (
requestStore?.isHmrRefresh && useCacheOrRequestStore?.isHmrRefresh &&
requestStore.serverComponentsHmrCache useCacheOrRequestStore.serverComponentsHmrCache
) { ) {
cachedFetchData = cachedFetchData =
requestStore.serverComponentsHmrCache.get(cacheKey) useCacheOrRequestStore.serverComponentsHmrCache.get(cacheKey)
isHmrRefreshCache = true isHmrRefreshCache = true
} }
......
...@@ -20,6 +20,7 @@ import type { ...@@ -20,6 +20,7 @@ import type {
WorkUnitStore, WorkUnitStore,
} from '../app-render/work-unit-async-storage.external' } from '../app-render/work-unit-async-storage.external'
import { import {
getHmrRefreshHash,
getRenderResumeDataCache, getRenderResumeDataCache,
getPrerenderResumeDataCache, getPrerenderResumeDataCache,
workUnitAsyncStorage, workUnitAsyncStorage,
...@@ -43,6 +44,13 @@ import { cacheHandlerGlobal, DYNAMIC_EXPIRE } from './constants' ...@@ -43,6 +44,13 @@ import { cacheHandlerGlobal, DYNAMIC_EXPIRE } from './constants'
import { UseCacheTimeoutError } from './use-cache-errors' import { UseCacheTimeoutError } from './use-cache-errors'
import { createHangingInputAbortSignal } from '../app-render/dynamic-rendering' import { createHangingInputAbortSignal } from '../app-render/dynamic-rendering'
type CacheKeyParts = [
buildId: string,
hmrRefreshHash: string | undefined,
id: string,
args: unknown[],
]
const isEdgeRuntime = process.env.NEXT_RUNTIME === 'edge' const isEdgeRuntime = process.env.NEXT_RUNTIME === 'edge'
const cacheHandlerMap: Map<string, CacheHandler> = new Map() const cacheHandlerMap: Map<string, CacheHandler> = new Map()
...@@ -123,6 +131,12 @@ function generateCacheEntryWithCacheContext( ...@@ -123,6 +131,12 @@ function generateCacheEntryWithCacheContext(
) )
} }
const useCacheOrRequestStore =
outerWorkUnitStore?.type === 'request' ||
outerWorkUnitStore?.type === 'cache'
? outerWorkUnitStore
: undefined
// Initialize the Store for this Cache entry. // Initialize the Store for this Cache entry.
const cacheStore: UseCacheStore = { const cacheStore: UseCacheStore = {
type: 'cache', type: 'cache',
...@@ -139,7 +153,11 @@ function generateCacheEntryWithCacheContext( ...@@ -139,7 +153,11 @@ function generateCacheEntryWithCacheContext(
explicitExpire: undefined, explicitExpire: undefined,
explicitStale: undefined, explicitStale: undefined,
tags: null, tags: null,
hmrRefreshHash: outerWorkUnitStore && getHmrRefreshHash(outerWorkUnitStore),
isHmrRefresh: useCacheOrRequestStore?.isHmrRefresh ?? false,
serverComponentsHmrCache: useCacheOrRequestStore?.serverComponentsHmrCache,
} }
return workUnitAsyncStorage.run( return workUnitAsyncStorage.run(
cacheStore, cacheStore,
generateCacheEntryImpl, generateCacheEntryImpl,
...@@ -278,12 +296,14 @@ async function generateCacheEntryImpl( ...@@ -278,12 +296,14 @@ async function generateCacheEntryImpl(
): Promise<[ReadableStream, Promise<CacheEntry>]> { ): Promise<[ReadableStream, Promise<CacheEntry>]> {
const temporaryReferences = createServerTemporaryReferenceSet() const temporaryReferences = createServerTemporaryReferenceSet()
const [, , args] = const [, , , args] =
typeof encodedArguments === 'string' typeof encodedArguments === 'string'
? await decodeReply<any[]>(encodedArguments, getServerModuleMap(), { ? await decodeReply<CacheKeyParts>(
temporaryReferences, encodedArguments,
}) getServerModuleMap(),
: await decodeReplyFromAsyncIterable<any[]>( { temporaryReferences }
)
: await decodeReplyFromAsyncIterable<CacheKeyParts>(
{ {
async *[Symbol.asyncIterator]() { async *[Symbol.asyncIterator]() {
for (const entry of encodedArguments) { for (const entry of encodedArguments) {
...@@ -507,6 +527,13 @@ export function cache( ...@@ -507,6 +527,13 @@ export function cache(
// the implementation. // the implementation.
const buildId = workStore.buildId const buildId = workStore.buildId
// In dev mode, when the HMR refresh hash is set, we include it in the
// cache key. This ensures that cache entries are not reused when server
// components have been edited. This is a very coarse approach. But it's
// also only a temporary solution until Action IDs are unique per
// implementation. Remove this once Action IDs hash the implementation.
const hmrRefreshHash = workUnitStore && getHmrRefreshHash(workUnitStore)
const hangingInputAbortSignal = const hangingInputAbortSignal =
workUnitStore?.type === 'prerender' workUnitStore?.type === 'prerender'
? createHangingInputAbortSignal(workUnitStore) ? createHangingInputAbortSignal(workUnitStore)
...@@ -538,17 +565,18 @@ export function cache( ...@@ -538,17 +565,18 @@ export function cache(
} }
const temporaryReferences = createClientTemporaryReferenceSet() const temporaryReferences = createClientTemporaryReferenceSet()
const encodedArguments: FormData | string = await encodeReply( const cacheKeyParts: CacheKeyParts = [buildId, hmrRefreshHash, id, args]
[buildId, id, args], const encodedCacheKeyParts: FormData | string = await encodeReply(
cacheKeyParts,
{ temporaryReferences, signal: hangingInputAbortSignal } { temporaryReferences, signal: hangingInputAbortSignal }
) )
const serializedCacheKey = const serializedCacheKey =
typeof encodedArguments === 'string' typeof encodedCacheKeyParts === 'string'
? // Fast path for the simple case for simple inputs. We let the CacheHandler ? // Fast path for the simple case for simple inputs. We let the CacheHandler
// Convert it to an ArrayBuffer if it wants to. // Convert it to an ArrayBuffer if it wants to.
encodedArguments encodedCacheKeyParts
: await encodeFormData(encodedArguments) : await encodeFormData(encodedCacheKeyParts)
let stream: undefined | ReadableStream = undefined let stream: undefined | ReadableStream = undefined
...@@ -669,7 +697,7 @@ export function cache( ...@@ -669,7 +697,7 @@ export function cache(
workStore, workStore,
workUnitStore, workUnitStore,
clientReferenceManifest, clientReferenceManifest,
encodedArguments, encodedCacheKeyParts,
fn, fn,
timeoutError timeoutError
) )
...@@ -729,7 +757,7 @@ export function cache( ...@@ -729,7 +757,7 @@ export function cache(
workStore, workStore,
undefined, // This is not running within the context of this unit. undefined, // This is not running within the context of this unit.
clientReferenceManifest, clientReferenceManifest,
encodedArguments, encodedCacheKeyParts,
fn, fn,
timeoutError timeoutError
) )
......
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
async function getData() {
'use cache'
return fetch('https://next-data-api-endpoint.vercel.app/api/random').then(
(res) => res.text().then((text) => [text, 'foo', Math.random()] as const)
)
}
export default async function Page() {
const [fetchedRandom, text, mathRandom] = await getData()
return (
<>
<p id="fetchedRandom">{fetchedRandom}</p>
<p id="text">{text}</p>
<p id="mathRandom">{mathRandom}</p>
</>
)
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
useCache: true,
},
logging: {
fetches: {
hmrRefreshes: true,
},
},
}
module.exports = nextConfig
import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
describe('use-cache-hmr', () => {
const { next, isTurbopack } = nextTestSetup({
files: __dirname,
})
it('should update cached data after editing a file', async () => {
const browser = await next.browser('/')
const [initialFetchedRandom, initialText, initialMathRandom] =
await Promise.all([
browser.elementById('fetchedRandom').text(),
browser.elementById('text').text(),
browser.elementById('mathRandom').text(),
])
expect(initialFetchedRandom).toMatch(/[0,1]\.\d+/)
expect(initialText).toBe('foo')
expect(initialMathRandom).toMatch(/[0,1]\.\d+/)
// Edit something inside of "use cache" in the page.tsx file.
await next.patchFile('app/page.tsx', (content) =>
content.replace('foo', 'bar')
)
let newFetchedRandom: string
let newText: string
let newMathRandom: string
await retry(async () => {
;[newFetchedRandom, newText, newMathRandom] = await Promise.all([
browser.elementById('fetchedRandom').text(),
browser.elementById('text').text(),
browser.elementById('mathRandom').text(),
])
// Cached via server components HMR cache:
expect(newFetchedRandom).toBe(initialFetchedRandom)
// Edited value:
expect(newText).toBe('bar')
// Newly computed value due to cache miss.
expect(newMathRandom).not.toBe(initialMathRandom)
expect(newMathRandom).toMatch(/[0,1]\.\d+/)
})
// Now revert the edit.
await next.patchFile('app/page.tsx', (content) =>
content.replace('bar', 'foo')
)
await retry(async () => {
const [fetchedRandom, text, mathRandom] = await Promise.all([
browser.elementById('fetchedRandom').text(),
browser.elementById('text').text(),
browser.elementById('mathRandom').text(),
])
// Cached via server components HMR cache:
expect(fetchedRandom).toBe(initialFetchedRandom)
// Edited value:
expect(text).toBe(initialText)
// Newly computed value due to cache miss, because the initial request did
// not use an HMR hash for the cache key.
// TODO: Can we get a cache hit here? It's a micro optimization though.
expect(mathRandom).not.toBe(initialFetchedRandom)
expect(mathRandom).not.toBe(newMathRandom)
expect(mathRandom).toMatch(/[0,1]\.\d+/)
})
// Apply the initial edit again.
await next.patchFile(
'app/page.tsx',
(content) => content.replace('foo', 'bar'),
async () =>
retry(async () => {
const [fetchedRandom, text, mathRandom] = await Promise.all([
browser.elementById('fetchedRandom').text(),
browser.elementById('text').text(),
browser.elementById('mathRandom').text(),
])
// This should be a full cache hit now:
expect(fetchedRandom).toBe(newFetchedRandom)
expect(text).toBe(newText)
if (isTurbopack) {
// TODO: Turbopack does not provide content hashes during HMR, so we
// actually get a cache miss. However, fetchedRandom is still cached
// because of the server components HMR cache.
expect(mathRandom).not.toBe(newMathRandom)
expect(mathRandom).toMatch(/[0,1]\.\d+/)
} else {
expect(mathRandom).toBe(newMathRandom)
}
})
)
})
})
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