Unverified Commit 962ce0dc authored by Wyatt Johnson's avatar Wyatt Johnson Committed by GitHub

Short circut 404's for /favicon.ico in development (#54747)

In development, it's common to have a project without a favicon.ico in the public or static folders. This short circuits the route process in development so it doesn't compile the not found page for every page request.
Co-authored-by: 's avatarTim Neutkens <6324199+timneutkens@users.noreply.github.com>
parent 3fbbfeb6
...@@ -676,6 +676,13 @@ export async function initialize(opts: { ...@@ -676,6 +676,13 @@ export async function initialize(opts: {
'no-cache, no-store, max-age=0, must-revalidate' 'no-cache, no-store, max-age=0, must-revalidate'
) )
// Short-circuit favicon.ico serving so that the 404 page doesn't get built as favicon is requested by the browser when loading any route.
if (opts.dev && !matchedOutput && parsedUrl.pathname === '/favicon.ico') {
res.statusCode = 404
res.end('')
return null
}
const appNotFound = opts.dev const appNotFound = opts.dev
? devInstance?.serverFields.hasAppNotFound ? devInstance?.serverFields.hasAppNotFound
: await fsChecker.getItem('/_not-found') : await fsChecker.getItem('/_not-found')
......
export async function GET() {
return new Response('Hello world')
}
import { createNextDescribe } from 'e2e-utils'
createNextDescribe(
'favicon-short-circuit',
{
files: __dirname,
},
({ next, isNextDev, isNextStart }) => {
if (isNextDev) {
it('should short circuit the favicon in development', async () => {
const res = await next.fetch('/favicon.ico')
// Expect we got the right status and headers.
expect(res.status).toBe(404)
expect(res.headers.get('content-type')).toBeNull()
// Expect we got no body.
const text = await res.text()
expect(text).toBeEmpty()
// Expect we didn't compile the not found route.
expect(next.cliOutput).not.toContain('compiling /not-found')
})
} else if (isNextStart) {
it('should not short circuit the favicon in production', async () => {
const res = await next.fetch('/favicon.ico')
// Expect we got the right status and headers.
expect(res.status).toBe(404)
expect(res.headers.get('content-type')).toBe('text/html; charset=utf-8')
// Expect we got the right body.
const html = await res.text()
expect(html).toContain('<html>')
})
}
}
)
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