Unverified Commit a976c918 authored by Jiwon Choi's avatar Jiwon Choi Committed by GitHub

Sync devFallbackParams when generateStaticParams change (#85741)

Since `generateStaticParams` runs in the background, when accessing the
`devFallbackParams` during the render, it is still set to the previous
result from the cache. Therefore, when the result has changed, re-render
the RSC to sync the `devFallbackParams` with the new result.

Closes NAR-491

---------
Co-authored-by: 's avatarJosh Story <story@hey.com>
parent 249fcec6
...@@ -2096,8 +2096,7 @@ async function renderToHTMLOrFlightImpl( ...@@ -2096,8 +2096,7 @@ async function renderToHTMLOrFlightImpl(
null null
const rootParams = getRootParams(loaderTree, ctx.getDynamicParamFromSegment) const rootParams = getRootParams(loaderTree, ctx.getDynamicParamFromSegment)
const devValidatingFallbackParams = const devFallbackParams = getRequestMeta(req, 'devFallbackParams') || null
getRequestMeta(req, 'devValidatingFallbackParams') || null
const createRequestStore = createRequestStoreForRender.bind( const createRequestStore = createRequestStoreForRender.bind(
null, null,
...@@ -2111,7 +2110,7 @@ async function renderToHTMLOrFlightImpl( ...@@ -2111,7 +2110,7 @@ async function renderToHTMLOrFlightImpl(
isHmrRefresh, isHmrRefresh,
serverComponentsHmrCache, serverComponentsHmrCache,
renderResumeDataCache, renderResumeDataCache,
devValidatingFallbackParams devFallbackParams
) )
const requestStore = createRequestStore() const requestStore = createRequestStore()
...@@ -2201,7 +2200,7 @@ async function renderToHTMLOrFlightImpl( ...@@ -2201,7 +2200,7 @@ async function renderToHTMLOrFlightImpl(
postponedState, postponedState,
metadata, metadata,
undefined, // Prevent restartable-render behavior in dev + Cache Components mode undefined, // Prevent restartable-render behavior in dev + Cache Components mode
devValidatingFallbackParams devFallbackParams
) )
return new RenderResult(stream, { return new RenderResult(stream, {
...@@ -2245,7 +2244,7 @@ async function renderToHTMLOrFlightImpl( ...@@ -2245,7 +2244,7 @@ async function renderToHTMLOrFlightImpl(
// and we currently we don't copy changes over when creating a new store, // and we currently we don't copy changes over when creating a new store,
// so the restarted render wouldn't be correct. // so the restarted render wouldn't be correct.
didExecuteServerAction ? undefined : createRequestStore, didExecuteServerAction ? undefined : createRequestStore,
devValidatingFallbackParams devFallbackParams
) )
// Invalid dynamic usages should only error the request in development. // Invalid dynamic usages should only error the request in development.
...@@ -2451,7 +2450,7 @@ async function renderToStream( ...@@ -2451,7 +2450,7 @@ async function renderToStream(
postponedState: PostponedState | null, postponedState: PostponedState | null,
metadata: AppPageRenderResultMetadata, metadata: AppPageRenderResultMetadata,
createRequestStore: (() => RequestStore) | undefined, createRequestStore: (() => RequestStore) | undefined,
devValidatingFallbackParams: OpaqueFallbackRouteParams | null devFallbackParams: OpaqueFallbackRouteParams | null
): Promise<ReadableStream<Uint8Array>> { ): Promise<ReadableStream<Uint8Array>> {
/* eslint-disable @next/internal/no-ambiguous-jsx -- React Client */ /* eslint-disable @next/internal/no-ambiguous-jsx -- React Client */
const { const {
...@@ -2689,7 +2688,7 @@ async function renderToStream( ...@@ -2689,7 +2688,7 @@ async function renderToStream(
res.statusCode === 404, res.statusCode === 404,
clientReferenceManifest, clientReferenceManifest,
requestStore, requestStore,
devValidatingFallbackParams devFallbackParams
) )
} else { } else {
// This is a dynamic render. We don't do dynamic tracking because we're not prerendering // This is a dynamic render. We don't do dynamic tracking because we're not prerendering
......
...@@ -2298,7 +2298,7 @@ export default abstract class Server< ...@@ -2298,7 +2298,7 @@ export default abstract class Server<
if (smallestFallbackRouteParams) { if (smallestFallbackRouteParams) {
addRequestMeta( addRequestMeta(
req, req,
'devValidatingFallbackParams', 'devFallbackParams',
createOpaqueFallbackRouteParams(smallestFallbackRouteParams)! createOpaqueFallbackRouteParams(smallestFallbackRouteParams)!
) )
} }
......
...@@ -76,6 +76,7 @@ import { ...@@ -76,6 +76,7 @@ import {
import type { PrerenderManifest } from '../../build' import type { PrerenderManifest } from '../../build'
import { getRouteRegex } from '../../shared/lib/router/utils/route-regex' import { getRouteRegex } from '../../shared/lib/router/utils/route-regex'
import type { PrerenderedRoute } from '../../build/static-paths/types' import type { PrerenderedRoute } from '../../build/static-paths/types'
import { HMR_MESSAGE_SENT_TO_BROWSER } from './hot-reloader-types'
// Load ReactDevOverlay only when needed // Load ReactDevOverlay only when needed
let PagesDevOverlayBridgeImpl: PagesDevOverlayBridgeType let PagesDevOverlayBridgeImpl: PagesDevOverlayBridgeType
...@@ -802,6 +803,24 @@ export default class DevServer extends Server { ...@@ -802,6 +803,24 @@ export default class DevServer extends Server {
) )
} }
} }
// Since generateStaticParams run on the background, when accessing the
// devFallbackParams during the render, it is still set to the previous
// result from the cache. Therefore when the result has changed, re-render
// the Server Component to sync the devFallbackParams with the new result.
if (
isAppPath &&
this.nextConfig.cacheComponents &&
// Ensure this is not the first invocation.
result &&
// Ideally, we would want to compare the whole objects, but that is too expensive.
result.prerenderedRoutes?.length !== prerenderedRoutes?.length
) {
this.bundlerService.triggerHMR({
type: HMR_MESSAGE_SENT_TO_BROWSER.SERVER_COMPONENT_CHANGES,
hash: `generateStaticParams-${Date.now()}`,
})
}
} }
if (!isAppPath && this.nextConfig.output === 'export') { if (!isAppPath && this.nextConfig.output === 'export') {
......
...@@ -4,7 +4,10 @@ import type { WorkerRequestHandler } from './types' ...@@ -4,7 +4,10 @@ import type { WorkerRequestHandler } from './types'
import { LRUCache } from './lru-cache' import { LRUCache } from './lru-cache'
import { createRequestResponseMocks } from './mock-request' import { createRequestResponseMocks } from './mock-request'
import { HMR_MESSAGE_SENT_TO_BROWSER } from '../dev/hot-reloader-types' import {
HMR_MESSAGE_SENT_TO_BROWSER,
type HmrMessageSentToBrowser,
} from '../dev/hot-reloader-types'
import type { ReactDebugChannelForBrowser } from '../dev/debug-channel' import type { ReactDebugChannelForBrowser } from '../dev/debug-channel'
import type { ServerCacheStatus } from '../../next-devtools/dev-overlay/cache-indicator' import type { ServerCacheStatus } from '../../next-devtools/dev-overlay/cache-indicator'
...@@ -137,4 +140,8 @@ export class DevBundlerService { ...@@ -137,4 +140,8 @@ export class DevBundlerService {
public close() { public close() {
this.bundler.hotReloader.close() this.bundler.hotReloader.close()
} }
public triggerHMR(message: HmrMessageSentToBrowser) {
this.bundler.hotReloader.send(message)
}
} }
...@@ -261,7 +261,7 @@ export interface RequestMeta { ...@@ -261,7 +261,7 @@ export interface RequestMeta {
/** /**
* DEV only: The fallback params that should be used when validating prerenders during dev * DEV only: The fallback params that should be used when validating prerenders during dev
*/ */
devValidatingFallbackParams?: OpaqueFallbackRouteParams devFallbackParams?: OpaqueFallbackRouteParams
/** /**
* DEV only: Request timings in process.hrtime.bigint() * DEV only: Request timings in process.hrtime.bigint()
......
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