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:
parent
139e573da2
commit
9dd24a5931
2 changed files with 11 additions and 1 deletions
|
@ -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
|
||||
|
|
|
@ -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 () => {
|
||||
|
|
Loading…
Reference in a new issue