Skip to content

Commit

Permalink
[next] Ensure vary header is set for static app paths (#8737)
Browse files Browse the repository at this point in the history
### Related Issues

x-ref: [slack thread](https://vercel.slack.com/archives/C035J346QQL/p1666025811694049?thread_ts=1666008214.416389&cid=C035J346QQL)
x-ref: vercel/next.js#41479

### 📋 Checklist

<!--
  Please keep your PR as a Draft until the checklist is complete
-->

#### Tests

- [ ] The code changed/added as part of this PR has been covered with tests
- [ ] All tests pass locally with `yarn test-unit`

#### Code Review

- [ ] This PR has a concise title and thorough description useful to a reviewer
- [ ] Issue from task tracker has a link to this PR
  • Loading branch information
ijjk committed Oct 18, 2022
1 parent cfe6550 commit 300e6c6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
39 changes: 39 additions & 0 deletions packages/next/src/server-build.ts
Expand Up @@ -964,6 +964,43 @@ export async function serverBuild({
await getStaticFiles(entryPath, entryDirectory, outputDirectory);

const notFoundPreviewRoutes: RouteWithSrc[] = [];
const appDirVaryRoutes: RouteWithSrc[] = [];

// ensure Vary header is set for static app paths
// TODO: remove when we are able to set initial headers on Prerender
if (appPathRoutesManifest) {
for (const route of Object.values(appPathRoutesManifest)) {
if (!prerenders[path.posix.join('.', entryDirectory, route)]) {
continue;
}
let src = path.posix.join('/', entryDirectory, route);

if (isDynamicRoute(route)) {
const dynamicRoute = routesManifest.dynamicRoutes.find(r => {
return r.page === route;
});

if (
dynamicRoute &&
'namedRegex' in dynamicRoute &&
dynamicRoute.namedRegex
) {
src = src.replace(
new RegExp(`${escapeStringRegexp(route)}$`),
dynamicRoute.namedRegex.replace(/^\^/, '')
);
}
}

appDirVaryRoutes.push({
src,
headers: {
vary: '__rsc__, __next_router_state_tree__, __next_router_prefetch__',
},
continue: true,
});
}
}

if (prerenderManifest.notFoundRoutes?.length > 0 && canUsePreviewMode) {
// we combine routes into one src here to reduce the number of needed
Expand Down Expand Up @@ -1653,10 +1690,12 @@ export async function serverBuild({
src: path.posix.join('/', entryDirectory, '/(.*).rsc$'),
headers: {
'content-type': 'application/octet-stream',
vary: '__rsc__, __next_router_state_tree__, __next_router_prefetch__',
},
continue: true,
important: true,
},
...appDirVaryRoutes,
]
: []),

Expand Down
8 changes: 6 additions & 2 deletions packages/next/test/fixtures/00-app-dir/vercel.json
Expand Up @@ -9,7 +9,10 @@
{
"path": "/dashboard",
"status": 200,
"mustContain": "hello from app/dashboard"
"mustContain": "hello from app/dashboard",
"responseHeaders": {
"vary": "__rsc__, __next_router_state_tree__, __next_router_prefetch__"
}
},
{
"path": "/dashboard",
Expand All @@ -27,7 +30,8 @@
"__rsc__": "1"
},
"responseHeaders": {
"content-type": "application/octet-stream"
"content-type": "application/octet-stream",
"vary": "__rsc__, __next_router_state_tree__, __next_router_prefetch__"
}
},
{
Expand Down

0 comments on commit 300e6c6

Please sign in to comment.