Unverified Commit 77bab390 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Ensure bundle 404 can be rewritten in dev (#45728)

This fixes a mismatch in behavior between dev/start where bundle 404s
could be rewritten in production but not dev. Also ensures we have a
regression test for this behavior.

x-ref: [slack
thread](https://vercel.slack.com/archives/C03S8ED1DKM/p1675869554570429)

## 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)

---------
Co-authored-by: 's avatarShu Uesugi <shu.chibicode@gmail.com>
parent 94ffcba2
...@@ -60,7 +60,7 @@ export async function renderScriptError( ...@@ -60,7 +60,7 @@ export async function renderScriptError(
res: ServerResponse, res: ServerResponse,
error: Error, error: Error,
{ verbose = true } = {} { verbose = true } = {}
) { ): Promise<{ finished: true | undefined }> {
// Asks CDNs and others to not to cache the errored page // Asks CDNs and others to not to cache the errored page
res.setHeader( res.setHeader(
'Cache-Control', 'Cache-Control',
...@@ -68,9 +68,7 @@ export async function renderScriptError( ...@@ -68,9 +68,7 @@ export async function renderScriptError(
) )
if ((error as any).code === 'ENOENT') { if ((error as any).code === 'ENOENT') {
res.statusCode = 404 return { finished: undefined }
res.end('404 - Not Found')
return
} }
if (verbose) { if (verbose) {
...@@ -78,6 +76,7 @@ export async function renderScriptError( ...@@ -78,6 +76,7 @@ export async function renderScriptError(
} }
res.statusCode = 500 res.statusCode = 500
res.end('500 - Internal Error') res.end('500 - Internal Error')
return { finished: true }
} }
function addCorsSupport(req: IncomingMessage, res: ServerResponse) { function addCorsSupport(req: IncomingMessage, res: ServerResponse) {
...@@ -271,14 +270,14 @@ export default class HotReloader { ...@@ -271,14 +270,14 @@ export default class HotReloader {
try { try {
await this.ensurePage({ page, clientOnly: true }) await this.ensurePage({ page, clientOnly: true })
} catch (error) { } catch (error) {
await renderScriptError(pageBundleRes, getProperError(error)) return await renderScriptError(pageBundleRes, getProperError(error))
return { finished: true }
} }
const errors = await this.getCompilationErrors(page) const errors = await this.getCompilationErrors(page)
if (errors.length > 0) { if (errors.length > 0) {
await renderScriptError(pageBundleRes, errors[0], { verbose: false }) return await renderScriptError(pageBundleRes, errors[0], {
return { finished: true } verbose: false,
})
} }
} }
......
...@@ -49,6 +49,12 @@ export async function middleware(request) { ...@@ -49,6 +49,12 @@ export async function middleware(request) {
return NextResponse.next() return NextResponse.next()
} }
if (
url.pathname.includes('/_next/static/chunks/pages/_app-non-existent.js')
) {
return NextResponse.rewrite('https://example.vercel.sh')
}
if (url.pathname === '/api/edge-search-params') { if (url.pathname === '/api/edge-search-params') {
const newUrl = url.clone() const newUrl = url.clone()
newUrl.searchParams.set('foo', 'bar') newUrl.searchParams.set('foo', 'bar')
......
...@@ -70,6 +70,16 @@ describe('Middleware Runtime', () => { ...@@ -70,6 +70,16 @@ describe('Middleware Runtime', () => {
} }
function runTests({ i18n }: { i18n?: boolean }) { function runTests({ i18n }: { i18n?: boolean }) {
it('should be able to rewrite on _next/static/chunks/pages/ 404', async () => {
const res = await fetchViaHTTP(
next.url,
'/_next/static/chunks/pages/_app-non-existent.js'
)
expect(res.status).toBe(200)
expect(await res.text()).toContain('Example Domain')
})
if ((global as any).isNextDev) { if ((global as any).isNextDev) {
it('refreshes the page when middleware changes ', async () => { it('refreshes the page when middleware changes ', async () => {
const browser = await webdriver(next.url, `/about`) const browser = await webdriver(next.url, `/about`)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment