Fix middleware 404 prefetch case (#46782)

Ensures we properly generate the 404 page when a path is being prefetched with middleware configured. 

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

Closes: https://github.com/vercel/next.js/issues/38239#issuecomment-1454928413
This commit is contained in:
JJ Kasper 2023-03-04 18:32:53 -08:00 committed by GitHub
parent 139e573da2
commit 9dd24a5931
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 1 deletions

View file

@ -1196,7 +1196,11 @@ export default abstract class Server<ServerOptions extends Options = Options> {
// when we are handling a middleware prefetch and it doesn't
// resolve to a static data route we bail early to avoid
// unexpected SSR invocations
if (!isSSG && req.headers['x-middleware-prefetch']) {
if (
!isSSG &&
req.headers['x-middleware-prefetch'] &&
!(is404Page || pathname === '/_error')
) {
res.setHeader('x-middleware-skip', '1')
res.body('{}').send()
return null

View file

@ -80,6 +80,12 @@ describe('Middleware Runtime', () => {
/This page could not be found/
)
expect(await browser.eval('window.beforeNav')).toBe(1)
await browser.refresh()
await check(
() => browser.eval('document.documentElement.innerHTML'),
/This page could not be found/
)
})
it('should be able to rewrite on _next/static/chunks/pages/ 404', async () => {