Commit b2614f26 authored by Janka Uryga's avatar Janka Uryga

label as Prefetch/Prefetchable depending on prefetch config

parent 06015036
...@@ -212,6 +212,7 @@ import { createPromiseWithResolvers } from '../../shared/lib/promise-with-resolv ...@@ -212,6 +212,7 @@ import { createPromiseWithResolvers } from '../../shared/lib/promise-with-resolv
import { ImageConfigContext } from '../../shared/lib/image-config-context.shared-runtime' import { ImageConfigContext } from '../../shared/lib/image-config-context.shared-runtime'
import { imageConfigDefault } from '../../shared/lib/image-config' import { imageConfigDefault } from '../../shared/lib/image-config'
import { RenderStage, StagedRenderingController } from './staged-rendering' import { RenderStage, StagedRenderingController } from './staged-rendering'
import { anySegmentHasRuntimePrefetchEnabled } from './staged-validation'
export type GetDynamicParamFromSegment = ( export type GetDynamicParamFromSegment = (
// [slug] / [[slug]] / [...slug] // [slug] / [[slug]] / [...slug]
...@@ -2768,7 +2769,16 @@ async function renderWithRestartOnCacheMissInDev( ...@@ -2768,7 +2769,16 @@ async function renderWithRestartOnCacheMissInDev(
getPayload: (requestStore: RequestStore) => Promise<RSCPayload>, getPayload: (requestStore: RequestStore) => Promise<RSCPayload>,
onError: (error: unknown) => void onError: (error: unknown) => void
) { ) {
const { htmlRequestId, renderOpts, requestId } = ctx const {
htmlRequestId,
renderOpts,
requestId,
componentMod: {
routeModule: {
userland: { loaderTree },
},
},
} = ctx
const { const {
clientReferenceManifest, clientReferenceManifest,
ComponentMod, ComponentMod,
...@@ -2777,6 +2787,9 @@ async function renderWithRestartOnCacheMissInDev( ...@@ -2777,6 +2787,9 @@ async function renderWithRestartOnCacheMissInDev(
} = renderOpts } = renderOpts
assertClientReferenceManifest(clientReferenceManifest) assertClientReferenceManifest(clientReferenceManifest)
const hasRuntimePrefetch =
await anySegmentHasRuntimePrefetchEnabled(loaderTree)
// If the render is restarted, we'll recreate a fresh request store // If the render is restarted, we'll recreate a fresh request store
let requestStore: RequestStore = initialRequestStore let requestStore: RequestStore = initialRequestStore
...@@ -2786,8 +2799,7 @@ async function renderWithRestartOnCacheMissInDev( ...@@ -2786,8 +2799,7 @@ async function renderWithRestartOnCacheMissInDev(
case RenderStage.Static: case RenderStage.Static:
return 'Prerender' return 'Prerender'
case RenderStage.Runtime: case RenderStage.Runtime:
// TODO: only label as "Prefetch" if the page has a `prefetch` config. return hasRuntimePrefetch ? 'Prefetch' : 'Prefetchable'
return 'Prefetch'
case RenderStage.Dynamic: case RenderStage.Dynamic:
return 'Server' return 'Server'
default: default:
......
import { getLayoutOrPageModule } from '../lib/app-dir-module'
import type { LoaderTree } from '../lib/app-dir-module'
import { parseLoaderTree } from '../../shared/lib/router/utils/parse-loader-tree'
import type { AppSegmentConfig } from '../../build/segment-config/app/app-segment-config'
export async function anySegmentHasRuntimePrefetchEnabled(
tree: LoaderTree
): Promise<boolean> {
const { mod: layoutOrPageMod } = await getLayoutOrPageModule(tree)
// TODO(restart-on-cache-miss): Does this work correctly for client page/layout modules?
const prefetchConfig = layoutOrPageMod
? (layoutOrPageMod as AppSegmentConfig).unstable_prefetch
: undefined
/** Whether this segment should use a runtime prefetch instead of a static prefetch. */
const hasRuntimePrefetch = prefetchConfig?.mode === 'runtime'
if (hasRuntimePrefetch) {
return true
}
const { parallelRoutes } = parseLoaderTree(tree)
for (const parallelRouteKey in parallelRoutes) {
const parallelRoute = parallelRoutes[parallelRouteKey]
const hasChildRuntimePrefetch =
await anySegmentHasRuntimePrefetchEnabled(parallelRoute)
if (hasChildRuntimePrefetch) {
return true
}
}
return false
}
...@@ -29,7 +29,7 @@ describe('cache-components-dev-warmup', () => { ...@@ -29,7 +29,7 @@ describe('cache-components-dev-warmup', () => {
) { ) {
// Match logs that contain the message, with any environment. // Match logs that contain the message, with any environment.
const logPattern = new RegExp( const logPattern = new RegExp(
`^(?=.*\\b${message}\\b)(?=.*\\b(Cache|Prerender|Prefetch|Server)\\b).*` `^(?=.*\\b${message}\\b)(?=.*\\b(Cache|Prerender|Prefetch|Prefetchable|Server)\\b).*`
) )
const logMessages = logs.map((log) => log.message) const logMessages = logs.map((log) => log.message)
const messages = logMessages.filter((message) => logPattern.test(message)) const messages = logMessages.filter((message) => logPattern.test(message))
...@@ -175,12 +175,12 @@ describe('cache-components-dev-warmup', () => { ...@@ -175,12 +175,12 @@ describe('cache-components-dev-warmup', () => {
// Private caches are dynamic holes in static prerenders, // Private caches are dynamic holes in static prerenders,
// so they shouldn't resolve in the static stage. // so they shouldn't resolve in the static stage.
assertLog(logs, 'after private cache read - page', 'Prefetch') assertLog(logs, 'after private cache read - page', 'Prefetchable')
assertLog(logs, 'after private cache read - layout', 'Prefetch') assertLog(logs, 'after private cache read - layout', 'Prefetchable')
assertLog( assertLog(
logs, logs,
'after successive private cache reads - page', 'after successive private cache reads - page',
'Prefetch' 'Prefetchable'
) )
assertLog(logs, 'after uncached fetch - layout', 'Server') assertLog(logs, 'after uncached fetch - layout', 'Server')
...@@ -204,8 +204,12 @@ describe('cache-components-dev-warmup', () => { ...@@ -204,8 +204,12 @@ describe('cache-components-dev-warmup', () => {
// Short lived caches are dynamic holes in static prerenders, // Short lived caches are dynamic holes in static prerenders,
// so they shouldn't resolve in the static stage. // so they shouldn't resolve in the static stage.
assertLog(logs, 'after short-lived cache read - page', 'Prefetch') assertLog(logs, 'after short-lived cache read - page', 'Prefetchable')
assertLog(logs, 'after short-lived cache read - layout', 'Prefetch') assertLog(
logs,
'after short-lived cache read - layout',
'Prefetchable'
)
assertLog(logs, 'after uncached fetch - layout', 'Server') assertLog(logs, 'after uncached fetch - layout', 'Server')
assertLog(logs, 'after uncached fetch - page', 'Server') assertLog(logs, 'after uncached fetch - page', 'Server')
...@@ -247,10 +251,10 @@ describe('cache-components-dev-warmup', () => { ...@@ -247,10 +251,10 @@ describe('cache-components-dev-warmup', () => {
assertLog(logs, 'after cache read - page', 'Prerender') assertLog(logs, 'after cache read - page', 'Prerender')
// TODO: we should only label this as "Prefetch" if there's a prefetch config. // TODO: we should only label this as "Prefetch" if there's a prefetch config.
assertLog(logs, `after cookies`, 'Prefetch') assertLog(logs, `after cookies`, 'Prefetchable')
assertLog(logs, `after headers`, 'Prefetch') assertLog(logs, `after headers`, 'Prefetchable')
assertLog(logs, `after params`, 'Prefetch') assertLog(logs, `after params`, 'Prefetchable')
assertLog(logs, `after searchParams`, 'Prefetch') assertLog(logs, `after searchParams`, 'Prefetchable')
assertLog(logs, 'after connection', 'Server') assertLog(logs, 'after connection', 'Server')
} }
......
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