Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 5 additions & 64 deletions packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
PROXY_FILENAME,
PAGES_DIR_ALIAS,
INSTRUMENTATION_HOOK_FILENAME,
RSC_PREFETCH_SUFFIX,
RSC_SUFFIX,
NEXT_RESUME_HEADER,
PRERENDER_REVALIDATE_HEADER,
Expand Down Expand Up @@ -202,11 +201,6 @@ import { RenderingMode } from './rendering-mode'
import { InvariantError } from '../shared/lib/invariant-error'
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 {
buildInversePrefetchSegmentDataRoute,
buildPrefetchSegmentDataRoute,
type PrefetchSegmentDataRoute,
} from '../server/lib/router-utils/build-prefetch-segment-data-route'

import { turbopackBuild } from './turbopack-build'
import { isFileSystemCacheEnabledForBuild } from '../shared/lib/turbopack/utils'
Expand All @@ -230,6 +224,10 @@ import {
writeValidatorFile,
} from '../server/lib/router-utils/route-types-utils'
import { Lockfile } from './lockfile'
import {
buildPrefetchSegmentDataRoute,
type PrefetchSegmentDataRoute,
} from '../server/lib/router-utils/build-prefetch-segment-data-route'

type Fallback = null | boolean | string

Expand Down Expand Up @@ -465,7 +463,6 @@ export type RoutesManifest = {
varyHeader: string
prefetchHeader: typeof NEXT_ROUTER_PREFETCH_HEADER
suffix: typeof RSC_SUFFIX
prefetchSuffix: typeof RSC_PREFETCH_SUFFIX
prefetchSegmentHeader: typeof NEXT_ROUTER_SEGMENT_PREFETCH_HEADER
prefetchSegmentDirSuffix: typeof RSC_SEGMENTS_DIR_SUFFIX
prefetchSegmentSuffix: typeof RSC_SEGMENT_SUFFIX
Expand Down Expand Up @@ -1665,7 +1662,6 @@ export default async function build(
didPostponeHeader: NEXT_DID_POSTPONE_HEADER,
contentTypeHeader: RSC_CONTENT_TYPE_HEADER,
suffix: RSC_SUFFIX,
prefetchSuffix: RSC_PREFETCH_SUFFIX,
prefetchSegmentHeader: NEXT_ROUTER_SEGMENT_PREFETCH_HEADER,
prefetchSegmentSuffix: RSC_SEGMENT_SUFFIX,
prefetchSegmentDirSuffix: RSC_SEGMENTS_DIR_SUFFIX,
Expand Down Expand Up @@ -3328,26 +3324,6 @@ export default async function build(
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 status =
route.pathname === UNDERSCORE_NOT_FOUND_ROUTE
Expand All @@ -3368,7 +3344,7 @@ export default async function build(
initialExpireSeconds: cacheControl.expire,
srcRoute: page,
dataRoute,
prefetchDataRoute,
prefetchDataRoute: undefined,
allowHeader: ALLOWED_HEADERS,
}
} else {
Expand Down Expand Up @@ -3442,20 +3418,6 @@ export default async function build(
(r) => r.page === route.pathname
)
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
// it. This ensures that for each fallback shell there's an
// entry in the app routes manifest which enables routing for
Expand Down Expand Up @@ -3523,27 +3485,6 @@ export default async function build(
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, {
Expand Down
14 changes: 2 additions & 12 deletions packages/next/src/export/routes/app-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { isDynamicUsageError } from '../helpers/is-dynamic-usage-error'
import {
NEXT_CACHE_TAGS_HEADER,
NEXT_META_SUFFIX,
RSC_PREFETCH_SUFFIX,
RSC_SUFFIX,
RSC_SEGMENTS_DIR_SUFFIX,
RSC_SEGMENT_SUFFIX,
Expand Down Expand Up @@ -140,20 +139,11 @@ export async function exportAppPage(
throw new Error(`Invariant: failed to get page data for ${path}`)
}
} 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
// contain the dynamic data. We do this if any routes have PPR enabled so
// that the cache read/write is the same.
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 {
if (!renderOpts.experimental.isRoutePPREnabled) {
// Writing the RSC payload to a file if we don't have PPR enabled.
fileWriter.append(
htmlFilepath.replace(/\.html$/, RSC_SUFFIX),
Expand Down
1 change: 0 additions & 1 deletion packages/next/src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export const PRERENDER_REVALIDATE_HEADER = 'x-prerender-revalidate'
export const PRERENDER_REVALIDATE_ONLY_GENERATED_HEADER =
'x-prerender-revalidate-if-generated'

export const RSC_PREFETCH_SUFFIX = '.prefetch.rsc'
export const RSC_SEGMENTS_DIR_SUFFIX = '.segments'
export const RSC_SEGMENT_SUFFIX = '.segment.rsc'
export const RSC_SUFFIX = '.rsc'
Expand Down
24 changes: 0 additions & 24 deletions packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ import {
isAppPageRouteModule,
isAppRouteRouteModule,
} from './route-modules/checks'
import { PrefetchRSCPathnameNormalizer } from './normalizers/request/prefetch-rsc'
import { NextDataPathnameNormalizer } from './normalizers/request/next-data'
import { getIsPossibleServerAction } from './lib/server-action-request-meta'
import { isInterceptionRouteAppPath } from '../shared/lib/router/utils/interception-routes'
Expand Down Expand Up @@ -415,7 +414,6 @@ export default abstract class Server<

protected readonly normalizers: {
readonly rsc: RSCPathnameNormalizer | undefined
readonly prefetchRSC: PrefetchRSCPathnameNormalizer | undefined
readonly segmentPrefetchRSC: SegmentPrefixRSCPathnameNormalizer | undefined
readonly data: NextDataPathnameNormalizer | undefined
}
Expand Down Expand Up @@ -496,10 +494,6 @@ export default abstract class Server<
this.enabledDirectories.app && this.minimalMode
? new RSCPathnameNormalizer()
: undefined,
prefetchRSC:
this.isAppPPREnabled && this.minimalMode
? new PrefetchRSCPathnameNormalizer()
: undefined,
segmentPrefetchRSC: this.minimalMode
? new SegmentPrefixRSCPathnameNormalizer()
: undefined,
Expand Down Expand Up @@ -600,17 +594,6 @@ export default abstract class Server<
addRequestMeta(req, 'isRSCRequest', true)
addRequestMeta(req, 'isPrefetchRSCRequest', true)
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)) {
parsedUrl.pathname = this.normalizers.rsc.normalize(
parsedUrl.pathname,
Expand Down Expand Up @@ -1584,12 +1567,6 @@ export default abstract class Server<
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) {
normalizers.push(this.normalizers.rsc)
}
Expand Down Expand Up @@ -2324,7 +2301,6 @@ export default abstract class Server<

for (const normalizer of [
this.normalizers.segmentPrefetchRSC,
this.normalizers.prefetchRSC,
this.normalizers.rsc,
]) {
if (normalizer?.match(initPathname)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
NEXT_CACHE_TAGS_HEADER,
NEXT_DATA_SUFFIX,
NEXT_META_SUFFIX,
RSC_PREFETCH_SUFFIX,
RSC_SEGMENT_SUFFIX,
RSC_SEGMENTS_DIR_SUFFIX,
RSC_SUFFIX,
Expand Down Expand Up @@ -231,10 +230,10 @@ export default class FileSystemCache implements CacheHandler {
}

let rscData: Buffer | undefined
if (!ctx.isFallback) {
if (!ctx.isFallback && !ctx.isRoutePPREnabled) {
rscData = await this.fs.readFile(
this.getFilePath(
`${key}${ctx.isRoutePPREnabled ? RSC_PREFETCH_SUFFIX : RSC_SUFFIX}`,
`${key}${RSC_SUFFIX}`,
IncrementalCacheKind.APP_PAGE
)
)
Expand Down Expand Up @@ -396,16 +395,10 @@ export default class FileSystemCache implements CacheHandler {
writer.append(htmlPath, data.html)

// Fallbacks don't generate a data file.
if (!ctx.fetchCache && !ctx.isFallback) {
if (!ctx.fetchCache && !ctx.isFallback && !ctx.isRoutePPREnabled) {
writer.append(
this.getFilePath(
`${key}${
isAppPath
? ctx.isRoutePPREnabled
? RSC_PREFETCH_SUFFIX
: RSC_SUFFIX
: NEXT_DATA_SUFFIX
}`,
`${key}${isAppPath ? RSC_SUFFIX : NEXT_DATA_SUFFIX}`,
isAppPath
? IncrementalCacheKind.APP_PAGE
: IncrementalCacheKind.PAGES
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
buildInversePrefetchSegmentDataRoute,
buildPrefetchSegmentDataRoute,
} from './build-prefetch-segment-data-route'
import { buildPrefetchSegmentDataRoute } from './build-prefetch-segment-data-route'

describe('buildPrefetchSegmentDataRoute', () => {
it('should build a prefetch segment data route', () => {
Expand All @@ -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$",
}
`)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import path from '../../../shared/lib/isomorphic/path'
import { normalizePagePath } from '../../../shared/lib/page-path/normalize-page-path'
import { getNamedRouteRegex } from '../../../shared/lib/router/utils/route-regex'
import {
RSC_PREFETCH_SUFFIX,
RSC_SEGMENT_SUFFIX,
RSC_SEGMENTS_DIR_SUFFIX,
} from '../../../lib/constants'
Expand Down Expand Up @@ -40,40 +39,3 @@ export function buildPrefetchSegmentDataRoute(
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,
}
}
8 changes: 1 addition & 7 deletions packages/next/src/server/lib/router-utils/filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import {
import { normalizePathSep } from '../../../shared/lib/page-path/normalize-path-sep'
import { normalizeMetadataRoute } from '../../../lib/metadata/get-metadata-route'
import { RSCPathnameNormalizer } from '../../normalizers/request/rsc'
import { PrefetchRSCPathnameNormalizer } from '../../normalizers/request/prefetch-rsc'
import { encodeURIPath } from '../../../shared/lib/encode-uri-path'
import { isMetadataRouteFile } from '../../../lib/metadata/is-metadata-route'

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

return {
Expand Down Expand Up @@ -483,9 +479,7 @@ export async function setupFsCheck(opts: {
// Simulate minimal mode requests by normalizing RSC and postponed
// requests.
if (opts.minimalMode) {
if (normalizers.prefetchRSC?.match(itemPath)) {
itemPath = normalizers.prefetchRSC.normalize(itemPath, true)
} else if (normalizers.rsc.match(itemPath)) {
if (normalizers.rsc.match(itemPath)) {
itemPath = normalizers.rsc.normalize(itemPath, true)
}
}
Expand Down
Loading
Loading