Skip to content

Commit 40dd4ce

Browse files
authored
Delete un-used prefetch outputs with PPR (#86100)
1 parent 36f50c5 commit 40dd4ce

File tree

19 files changed

+38
-334
lines changed

19 files changed

+38
-334
lines changed

packages/next/src/build/generate-routes-manifest.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
} from '../client/components/app-router-headers'
1313
import {
1414
RSC_SUFFIX,
15-
RSC_PREFETCH_SUFFIX,
1615
RSC_SEGMENT_SUFFIX,
1716
RSC_SEGMENTS_DIR_SUFFIX,
1817
NEXT_RESUME_HEADER,
@@ -123,7 +122,6 @@ export function generateRoutesManifest(
123122
didPostponeHeader: NEXT_DID_POSTPONE_HEADER,
124123
contentTypeHeader: RSC_CONTENT_TYPE_HEADER,
125124
suffix: RSC_SUFFIX,
126-
prefetchSuffix: RSC_PREFETCH_SUFFIX,
127125
prefetchSegmentHeader: NEXT_ROUTER_SEGMENT_PREFETCH_HEADER,
128126
prefetchSegmentSuffix: RSC_SEGMENT_SUFFIX,
129127
prefetchSegmentDirSuffix: RSC_SEGMENTS_DIR_SUFFIX,

packages/next/src/build/index.ts

Lines changed: 6 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import {
2424
PROXY_FILENAME,
2525
PAGES_DIR_ALIAS,
2626
INSTRUMENTATION_HOOK_FILENAME,
27-
RSC_PREFETCH_SUFFIX,
2827
RSC_SUFFIX,
2928
PRERENDER_REVALIDATE_HEADER,
3029
PRERENDER_REVALIDATE_ONLY_GENERATED_HEADER,
@@ -200,11 +199,6 @@ import { RenderingMode } from './rendering-mode'
200199
import { InvariantError } from '../shared/lib/invariant-error'
201200
import { HTML_LIMITED_BOT_UA_RE_STRING } from '../shared/lib/router/utils/is-bot'
202201
import type { UseCacheTrackerKey } from './webpack/plugins/telemetry-plugin/use-cache-tracker-utils'
203-
import {
204-
buildInversePrefetchSegmentDataRoute,
205-
buildPrefetchSegmentDataRoute,
206-
type PrefetchSegmentDataRoute,
207-
} from '../server/lib/router-utils/build-prefetch-segment-data-route'
208202

209203
import { turbopackBuild } from './turbopack-build'
210204
import { isFileSystemCacheEnabledForBuild } from '../shared/lib/turbopack/utils'
@@ -228,8 +222,12 @@ import {
228222
writeValidatorFile,
229223
} from '../server/lib/router-utils/route-types-utils'
230224
import { Lockfile } from './lockfile'
231-
import { validateAppPaths } from './validate-app-paths'
225+
import {
226+
buildPrefetchSegmentDataRoute,
227+
type PrefetchSegmentDataRoute,
228+
} from '../server/lib/router-utils/build-prefetch-segment-data-route'
232229
import { generateRoutesManifest } from './generate-routes-manifest'
230+
import { validateAppPaths } from './validate-app-paths'
233231

234232
type Fallback = null | boolean | string
235233

@@ -456,7 +454,6 @@ export type RoutesManifest = {
456454
varyHeader: string
457455
prefetchHeader: typeof NEXT_ROUTER_PREFETCH_HEADER
458456
suffix: typeof RSC_SUFFIX
459-
prefetchSuffix: typeof RSC_PREFETCH_SUFFIX
460457
prefetchSegmentHeader: typeof NEXT_ROUTER_SEGMENT_PREFETCH_HEADER
461458
prefetchSegmentDirSuffix: typeof RSC_SEGMENTS_DIR_SUFFIX
462459
prefetchSegmentSuffix: typeof RSC_SEGMENT_SUFFIX
@@ -3212,26 +3209,6 @@ export default async function build(
32123209
dataRoute = path.posix.join(`${normalizedRoute}${RSC_SUFFIX}`)
32133210
}
32143211

3215-
let prefetchDataRoute: string | null = null
3216-
// While we may only write the `.rsc` when the route does not
3217-
// have PPR enabled, we still want to generate the route when
3218-
// deployed so it doesn't 404. If the app has PPR enabled, we
3219-
// should add this key.
3220-
if (
3221-
!isAppRouteHandler &&
3222-
isAppPPREnabled &&
3223-
// Don't add a prefetch data route if we have
3224-
// cacheComponents enabled. This is
3225-
// because we don't actually use the prefetch data route in
3226-
// this case. This only applies if we have PPR enabled for
3227-
// this route.
3228-
!(config.cacheComponents && isRoutePPREnabled)
3229-
) {
3230-
prefetchDataRoute = path.posix.join(
3231-
`${normalizedRoute}${RSC_PREFETCH_SUFFIX}`
3232-
)
3233-
}
3234-
32353212
const meta = collectMeta(metadata)
32363213
const status =
32373214
route.pathname === UNDERSCORE_NOT_FOUND_ROUTE
@@ -3252,7 +3229,7 @@ export default async function build(
32523229
initialExpireSeconds: cacheControl.expire,
32533230
srcRoute: page,
32543231
dataRoute,
3255-
prefetchDataRoute,
3232+
prefetchDataRoute: undefined,
32563233
allowHeader: ALLOWED_HEADERS,
32573234
}
32583235
} else {
@@ -3326,20 +3303,6 @@ export default async function build(
33263303
(r) => r.page === route.pathname
33273304
)
33283305
if (!isAppRouteHandler && isAppPPREnabled) {
3329-
if (
3330-
// Don't add a prefetch data route if we have
3331-
// cacheComponents enabled. This is
3332-
// because we don't actually use the prefetch data route in
3333-
// this case. This only applies if we have PPR enabled for
3334-
// this route.
3335-
!config.cacheComponents ||
3336-
!isRoutePPREnabled
3337-
) {
3338-
prefetchDataRoute = path.posix.join(
3339-
`${normalizedRoute}${RSC_PREFETCH_SUFFIX}`
3340-
)
3341-
}
3342-
33433306
// If the dynamic route wasn't found, then we need to create
33443307
// it. This ensures that for each fallback shell there's an
33453308
// entry in the app routes manifest which enables routing for
@@ -3407,27 +3370,6 @@ export default async function build(
34073370
builtSegmentDataRoute
34083371
)
34093372
}
3410-
// If the route has fallback root params, and we don't have
3411-
// any segment paths, we need to write the inverse prefetch
3412-
// segment data route so that it can first rewrite the /_tree
3413-
// request to the prefetch RSC route. We also need to set the
3414-
// `hasFallbackRootParams` flag so that we can simplify the
3415-
// route regex for matching.
3416-
else if (
3417-
route.fallbackRootParams &&
3418-
route.fallbackRootParams.length > 0
3419-
) {
3420-
dynamicRoute.hasFallbackRootParams = true
3421-
dynamicRoute.prefetchSegmentDataRoutes = [
3422-
buildInversePrefetchSegmentDataRoute(
3423-
dynamicRoute.page,
3424-
// We use the special segment path of `/_tree` because it's
3425-
// the first one sent by the client router so it's the only
3426-
// one we need to rewrite to the regular prefetch RSC route.
3427-
'/_tree'
3428-
),
3429-
]
3430-
}
34313373
}
34323374

34333375
pageInfos.set(route.pathname, {

packages/next/src/export/routes/app-page.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import { isDynamicUsageError } from '../helpers/is-dynamic-usage-error'
1212
import {
1313
NEXT_CACHE_TAGS_HEADER,
1414
NEXT_META_SUFFIX,
15-
RSC_PREFETCH_SUFFIX,
1615
RSC_SUFFIX,
1716
RSC_SEGMENTS_DIR_SUFFIX,
1817
RSC_SEGMENT_SUFFIX,
@@ -140,20 +139,11 @@ export async function exportAppPage(
140139
throw new Error(`Invariant: failed to get page data for ${path}`)
141140
}
142141
} else {
143-
// If PPR is enabled, we want to emit a prefetch rsc file for the page
142+
// If PPR is enabled, we want to emit a segment prefetch files
144143
// instead of the standard rsc. This is because the standard rsc will
145144
// contain the dynamic data. We do this if any routes have PPR enabled so
146145
// that the cache read/write is the same.
147-
if (renderOpts.experimental.isRoutePPREnabled) {
148-
// If PPR is enabled, we should emit the flight data as the prefetch
149-
// payload.
150-
// TODO: This will eventually be replaced by the per-segment prefetch
151-
// output below.
152-
fileWriter.append(
153-
htmlFilepath.replace(/\.html$/, RSC_PREFETCH_SUFFIX),
154-
flightData
155-
)
156-
} else {
146+
if (!renderOpts.experimental.isRoutePPREnabled) {
157147
// Writing the RSC payload to a file if we don't have PPR enabled.
158148
fileWriter.append(
159149
htmlFilepath.replace(/\.html$/, RSC_SUFFIX),

packages/next/src/lib/constants.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export const PRERENDER_REVALIDATE_HEADER = 'x-prerender-revalidate'
1111
export const PRERENDER_REVALIDATE_ONLY_GENERATED_HEADER =
1212
'x-prerender-revalidate-if-generated'
1313

14-
export const RSC_PREFETCH_SUFFIX = '.prefetch.rsc'
1514
export const RSC_SEGMENTS_DIR_SUFFIX = '.segments'
1615
export const RSC_SEGMENT_SUFFIX = '.segment.rsc'
1716
export const RSC_SUFFIX = '.rsc'

packages/next/src/server/base-server.ts

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ import {
121121
isAppPageRouteModule,
122122
isAppRouteRouteModule,
123123
} from './route-modules/checks'
124-
import { PrefetchRSCPathnameNormalizer } from './normalizers/request/prefetch-rsc'
125124
import { NextDataPathnameNormalizer } from './normalizers/request/next-data'
126125
import { getIsPossibleServerAction } from './lib/server-action-request-meta'
127126
import { isInterceptionRouteAppPath } from '../shared/lib/router/utils/interception-routes'
@@ -415,7 +414,6 @@ export default abstract class Server<
415414

416415
protected readonly normalizers: {
417416
readonly rsc: RSCPathnameNormalizer | undefined
418-
readonly prefetchRSC: PrefetchRSCPathnameNormalizer | undefined
419417
readonly segmentPrefetchRSC: SegmentPrefixRSCPathnameNormalizer | undefined
420418
readonly data: NextDataPathnameNormalizer | undefined
421419
}
@@ -496,10 +494,6 @@ export default abstract class Server<
496494
this.enabledDirectories.app && this.minimalMode
497495
? new RSCPathnameNormalizer()
498496
: undefined,
499-
prefetchRSC:
500-
this.isAppPPREnabled && this.minimalMode
501-
? new PrefetchRSCPathnameNormalizer()
502-
: undefined,
503497
segmentPrefetchRSC: this.minimalMode
504498
? new SegmentPrefixRSCPathnameNormalizer()
505499
: undefined,
@@ -600,17 +594,6 @@ export default abstract class Server<
600594
addRequestMeta(req, 'isRSCRequest', true)
601595
addRequestMeta(req, 'isPrefetchRSCRequest', true)
602596
addRequestMeta(req, 'segmentPrefetchRSCRequest', segmentPath)
603-
} else if (this.normalizers.prefetchRSC?.match(parsedUrl.pathname)) {
604-
parsedUrl.pathname = this.normalizers.prefetchRSC.normalize(
605-
parsedUrl.pathname,
606-
true
607-
)
608-
609-
// Mark the request as a router prefetch request.
610-
req.headers[RSC_HEADER] = '1'
611-
req.headers[NEXT_ROUTER_PREFETCH_HEADER] = '1'
612-
addRequestMeta(req, 'isRSCRequest', true)
613-
addRequestMeta(req, 'isPrefetchRSCRequest', true)
614597
} else if (this.normalizers.rsc?.match(parsedUrl.pathname)) {
615598
parsedUrl.pathname = this.normalizers.rsc.normalize(
616599
parsedUrl.pathname,
@@ -1584,12 +1567,6 @@ export default abstract class Server<
15841567
normalizers.push(this.normalizers.segmentPrefetchRSC)
15851568
}
15861569

1587-
// We have to put the prefetch normalizer before the RSC normalizer
1588-
// because the RSC normalizer will match the prefetch RSC routes too.
1589-
if (this.normalizers.prefetchRSC) {
1590-
normalizers.push(this.normalizers.prefetchRSC)
1591-
}
1592-
15931570
if (this.normalizers.rsc) {
15941571
normalizers.push(this.normalizers.rsc)
15951572
}
@@ -2324,7 +2301,6 @@ export default abstract class Server<
23242301

23252302
for (const normalizer of [
23262303
this.normalizers.segmentPrefetchRSC,
2327-
this.normalizers.prefetchRSC,
23282304
this.normalizers.rsc,
23292305
]) {
23302306
if (normalizer?.match(initPathname)) {

packages/next/src/server/lib/incremental-cache/file-system-cache.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {
1717
NEXT_CACHE_TAGS_HEADER,
1818
NEXT_DATA_SUFFIX,
1919
NEXT_META_SUFFIX,
20-
RSC_PREFETCH_SUFFIX,
2120
RSC_SEGMENT_SUFFIX,
2221
RSC_SEGMENTS_DIR_SUFFIX,
2322
RSC_SUFFIX,
@@ -231,10 +230,10 @@ export default class FileSystemCache implements CacheHandler {
231230
}
232231

233232
let rscData: Buffer | undefined
234-
if (!ctx.isFallback) {
233+
if (!ctx.isFallback && !ctx.isRoutePPREnabled) {
235234
rscData = await this.fs.readFile(
236235
this.getFilePath(
237-
`${key}${ctx.isRoutePPREnabled ? RSC_PREFETCH_SUFFIX : RSC_SUFFIX}`,
236+
`${key}${RSC_SUFFIX}`,
238237
IncrementalCacheKind.APP_PAGE
239238
)
240239
)
@@ -396,16 +395,10 @@ export default class FileSystemCache implements CacheHandler {
396395
writer.append(htmlPath, data.html)
397396

398397
// Fallbacks don't generate a data file.
399-
if (!ctx.fetchCache && !ctx.isFallback) {
398+
if (!ctx.fetchCache && !ctx.isFallback && !ctx.isRoutePPREnabled) {
400399
writer.append(
401400
this.getFilePath(
402-
`${key}${
403-
isAppPath
404-
? ctx.isRoutePPREnabled
405-
? RSC_PREFETCH_SUFFIX
406-
: RSC_SUFFIX
407-
: NEXT_DATA_SUFFIX
408-
}`,
401+
`${key}${isAppPath ? RSC_SUFFIX : NEXT_DATA_SUFFIX}`,
409402
isAppPath
410403
? IncrementalCacheKind.APP_PAGE
411404
: IncrementalCacheKind.PAGES
Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import {
2-
buildInversePrefetchSegmentDataRoute,
3-
buildPrefetchSegmentDataRoute,
4-
} from './build-prefetch-segment-data-route'
1+
import { buildPrefetchSegmentDataRoute } from './build-prefetch-segment-data-route'
52

63
describe('buildPrefetchSegmentDataRoute', () => {
74
it('should build a prefetch segment data route', () => {
@@ -21,22 +18,3 @@ describe('buildPrefetchSegmentDataRoute', () => {
2118
`)
2219
})
2320
})
24-
25-
describe('buildInversePrefetchSegmentDataRoute', () => {
26-
it('should build an inverted prefetch segment data route', () => {
27-
const route = buildInversePrefetchSegmentDataRoute(
28-
'/blog/[...slug]',
29-
'/_tree'
30-
)
31-
32-
expect(route).toMatchInlineSnapshot(`
33-
{
34-
"destination": "/blog/[...slug].prefetch.rsc",
35-
"routeKeys": {
36-
"nxtPslug": "nxtPslug",
37-
},
38-
"source": "^/blog/(?<nxtPslug>.+?)\\.segments/_tree\\.segment\\.rsc$",
39-
}
40-
`)
41-
})
42-
})

packages/next/src/server/lib/router-utils/build-prefetch-segment-data-route.ts

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import path from '../../../shared/lib/isomorphic/path'
22
import { normalizePagePath } from '../../../shared/lib/page-path/normalize-page-path'
33
import { getNamedRouteRegex } from '../../../shared/lib/router/utils/route-regex'
44
import {
5-
RSC_PREFETCH_SUFFIX,
65
RSC_SEGMENT_SUFFIX,
76
RSC_SEGMENTS_DIR_SUFFIX,
87
} from '../../../lib/constants'
@@ -40,40 +39,3 @@ export function buildPrefetchSegmentDataRoute(
4039
routeKeys,
4140
}
4241
}
43-
44-
/**
45-
* Builds a prefetch segment data route that is inverted. This means that it's
46-
* supposed to rewrite from the previous segment paths route back to the
47-
* prefetch RSC route.
48-
*
49-
* @param page - The page to build the route for.
50-
* @param segmentPath - The segment path to build the route for.
51-
* @returns The prefetch segment data route.
52-
*/
53-
export function buildInversePrefetchSegmentDataRoute(
54-
page: string,
55-
segmentPath: string
56-
): PrefetchSegmentDataRoute {
57-
const pagePath = normalizePagePath(page)
58-
59-
const source = path.posix.join(
60-
`${pagePath}${RSC_SEGMENTS_DIR_SUFFIX}`,
61-
`${segmentPath}${RSC_SEGMENT_SUFFIX}`
62-
)
63-
64-
const { namedRegex, routeKeys } = getNamedRouteRegex(source, {
65-
prefixRouteKeys: true,
66-
includePrefix: true,
67-
includeSuffix: true,
68-
excludeOptionalTrailingSlash: true,
69-
backreferenceDuplicateKeys: true,
70-
})
71-
72-
const destination = path.posix.join(`${pagePath}${RSC_PREFETCH_SUFFIX}`)
73-
74-
return {
75-
source: namedRegex,
76-
destination,
77-
routeKeys,
78-
}
79-
}

packages/next/src/server/lib/router-utils/filesystem.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ import {
4343
import { normalizePathSep } from '../../../shared/lib/page-path/normalize-path-sep'
4444
import { normalizeMetadataRoute } from '../../../lib/metadata/get-metadata-route'
4545
import { RSCPathnameNormalizer } from '../../normalizers/request/rsc'
46-
import { PrefetchRSCPathnameNormalizer } from '../../normalizers/request/prefetch-rsc'
4746
import { encodeURIPath } from '../../../shared/lib/encode-uri-path'
4847
import { isMetadataRouteFile } from '../../../lib/metadata/is-metadata-route'
4948

@@ -425,9 +424,6 @@ export async function setupFsCheck(opts: {
425424
// Because we can't know if the app directory is enabled or not at this
426425
// stage, we assume that it is.
427426
rsc: new RSCPathnameNormalizer(),
428-
prefetchRSC: opts.config.experimental.ppr
429-
? new PrefetchRSCPathnameNormalizer()
430-
: undefined,
431427
}
432428

433429
return {
@@ -483,9 +479,7 @@ export async function setupFsCheck(opts: {
483479
// Simulate minimal mode requests by normalizing RSC and postponed
484480
// requests.
485481
if (opts.minimalMode) {
486-
if (normalizers.prefetchRSC?.match(itemPath)) {
487-
itemPath = normalizers.prefetchRSC.normalize(itemPath, true)
488-
} else if (normalizers.rsc.match(itemPath)) {
482+
if (normalizers.rsc.match(itemPath)) {
489483
itemPath = normalizers.rsc.normalize(itemPath, true)
490484
}
491485
}

0 commit comments

Comments
 (0)