Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/next/src/server/lib/router-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,13 @@ export async function initialize(opts: {
'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') {
timneutkens marked this conversation as resolved.
Show resolved Hide resolved
res.statusCode = 404
res.end('')
return null
}

const appNotFound = opts.dev
? devInstance?.serverFields.hasAppNotFound
: await fsChecker.getItem('/_not-found')
Expand Down
3 changes: 3 additions & 0 deletions test/e2e/favicon-short-circuit/app/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export async function GET() {
return new Response('Hello world')
}
38 changes: 38 additions & 0 deletions test/e2e/favicon-short-circuit/favicon-short-circuit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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>')
})
}
}
)
Loading