[ppr] Fix PPR detection for groups (#11625)

Previously, PPR was either all on, or all off. As we've added support for incremental adoption of PPR, the logic for determining if there's a grouping issue had to be adjusted.

This now only considers a group as supporting PPR if a route in the group supports PPR. As we also use this as a grouping key, this still ensures we won't have conflicts.
This commit is contained in:
Wyatt Johnson
2024-05-22 22:06:12 -06:00
committed by GitHub
parent 464cb26255
commit 73e558913a
4 changed files with 68 additions and 69 deletions

View File

@@ -192,8 +192,12 @@ function normalizePage(page: string): string {
if (!page.startsWith('/')) {
page = `/${page}`;
}
// remove '/index' from the end
page = page.replace(/\/index$/, '/');
// Replace the `/index` with `/`
if (page === '/index') {
page = '/';
}
return page;
}
@@ -320,8 +324,8 @@ export async function getDynamicRoutes({
bypassToken,
isServerMode,
dynamicMiddlewareRouteMap,
experimentalPPRRoutes,
hasActionOutputSupport,
isAppPPREnabled,
}: {
entryPath: string;
entryDirectory: string;
@@ -333,8 +337,8 @@ export async function getDynamicRoutes({
bypassToken?: string;
isServerMode?: boolean;
dynamicMiddlewareRouteMap?: ReadonlyMap<string, RouteWithSrc>;
experimentalPPRRoutes: ReadonlySet<string>;
hasActionOutputSupport: boolean;
isAppPPREnabled: boolean;
}): Promise<RouteWithSrc[]> {
if (routesManifest) {
switch (routesManifest.version) {
@@ -408,7 +412,7 @@ export async function getDynamicRoutes({
];
}
if (experimentalPPRRoutes.has(page)) {
if (isAppPPREnabled) {
let dest = route.dest?.replace(/($|\?)/, '.prefetch.rsc$1');
if (page === '/' || page === '/index') {
@@ -1504,9 +1508,9 @@ export type LambdaGroup = {
maxDuration?: number;
isAppRouter?: boolean;
isAppRouteHandler?: boolean;
isStreaming?: boolean;
isPrerenders?: boolean;
isExperimentalPPR?: boolean;
readonly isStreaming: boolean;
readonly isPrerenders: boolean;
readonly isExperimentalPPR: boolean;
isActionLambda?: boolean;
isPages?: boolean;
isApiLambda: boolean;
@@ -1561,6 +1565,7 @@ export async function getPageLambdaGroups({
const routeName = normalizePage(page.replace(/\.js$/, ''));
const isPrerenderRoute = prerenderRoutes.has(routeName);
const isExperimentalPPR = experimentalPPRRoutes?.has(routeName) ?? false;
const isStreaming = !isPrerenderRoute || isExperimentalPPR;
let opts: { memory?: number; maxDuration?: number } = {};
@@ -1632,6 +1637,7 @@ export async function getPageLambdaGroups({
...opts,
isPrerenders: isPrerenderRoute,
isExperimentalPPR,
isStreaming,
isApiLambda: !!isApiPage(page),
pseudoLayerBytes: initialPseudoLayer.pseudoLayerBytes,
pseudoLayerUncompressedBytes: initialPseudoLayerUncompressed,
@@ -2219,22 +2225,6 @@ export const onPrerenderRoute =
initialStatus = 404;
}
/**
* If the route key had an `/index` suffix added, we need to note it so we
* can remove it from the output path later accurately.
*/
let addedIndexSuffix = false;
if (isAppPathRoute) {
// for literal index routes we need to append an additional /index
// due to the proxy's normalizing for /index routes
if (routeKey !== '/index' && routeKey.endsWith('/index')) {
routeKey = `${routeKey}/index`;
routeFileNoExt = routeKey;
addedIndexSuffix = true;
}
}
let outputPathPage = path.posix.join(entryDirectory, routeFileNoExt);
if (!isAppPathRoute) {
@@ -2428,7 +2418,7 @@ export const onPrerenderRoute =
// static route first, then try the srcRoute if it doesn't exist. If we
// can't find it at all, this constitutes an error.
experimentalStreamingLambdaPath = experimentalStreamingLambdaPaths.get(
pathnameToOutputName(entryDirectory, routeKey, addedIndexSuffix)
pathnameToOutputName(entryDirectory, routeKey)
);
if (!experimentalStreamingLambdaPath && srcRoute) {
experimentalStreamingLambdaPath =
@@ -2689,19 +2679,10 @@ export function getNextServerPath(nextVersion: string) {
: 'next/dist/next-server/server';
}
function pathnameToOutputName(
entryDirectory: string,
pathname: string,
addedIndexSuffix = false
) {
function pathnameToOutputName(entryDirectory: string, pathname: string) {
if (pathname === '/') {
pathname = '/index';
}
// If the `/index` was added for a route that ended in `/index` we need to
// strip the second one off before joining it with the entryDirectory.
else if (addedIndexSuffix) {
pathname = pathname.replace(/\/index$/, '');
}
return path.posix.join(entryDirectory, pathname);
}