[now-next] Make sure to 404 and clear cache header for invalid _next requests (#3801)

This handles an edge case where deeply nested dynamic routes were handling `_next` 404s that were cached forever since the immutable header we set isn't cleared after not matching on the filesystem.

I updated our cache-header fixture to check for this edge case also to make sure we don't regress on this
This commit is contained in:
JJ Kasper
2020-02-17 11:42:51 -06:00
committed by GitHub
parent 6e1a72f557
commit 5d1069d464
8 changed files with 58 additions and 0 deletions

View File

@@ -459,6 +459,20 @@ export const build = async ({
// folder
{ handle: 'filesystem' },
// This needs to come directly after handle: filesystem to make sure to
// 404 and clear the cache header for _next requests
{
src: path.join(
'/',
entryDirectory,
'_next/static/(?:[^/]+/pages|chunks|runtime|css|media)/.+'
),
headers: {
'cache-control': '',
},
status: 404,
},
...rewrites,
// Dynamic routes
// TODO: do we want to do this?: ...dynamicRoutes,
@@ -1043,6 +1057,20 @@ export const build = async ({
// folder
{ handle: 'filesystem' },
// This needs to come directly after handle: filesystem to make sure to
// 404 and clear the cache header for _next requests
{
src: path.join(
'/',
entryDirectory,
'_next/static/(?:[^/]+/pages|chunks|runtime|css|media)/.+'
),
headers: {
'cache-control': '',
},
status: 404,
},
...rewrites,
// Dynamic routes
...dynamicRoutes,

View File

@@ -7,6 +7,16 @@
"responseHeaders": {
"cache-control": "public,max-age=31536000,immutable"
}
},
{
"path": "/_next/static/invalid-build-id/pages/index.js",
"notResponseHeaders": {
"cache-control": "public,max-age=31536000,immutable"
}
},
{
"path": "/_next/static/invalid-build-id/pages/non-existent.js",
"mustNotContain": "final route"
}
]
}

View File

@@ -0,0 +1 @@
export default () => 'hi from final route'

View File

@@ -0,0 +1 @@
export default () => 'hi from another route'

View File

@@ -0,0 +1 @@
export default () => 'hi from deployment route'

View File

@@ -0,0 +1 @@
export default () => 'hi from project route'

View File

@@ -0,0 +1 @@
export default () => 'hi from team route'

View File

@@ -163,6 +163,21 @@ async function testDeployment(
);
}
});
} else if (probe.notResponseHeaders) {
Object.keys(probe.notResponseHeaders).forEach(header => {
const headerValue = resp.headers.get(header);
const expected = probe.notResponseHeaders[header];
if (headerValue === expected) {
const headers = Array.from(resp.headers.entries())
.map(([k, v]) => ` ${k}=${v}`)
.join('\n');
throw new Error(
`Page ${probeUrl} invalid page header ${header}.\n\n Did not expect: ${header}=${expected}.\nBut got ${headers}`
);
}
});
} else if (!probe.status) {
assert(false, 'probe must have a test condition');
}