Skip to content

Commit

Permalink
[next] Update to route app rsc paths correctly (#8611)
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed Sep 22, 2022
1 parent 225e0a4 commit f5486a8
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 12 deletions.
33 changes: 33 additions & 0 deletions packages/next/src/server-build.ts
Expand Up @@ -1131,6 +1131,23 @@ export async function serverBuild({
});
}

if (appPathRoutesManifest) {
// create .rsc variant for app lambdas and edge functions
// to match prerenders so we can route the same when the
// __flight__ header is present
const edgeFunctions = middleware.edgeFunctions;

for (let route of Object.values(appPathRoutesManifest)) {
route = path.posix.join('./', route === '/' ? '/index' : route);

if (lambdas[route]) {
lambdas[`${route}.rsc`] = lambdas[route];
} else if (edgeFunctions[route]) {
edgeFunctions[`${route}.rsc`] = edgeFunctions[route];
}
}
}

return {
wildcard: wildcardConfig,
images:
Expand Down Expand Up @@ -1369,6 +1386,22 @@ export async function serverBuild({
// to prevent a local/deploy mismatch
...(!isCorrectMiddlewareOrder ? middleware.staticRoutes : []),

...(appDir
? [
{
src: `^${path.posix.join('/', entryDirectory, '/(.*)$')}`,
has: [
{
type: 'header',
key: '__flight__',
},
],
dest: path.posix.join('/', entryDirectory, '/$1.rsc'),
check: true,
},
]
: []),

// Next.js page lambdas, `static/` folder, reserved assets, and `public/`
// folder
{ handle: 'filesystem' },
Expand Down
7 changes: 5 additions & 2 deletions packages/next/src/utils.ts
Expand Up @@ -2229,7 +2229,11 @@ export async function getMiddlewareBundle({
prerenderBypassToken: string;
routesManifest: RoutesManifest;
isCorrectMiddlewareOrder: boolean;
}) {
}): Promise<{
staticRoutes: Route[];
dynamicRouteMap: Map<string, RouteWithSrc>;
edgeFunctions: Record<string, EdgeFunction>;
}> {
const middlewareManifest = await getMiddlewareManifest(
entryPath,
outputDirectory
Expand Down Expand Up @@ -2420,7 +2424,6 @@ export async function getMiddlewareBundle({
}
}
}

return source;
}

Expand Down
30 changes: 21 additions & 9 deletions packages/next/test/fixtures/00-app-dir/vercel.json
Expand Up @@ -12,15 +12,26 @@
"mustContain": "hello from app/dashboard"
},
{
"path": "/dashboard/another",
"path": "/dashboard",
"status": 200,
"mustContain": "hello from newroot/dashboard/another"
"headers": {
"__flight__": "1"
},
"mustContain": "M1:{",
"mustNotContain": "<html"
},
{
"path": "/dashboard/deployments/123",
"path": "/dashboard/another",
"status": 200,
"mustContain": "hello from app/dashboard/deployments/[id]. ID is: <!-- -->123"
"mustContain": "hello from newroot/dashboard/another"
},
// TODO: uncomment after this is fixed upstream
// x-ref: https://vercel.slack.com/archives/C035J346QQL/p1663820032810519?thread_ts=1663775935.504379&cid=C035J346QQL
// {
// "path": "/dashboard/deployments/123",
// "status": 200,
// "mustContain": "hello from app/dashboard/deployments/[id]. ID is: <!-- -->123"
// },
{
"path": "/",
"status": 200,
Expand All @@ -31,11 +42,12 @@
"status": 200,
"mustContain": "hello from pages/blog/[slug]"
},
{
"path": "/dynamic/category-1/id-1",
"status": 200,
"mustContain": "{&quot;category&quot;:&quot;category-1&quot;,&quot;id&quot;:&quot;id-1&quot;}"
},
// TODO: uncomment after this is fixed upstream
// {
// "path": "/dynamic/category-1/id-1",
// "status": 200,
// "mustContain": "{&quot;category&quot;:&quot;category-1&quot;,&quot;id&quot;:&quot;id-1&quot;}"
// },
{
"path": "/dashboard/changelog",
"status": 200,
Expand Down
3 changes: 2 additions & 1 deletion test/lib/run-build-lambda.js
@@ -1,6 +1,7 @@
const os = require('os');
const path = require('path');
const fs = require('fs-extra');
const json5 = require('json5');
const { glob } = require('@vercel/build-utils');

function runAnalyze(wrapper, context) {
Expand All @@ -18,7 +19,7 @@ async function runBuildLambda(inputPath) {
if (typeof expect !== 'undefined') {
expect(nowJsonRef).toBeDefined();
}
const nowJson = require(nowJsonRef.fsPath);
const nowJson = json5.parse(await fs.readFile(nowJsonRef.fsPath, 'utf8'));
const build = nowJson.builds[0];

if (typeof expect !== 'undefined') {
Expand Down

0 comments on commit f5486a8

Please sign in to comment.