Unverified Commit 40dd4ce4 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Delete un-used prefetch outputs with PPR (#86100)

parent 36f50c5a
...@@ -12,7 +12,6 @@ import { ...@@ -12,7 +12,6 @@ import {
} from '../client/components/app-router-headers' } from '../client/components/app-router-headers'
import { import {
RSC_SUFFIX, RSC_SUFFIX,
RSC_PREFETCH_SUFFIX,
RSC_SEGMENT_SUFFIX, RSC_SEGMENT_SUFFIX,
RSC_SEGMENTS_DIR_SUFFIX, RSC_SEGMENTS_DIR_SUFFIX,
NEXT_RESUME_HEADER, NEXT_RESUME_HEADER,
...@@ -123,7 +122,6 @@ export function generateRoutesManifest( ...@@ -123,7 +122,6 @@ export function generateRoutesManifest(
didPostponeHeader: NEXT_DID_POSTPONE_HEADER, didPostponeHeader: NEXT_DID_POSTPONE_HEADER,
contentTypeHeader: RSC_CONTENT_TYPE_HEADER, contentTypeHeader: RSC_CONTENT_TYPE_HEADER,
suffix: RSC_SUFFIX, suffix: RSC_SUFFIX,
prefetchSuffix: RSC_PREFETCH_SUFFIX,
prefetchSegmentHeader: NEXT_ROUTER_SEGMENT_PREFETCH_HEADER, prefetchSegmentHeader: NEXT_ROUTER_SEGMENT_PREFETCH_HEADER,
prefetchSegmentSuffix: RSC_SEGMENT_SUFFIX, prefetchSegmentSuffix: RSC_SEGMENT_SUFFIX,
prefetchSegmentDirSuffix: RSC_SEGMENTS_DIR_SUFFIX, prefetchSegmentDirSuffix: RSC_SEGMENTS_DIR_SUFFIX,
......
...@@ -24,7 +24,6 @@ import { ...@@ -24,7 +24,6 @@ import {
PROXY_FILENAME, PROXY_FILENAME,
PAGES_DIR_ALIAS, PAGES_DIR_ALIAS,
INSTRUMENTATION_HOOK_FILENAME, INSTRUMENTATION_HOOK_FILENAME,
RSC_PREFETCH_SUFFIX,
RSC_SUFFIX, RSC_SUFFIX,
PRERENDER_REVALIDATE_HEADER, PRERENDER_REVALIDATE_HEADER,
PRERENDER_REVALIDATE_ONLY_GENERATED_HEADER, PRERENDER_REVALIDATE_ONLY_GENERATED_HEADER,
...@@ -200,11 +199,6 @@ import { RenderingMode } from './rendering-mode' ...@@ -200,11 +199,6 @@ import { RenderingMode } from './rendering-mode'
import { InvariantError } from '../shared/lib/invariant-error' import { InvariantError } from '../shared/lib/invariant-error'
import { HTML_LIMITED_BOT_UA_RE_STRING } from '../shared/lib/router/utils/is-bot' import { HTML_LIMITED_BOT_UA_RE_STRING } from '../shared/lib/router/utils/is-bot'
import type { UseCacheTrackerKey } from './webpack/plugins/telemetry-plugin/use-cache-tracker-utils' import type { UseCacheTrackerKey } from './webpack/plugins/telemetry-plugin/use-cache-tracker-utils'
import {
buildInversePrefetchSegmentDataRoute,
buildPrefetchSegmentDataRoute,
type PrefetchSegmentDataRoute,
} from '../server/lib/router-utils/build-prefetch-segment-data-route'
import { turbopackBuild } from './turbopack-build' import { turbopackBuild } from './turbopack-build'
import { isFileSystemCacheEnabledForBuild } from '../shared/lib/turbopack/utils' import { isFileSystemCacheEnabledForBuild } from '../shared/lib/turbopack/utils'
...@@ -228,8 +222,12 @@ import { ...@@ -228,8 +222,12 @@ import {
writeValidatorFile, writeValidatorFile,
} from '../server/lib/router-utils/route-types-utils' } from '../server/lib/router-utils/route-types-utils'
import { Lockfile } from './lockfile' import { Lockfile } from './lockfile'
import { validateAppPaths } from './validate-app-paths' import {
buildPrefetchSegmentDataRoute,
type PrefetchSegmentDataRoute,
} from '../server/lib/router-utils/build-prefetch-segment-data-route'
import { generateRoutesManifest } from './generate-routes-manifest' import { generateRoutesManifest } from './generate-routes-manifest'
import { validateAppPaths } from './validate-app-paths'
type Fallback = null | boolean | string type Fallback = null | boolean | string
...@@ -456,7 +454,6 @@ export type RoutesManifest = { ...@@ -456,7 +454,6 @@ export type RoutesManifest = {
varyHeader: string varyHeader: string
prefetchHeader: typeof NEXT_ROUTER_PREFETCH_HEADER prefetchHeader: typeof NEXT_ROUTER_PREFETCH_HEADER
suffix: typeof RSC_SUFFIX suffix: typeof RSC_SUFFIX
prefetchSuffix: typeof RSC_PREFETCH_SUFFIX
prefetchSegmentHeader: typeof NEXT_ROUTER_SEGMENT_PREFETCH_HEADER prefetchSegmentHeader: typeof NEXT_ROUTER_SEGMENT_PREFETCH_HEADER
prefetchSegmentDirSuffix: typeof RSC_SEGMENTS_DIR_SUFFIX prefetchSegmentDirSuffix: typeof RSC_SEGMENTS_DIR_SUFFIX
prefetchSegmentSuffix: typeof RSC_SEGMENT_SUFFIX prefetchSegmentSuffix: typeof RSC_SEGMENT_SUFFIX
...@@ -3212,26 +3209,6 @@ export default async function build( ...@@ -3212,26 +3209,6 @@ export default async function build(
dataRoute = path.posix.join(`${normalizedRoute}${RSC_SUFFIX}`) dataRoute = path.posix.join(`${normalizedRoute}${RSC_SUFFIX}`)
} }
let prefetchDataRoute: string | null = null
// While we may only write the `.rsc` when the route does not
// have PPR enabled, we still want to generate the route when
// deployed so it doesn't 404. If the app has PPR enabled, we
// should add this key.
if (
!isAppRouteHandler &&
isAppPPREnabled &&
// Don't add a prefetch data route if we have
// cacheComponents enabled. This is
// because we don't actually use the prefetch data route in
// this case. This only applies if we have PPR enabled for
// this route.
!(config.cacheComponents && isRoutePPREnabled)
) {
prefetchDataRoute = path.posix.join(
`${normalizedRoute}${RSC_PREFETCH_SUFFIX}`
)
}
const meta = collectMeta(metadata) const meta = collectMeta(metadata)
const status = const status =
route.pathname === UNDERSCORE_NOT_FOUND_ROUTE route.pathname === UNDERSCORE_NOT_FOUND_ROUTE
...@@ -3252,7 +3229,7 @@ export default async function build( ...@@ -3252,7 +3229,7 @@ export default async function build(
initialExpireSeconds: cacheControl.expire, initialExpireSeconds: cacheControl.expire,
srcRoute: page, srcRoute: page,
dataRoute, dataRoute,
prefetchDataRoute, prefetchDataRoute: undefined,
allowHeader: ALLOWED_HEADERS, allowHeader: ALLOWED_HEADERS,
} }
} else { } else {
...@@ -3326,20 +3303,6 @@ export default async function build( ...@@ -3326,20 +3303,6 @@ export default async function build(
(r) => r.page === route.pathname (r) => r.page === route.pathname
) )
if (!isAppRouteHandler && isAppPPREnabled) { if (!isAppRouteHandler && isAppPPREnabled) {
if (
// Don't add a prefetch data route if we have
// cacheComponents enabled. This is
// because we don't actually use the prefetch data route in
// this case. This only applies if we have PPR enabled for
// this route.
!config.cacheComponents ||
!isRoutePPREnabled
) {
prefetchDataRoute = path.posix.join(
`${normalizedRoute}${RSC_PREFETCH_SUFFIX}`
)
}
// If the dynamic route wasn't found, then we need to create // If the dynamic route wasn't found, then we need to create
// it. This ensures that for each fallback shell there's an // it. This ensures that for each fallback shell there's an
// entry in the app routes manifest which enables routing for // entry in the app routes manifest which enables routing for
...@@ -3407,27 +3370,6 @@ export default async function build( ...@@ -3407,27 +3370,6 @@ export default async function build(
builtSegmentDataRoute builtSegmentDataRoute
) )
} }
// If the route has fallback root params, and we don't have
// any segment paths, we need to write the inverse prefetch
// segment data route so that it can first rewrite the /_tree
// request to the prefetch RSC route. We also need to set the
// `hasFallbackRootParams` flag so that we can simplify the
// route regex for matching.
else if (
route.fallbackRootParams &&
route.fallbackRootParams.length > 0
) {
dynamicRoute.hasFallbackRootParams = true
dynamicRoute.prefetchSegmentDataRoutes = [
buildInversePrefetchSegmentDataRoute(
dynamicRoute.page,
// We use the special segment path of `/_tree` because it's
// the first one sent by the client router so it's the only
// one we need to rewrite to the regular prefetch RSC route.
'/_tree'
),
]
}
} }
pageInfos.set(route.pathname, { pageInfos.set(route.pathname, {
......
...@@ -12,7 +12,6 @@ import { isDynamicUsageError } from '../helpers/is-dynamic-usage-error' ...@@ -12,7 +12,6 @@ import { isDynamicUsageError } from '../helpers/is-dynamic-usage-error'
import { import {
NEXT_CACHE_TAGS_HEADER, NEXT_CACHE_TAGS_HEADER,
NEXT_META_SUFFIX, NEXT_META_SUFFIX,
RSC_PREFETCH_SUFFIX,
RSC_SUFFIX, RSC_SUFFIX,
RSC_SEGMENTS_DIR_SUFFIX, RSC_SEGMENTS_DIR_SUFFIX,
RSC_SEGMENT_SUFFIX, RSC_SEGMENT_SUFFIX,
...@@ -140,20 +139,11 @@ export async function exportAppPage( ...@@ -140,20 +139,11 @@ export async function exportAppPage(
throw new Error(`Invariant: failed to get page data for ${path}`) throw new Error(`Invariant: failed to get page data for ${path}`)
} }
} else { } else {
// If PPR is enabled, we want to emit a prefetch rsc file for the page // If PPR is enabled, we want to emit a segment prefetch files
// instead of the standard rsc. This is because the standard rsc will // instead of the standard rsc. This is because the standard rsc will
// contain the dynamic data. We do this if any routes have PPR enabled so // contain the dynamic data. We do this if any routes have PPR enabled so
// that the cache read/write is the same. // that the cache read/write is the same.
if (renderOpts.experimental.isRoutePPREnabled) { if (!renderOpts.experimental.isRoutePPREnabled) {
// If PPR is enabled, we should emit the flight data as the prefetch
// payload.
// TODO: This will eventually be replaced by the per-segment prefetch
// output below.
fileWriter.append(
htmlFilepath.replace(/\.html$/, RSC_PREFETCH_SUFFIX),
flightData
)
} else {
// Writing the RSC payload to a file if we don't have PPR enabled. // Writing the RSC payload to a file if we don't have PPR enabled.
fileWriter.append( fileWriter.append(
htmlFilepath.replace(/\.html$/, RSC_SUFFIX), htmlFilepath.replace(/\.html$/, RSC_SUFFIX),
......
...@@ -11,7 +11,6 @@ export const PRERENDER_REVALIDATE_HEADER = 'x-prerender-revalidate' ...@@ -11,7 +11,6 @@ export const PRERENDER_REVALIDATE_HEADER = 'x-prerender-revalidate'
export const PRERENDER_REVALIDATE_ONLY_GENERATED_HEADER = export const PRERENDER_REVALIDATE_ONLY_GENERATED_HEADER =
'x-prerender-revalidate-if-generated' 'x-prerender-revalidate-if-generated'
export const RSC_PREFETCH_SUFFIX = '.prefetch.rsc'
export const RSC_SEGMENTS_DIR_SUFFIX = '.segments' export const RSC_SEGMENTS_DIR_SUFFIX = '.segments'
export const RSC_SEGMENT_SUFFIX = '.segment.rsc' export const RSC_SEGMENT_SUFFIX = '.segment.rsc'
export const RSC_SUFFIX = '.rsc' export const RSC_SUFFIX = '.rsc'
......
...@@ -121,7 +121,6 @@ import { ...@@ -121,7 +121,6 @@ import {
isAppPageRouteModule, isAppPageRouteModule,
isAppRouteRouteModule, isAppRouteRouteModule,
} from './route-modules/checks' } from './route-modules/checks'
import { PrefetchRSCPathnameNormalizer } from './normalizers/request/prefetch-rsc'
import { NextDataPathnameNormalizer } from './normalizers/request/next-data' import { NextDataPathnameNormalizer } from './normalizers/request/next-data'
import { getIsPossibleServerAction } from './lib/server-action-request-meta' import { getIsPossibleServerAction } from './lib/server-action-request-meta'
import { isInterceptionRouteAppPath } from '../shared/lib/router/utils/interception-routes' import { isInterceptionRouteAppPath } from '../shared/lib/router/utils/interception-routes'
...@@ -415,7 +414,6 @@ export default abstract class Server< ...@@ -415,7 +414,6 @@ export default abstract class Server<
protected readonly normalizers: { protected readonly normalizers: {
readonly rsc: RSCPathnameNormalizer | undefined readonly rsc: RSCPathnameNormalizer | undefined
readonly prefetchRSC: PrefetchRSCPathnameNormalizer | undefined
readonly segmentPrefetchRSC: SegmentPrefixRSCPathnameNormalizer | undefined readonly segmentPrefetchRSC: SegmentPrefixRSCPathnameNormalizer | undefined
readonly data: NextDataPathnameNormalizer | undefined readonly data: NextDataPathnameNormalizer | undefined
} }
...@@ -496,10 +494,6 @@ export default abstract class Server< ...@@ -496,10 +494,6 @@ export default abstract class Server<
this.enabledDirectories.app && this.minimalMode this.enabledDirectories.app && this.minimalMode
? new RSCPathnameNormalizer() ? new RSCPathnameNormalizer()
: undefined, : undefined,
prefetchRSC:
this.isAppPPREnabled && this.minimalMode
? new PrefetchRSCPathnameNormalizer()
: undefined,
segmentPrefetchRSC: this.minimalMode segmentPrefetchRSC: this.minimalMode
? new SegmentPrefixRSCPathnameNormalizer() ? new SegmentPrefixRSCPathnameNormalizer()
: undefined, : undefined,
...@@ -600,17 +594,6 @@ export default abstract class Server< ...@@ -600,17 +594,6 @@ export default abstract class Server<
addRequestMeta(req, 'isRSCRequest', true) addRequestMeta(req, 'isRSCRequest', true)
addRequestMeta(req, 'isPrefetchRSCRequest', true) addRequestMeta(req, 'isPrefetchRSCRequest', true)
addRequestMeta(req, 'segmentPrefetchRSCRequest', segmentPath) addRequestMeta(req, 'segmentPrefetchRSCRequest', segmentPath)
} else if (this.normalizers.prefetchRSC?.match(parsedUrl.pathname)) {
parsedUrl.pathname = this.normalizers.prefetchRSC.normalize(
parsedUrl.pathname,
true
)
// Mark the request as a router prefetch request.
req.headers[RSC_HEADER] = '1'
req.headers[NEXT_ROUTER_PREFETCH_HEADER] = '1'
addRequestMeta(req, 'isRSCRequest', true)
addRequestMeta(req, 'isPrefetchRSCRequest', true)
} else if (this.normalizers.rsc?.match(parsedUrl.pathname)) { } else if (this.normalizers.rsc?.match(parsedUrl.pathname)) {
parsedUrl.pathname = this.normalizers.rsc.normalize( parsedUrl.pathname = this.normalizers.rsc.normalize(
parsedUrl.pathname, parsedUrl.pathname,
...@@ -1584,12 +1567,6 @@ export default abstract class Server< ...@@ -1584,12 +1567,6 @@ export default abstract class Server<
normalizers.push(this.normalizers.segmentPrefetchRSC) normalizers.push(this.normalizers.segmentPrefetchRSC)
} }
// We have to put the prefetch normalizer before the RSC normalizer
// because the RSC normalizer will match the prefetch RSC routes too.
if (this.normalizers.prefetchRSC) {
normalizers.push(this.normalizers.prefetchRSC)
}
if (this.normalizers.rsc) { if (this.normalizers.rsc) {
normalizers.push(this.normalizers.rsc) normalizers.push(this.normalizers.rsc)
} }
...@@ -2324,7 +2301,6 @@ export default abstract class Server< ...@@ -2324,7 +2301,6 @@ export default abstract class Server<
for (const normalizer of [ for (const normalizer of [
this.normalizers.segmentPrefetchRSC, this.normalizers.segmentPrefetchRSC,
this.normalizers.prefetchRSC,
this.normalizers.rsc, this.normalizers.rsc,
]) { ]) {
if (normalizer?.match(initPathname)) { if (normalizer?.match(initPathname)) {
......
...@@ -17,7 +17,6 @@ import { ...@@ -17,7 +17,6 @@ import {
NEXT_CACHE_TAGS_HEADER, NEXT_CACHE_TAGS_HEADER,
NEXT_DATA_SUFFIX, NEXT_DATA_SUFFIX,
NEXT_META_SUFFIX, NEXT_META_SUFFIX,
RSC_PREFETCH_SUFFIX,
RSC_SEGMENT_SUFFIX, RSC_SEGMENT_SUFFIX,
RSC_SEGMENTS_DIR_SUFFIX, RSC_SEGMENTS_DIR_SUFFIX,
RSC_SUFFIX, RSC_SUFFIX,
...@@ -231,10 +230,10 @@ export default class FileSystemCache implements CacheHandler { ...@@ -231,10 +230,10 @@ export default class FileSystemCache implements CacheHandler {
} }
let rscData: Buffer | undefined let rscData: Buffer | undefined
if (!ctx.isFallback) { if (!ctx.isFallback && !ctx.isRoutePPREnabled) {
rscData = await this.fs.readFile( rscData = await this.fs.readFile(
this.getFilePath( this.getFilePath(
`${key}${ctx.isRoutePPREnabled ? RSC_PREFETCH_SUFFIX : RSC_SUFFIX}`, `${key}${RSC_SUFFIX}`,
IncrementalCacheKind.APP_PAGE IncrementalCacheKind.APP_PAGE
) )
) )
...@@ -396,16 +395,10 @@ export default class FileSystemCache implements CacheHandler { ...@@ -396,16 +395,10 @@ export default class FileSystemCache implements CacheHandler {
writer.append(htmlPath, data.html) writer.append(htmlPath, data.html)
// Fallbacks don't generate a data file. // Fallbacks don't generate a data file.
if (!ctx.fetchCache && !ctx.isFallback) { if (!ctx.fetchCache && !ctx.isFallback && !ctx.isRoutePPREnabled) {
writer.append( writer.append(
this.getFilePath( this.getFilePath(
`${key}${ `${key}${isAppPath ? RSC_SUFFIX : NEXT_DATA_SUFFIX}`,
isAppPath
? ctx.isRoutePPREnabled
? RSC_PREFETCH_SUFFIX
: RSC_SUFFIX
: NEXT_DATA_SUFFIX
}`,
isAppPath isAppPath
? IncrementalCacheKind.APP_PAGE ? IncrementalCacheKind.APP_PAGE
: IncrementalCacheKind.PAGES : IncrementalCacheKind.PAGES
......
import { import { buildPrefetchSegmentDataRoute } from './build-prefetch-segment-data-route'
buildInversePrefetchSegmentDataRoute,
buildPrefetchSegmentDataRoute,
} from './build-prefetch-segment-data-route'
describe('buildPrefetchSegmentDataRoute', () => { describe('buildPrefetchSegmentDataRoute', () => {
it('should build a prefetch segment data route', () => { it('should build a prefetch segment data route', () => {
...@@ -21,22 +18,3 @@ describe('buildPrefetchSegmentDataRoute', () => { ...@@ -21,22 +18,3 @@ describe('buildPrefetchSegmentDataRoute', () => {
`) `)
}) })
}) })
describe('buildInversePrefetchSegmentDataRoute', () => {
it('should build an inverted prefetch segment data route', () => {
const route = buildInversePrefetchSegmentDataRoute(
'/blog/[...slug]',
'/_tree'
)
expect(route).toMatchInlineSnapshot(`
{
"destination": "/blog/[...slug].prefetch.rsc",
"routeKeys": {
"nxtPslug": "nxtPslug",
},
"source": "^/blog/(?<nxtPslug>.+?)\\.segments/_tree\\.segment\\.rsc$",
}
`)
})
})
...@@ -2,7 +2,6 @@ import path from '../../../shared/lib/isomorphic/path' ...@@ -2,7 +2,6 @@ import path from '../../../shared/lib/isomorphic/path'
import { normalizePagePath } from '../../../shared/lib/page-path/normalize-page-path' import { normalizePagePath } from '../../../shared/lib/page-path/normalize-page-path'
import { getNamedRouteRegex } from '../../../shared/lib/router/utils/route-regex' import { getNamedRouteRegex } from '../../../shared/lib/router/utils/route-regex'
import { import {
RSC_PREFETCH_SUFFIX,
RSC_SEGMENT_SUFFIX, RSC_SEGMENT_SUFFIX,
RSC_SEGMENTS_DIR_SUFFIX, RSC_SEGMENTS_DIR_SUFFIX,
} from '../../../lib/constants' } from '../../../lib/constants'
...@@ -40,40 +39,3 @@ export function buildPrefetchSegmentDataRoute( ...@@ -40,40 +39,3 @@ export function buildPrefetchSegmentDataRoute(
routeKeys, routeKeys,
} }
} }
/**
* Builds a prefetch segment data route that is inverted. This means that it's
* supposed to rewrite from the previous segment paths route back to the
* prefetch RSC route.
*
* @param page - The page to build the route for.
* @param segmentPath - The segment path to build the route for.
* @returns The prefetch segment data route.
*/
export function buildInversePrefetchSegmentDataRoute(
page: string,
segmentPath: string
): PrefetchSegmentDataRoute {
const pagePath = normalizePagePath(page)
const source = path.posix.join(
`${pagePath}${RSC_SEGMENTS_DIR_SUFFIX}`,
`${segmentPath}${RSC_SEGMENT_SUFFIX}`
)
const { namedRegex, routeKeys } = getNamedRouteRegex(source, {
prefixRouteKeys: true,
includePrefix: true,
includeSuffix: true,
excludeOptionalTrailingSlash: true,
backreferenceDuplicateKeys: true,
})
const destination = path.posix.join(`${pagePath}${RSC_PREFETCH_SUFFIX}`)
return {
source: namedRegex,
destination,
routeKeys,
}
}
...@@ -43,7 +43,6 @@ import { ...@@ -43,7 +43,6 @@ import {
import { normalizePathSep } from '../../../shared/lib/page-path/normalize-path-sep' import { normalizePathSep } from '../../../shared/lib/page-path/normalize-path-sep'
import { normalizeMetadataRoute } from '../../../lib/metadata/get-metadata-route' import { normalizeMetadataRoute } from '../../../lib/metadata/get-metadata-route'
import { RSCPathnameNormalizer } from '../../normalizers/request/rsc' import { RSCPathnameNormalizer } from '../../normalizers/request/rsc'
import { PrefetchRSCPathnameNormalizer } from '../../normalizers/request/prefetch-rsc'
import { encodeURIPath } from '../../../shared/lib/encode-uri-path' import { encodeURIPath } from '../../../shared/lib/encode-uri-path'
import { isMetadataRouteFile } from '../../../lib/metadata/is-metadata-route' import { isMetadataRouteFile } from '../../../lib/metadata/is-metadata-route'
...@@ -425,9 +424,6 @@ export async function setupFsCheck(opts: { ...@@ -425,9 +424,6 @@ export async function setupFsCheck(opts: {
// Because we can't know if the app directory is enabled or not at this // Because we can't know if the app directory is enabled or not at this
// stage, we assume that it is. // stage, we assume that it is.
rsc: new RSCPathnameNormalizer(), rsc: new RSCPathnameNormalizer(),
prefetchRSC: opts.config.experimental.ppr
? new PrefetchRSCPathnameNormalizer()
: undefined,
} }
return { return {
...@@ -483,9 +479,7 @@ export async function setupFsCheck(opts: { ...@@ -483,9 +479,7 @@ export async function setupFsCheck(opts: {
// Simulate minimal mode requests by normalizing RSC and postponed // Simulate minimal mode requests by normalizing RSC and postponed
// requests. // requests.
if (opts.minimalMode) { if (opts.minimalMode) {
if (normalizers.prefetchRSC?.match(itemPath)) { if (normalizers.rsc.match(itemPath)) {
itemPath = normalizers.prefetchRSC.normalize(itemPath, true)
} else if (normalizers.rsc.match(itemPath)) {
itemPath = normalizers.rsc.normalize(itemPath, true) itemPath = normalizers.rsc.normalize(itemPath, true)
} }
} }
......
import { PrefetchRSCPathnameNormalizer } from './prefetch-rsc'
describe('PrefetchRSCPathnameNormalizer', () => {
const normalizer = new PrefetchRSCPathnameNormalizer()
it('should match the prefetch rsc pathname', () => {
expect(normalizer.match('/blog/post.prefetch.rsc')).toBe(true)
})
it('should not match the prefetch rsc pathname with a different suffix', () => {
expect(normalizer.match('/blog/post.prefetch.rsc2')).toBe(false)
})
it('should normalize the prefetch rsc pathname', () => {
expect(normalizer.normalize('/blog/post.prefetch.rsc')).toBe('/blog/post')
})
it('should normalize the prefetch rsc index pathname', () => {
expect(normalizer.normalize('/__index.prefetch.rsc')).toBe('/')
})
})
import type { PathnameNormalizer } from './pathname-normalizer'
import { RSC_PREFETCH_SUFFIX } from '../../../lib/constants'
import { SuffixPathnameNormalizer } from './suffix'
export class PrefetchRSCPathnameNormalizer
extends SuffixPathnameNormalizer
implements PathnameNormalizer
{
constructor() {
super(RSC_PREFETCH_SUFFIX)
}
public match(pathname: string): boolean {
if (pathname === '/__index' + RSC_PREFETCH_SUFFIX) {
return true
}
return super.match(pathname)
}
public normalize(pathname: string, matched?: boolean): string {
if (pathname === '/__index' + RSC_PREFETCH_SUFFIX) {
return '/'
}
return super.normalize(pathname, matched)
}
}
...@@ -16,9 +16,9 @@ describe('app dir - not-found - default', () => { ...@@ -16,9 +16,9 @@ describe('app dir - not-found - default', () => {
if (isNextStart) { if (isNextStart) {
it('should contain noindex contain in the page', async () => { it('should contain noindex contain in the page', async () => {
const html = await next.readFile('.next/server/app/_not-found.html') const html = await next.readFile('.next/server/app/_not-found.html')
const rsc = await next.readFile( const rsc = isPPREnabled
`.next/server/app/_not-found.${isPPREnabled ? 'prefetch.' : ''}rsc` ? 'noindex'
) : await next.readFile(`.next/server/app/_not-found.rsc`)
expect(html).toContain('noindex') expect(html).toContain('noindex')
expect(rsc).toContain('noindex') expect(rsc).toContain('noindex')
......
...@@ -411,6 +411,11 @@ const cases: { ...@@ -411,6 +411,11 @@ const cases: {
headers: { headers: {
rsc: '1', rsc: '1',
'next-router-prefetch': '1', 'next-router-prefetch': '1',
...(process.env.__NEXT_CACHE_COMPONENTS === 'true'
? {
'next-router-segment-prefetch': '/_tree',
}
: {}),
}, },
expected: { expected: {
'x-nextjs-rewritten-path': '/other', 'x-nextjs-rewritten-path': '/other',
......
...@@ -2437,7 +2437,6 @@ const runTests = (isDev = false) => { ...@@ -2437,7 +2437,6 @@ const runTests = (isDev = false) => {
"prefetchSegmentDirSuffix": ".segments", "prefetchSegmentDirSuffix": ".segments",
"prefetchSegmentHeader": "next-router-segment-prefetch", "prefetchSegmentHeader": "next-router-segment-prefetch",
"prefetchSegmentSuffix": ".segment.rsc", "prefetchSegmentSuffix": ".segment.rsc",
"prefetchSuffix": ".prefetch.rsc",
"suffix": ".rsc", "suffix": ".rsc",
"varyHeader": "rsc, next-router-state-tree, next-router-prefetch, next-router-segment-prefetch", "varyHeader": "rsc, next-router-state-tree, next-router-prefetch, next-router-segment-prefetch",
}, },
......
...@@ -1485,7 +1485,6 @@ function runTests({ dev }) { ...@@ -1485,7 +1485,6 @@ function runTests({ dev }) {
"prefetchSegmentDirSuffix": ".segments", "prefetchSegmentDirSuffix": ".segments",
"prefetchSegmentHeader": "next-router-segment-prefetch", "prefetchSegmentHeader": "next-router-segment-prefetch",
"prefetchSegmentSuffix": ".segment.rsc", "prefetchSegmentSuffix": ".segment.rsc",
"prefetchSuffix": ".prefetch.rsc",
"suffix": ".rsc", "suffix": ".rsc",
"varyHeader": "rsc, next-router-state-tree, next-router-prefetch, next-router-segment-prefetch", "varyHeader": "rsc, next-router-state-tree, next-router-prefetch, next-router-segment-prefetch",
}, },
......
...@@ -6,19 +6,25 @@ describe('app-dir parallel-routes-static', () => { ...@@ -6,19 +6,25 @@ describe('app-dir parallel-routes-static', () => {
}) })
it('should static generate parallel routes', async () => { it('should static generate parallel routes', async () => {
const rscExtension = process.env.__NEXT_CACHE_COMPONENTS const rscExtension = '.rsc'
? '.prefetch.rsc'
: '.rsc'
expect(await next.hasFile('.next/server/app/nested/foo.html')).toBe(true) expect(await next.hasFile('.next/server/app/nested/foo.html')).toBe(true)
expect(await next.hasFile('.next/server/app/nested/foo.meta')).toBe(true) expect(await next.hasFile('.next/server/app/nested/foo.meta')).toBe(true)
expect(
await next.hasFile(`.next/server/app/nested/foo${rscExtension}`) // we do not output '.rsc' statically with cache components
).toBe(true) if (!process.env.__NEXT_CACHE_COMPONENTS) {
expect(
await next.hasFile(`.next/server/app/nested/foo${rscExtension}`)
).toBe(true)
}
expect(await next.hasFile('.next/server/app/nested/bar.html')).toBe(true) expect(await next.hasFile('.next/server/app/nested/bar.html')).toBe(true)
expect(await next.hasFile('.next/server/app/nested/bar.meta')).toBe(true) expect(await next.hasFile('.next/server/app/nested/bar.meta')).toBe(true)
expect(
await next.hasFile(`.next/server/app/nested/bar${rscExtension}`) // we do not output '.rsc' statically with cache components
).toBe(true) if (!process.env.__NEXT_CACHE_COMPONENTS) {
expect(
await next.hasFile(`.next/server/app/nested/bar${rscExtension}`)
).toBe(true)
}
}) })
}) })
...@@ -51,10 +51,7 @@ module.exports = (res) => ({ ...@@ -51,10 +51,7 @@ module.exports = (res) => ({
} }
// If this is for a prefetch or segment request, then mark it as a miss. // If this is for a prefetch or segment request, then mark it as a miss.
if ( if (meta.url.endsWith('.segment.rsc')) {
meta.url.endsWith('.prefetch.rsc') ||
meta.url.endsWith('.segment.rsc')
) {
res.setHeader('x-nextjs-cache-entry-handler', 'MISS_2') res.setHeader('x-nextjs-cache-entry-handler', 'MISS_2')
return false return false
} }
......
...@@ -447,30 +447,6 @@ describe.skip('required server files app router', () => { ...@@ -447,30 +447,6 @@ describe.skip('required server files app router', () => {
expect(res.headers.has('x-nextjs-postponed')).toBeFalse() expect(res.headers.has('x-nextjs-postponed')).toBeFalse()
}) })
it('should handle prefetch RSC requests', async () => {
const res = await fetchViaHTTP(
appPort,
'/dyn/first.prefetch.rsc',
undefined,
{
headers: {
'x-matched-path': '/dyn/[slug]',
'x-now-route-matches': createNowRouteMatches({
slug: 'first',
}).toString(),
},
}
)
expect(res.status).toBe(200)
expect(res.headers.get('content-type')).toEqual('text/x-component')
expect(res.headers.has('x-nextjs-postponed')).toBeTrue()
// We expect that because we're performing a prefetch, we should be a miss
// because it isn't handled by the cache handler.
expect(res.headers.get('x-nextjs-cache-entry-handler')).toBe('MISS_2')
})
it('should use the postponed state for the RSC requests', async () => { it('should use the postponed state for the RSC requests', async () => {
// Let's parse the random number out of the HTML that was generated at build // Let's parse the random number out of the HTML that was generated at build
// time. We want to use that value as it's the one that's tied to the // time. We want to use that value as it's the one that's tied to the
......
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