Skip to content

Return plain text 404 for non-document requests to unknown paths - #95930

Merged
gaearon merged 4 commits into
canaryfrom
gaearon-not-found
Jul 24, 2026
Merged

gaearon merged 4 commits into
canaryfrom
gaearon-not-found

Conversation

@gaearon

@gaearon gaearon commented Jul 18, 2026 •

Copy link
Copy Markdown
Contributor

Not sure if there's any downside to this.

Currently, there are situations when a browser requests something in a particular format:

  • Favicons
  • Manifest icons
  • <img> with broken src
  • Wrong <link>s
  • new Worker()

These requests have a Sec-Fetch-Dest header which can definitely exclude things that can't be HTML. My proposal is to not return the HTML for them (even in dev) so that this doesn't waste time in case rendering the root layout is slow or expensive. Less work for the server, whether dev or prod.

I believe this was happening in dev, and in prod if rendering not-found requires something dynamic (e.g. in the root layout). And for static self-hosted, I believe it would respond with static HTML.

After this change, we don't render the HTML.

Before

Image in manifest hits the real /not-found route and runs application code:

Screenshot 2026-07-19 at 00 23 01

Notice 404 at the end with 2 seconds in application code.

After

Image in manifest is still 404 but doesn't hit any app code.

Screenshot 2026-07-19 at 00 21 17

It's maybe a bit weird it doesn't show in the terminal log. In that sense it's like /_next/static/ because it's handled at an earlier layer. Maybe we could still log it but it would have to happen from a different place, and also breaks consistency, so I opted with keeping it hidden.


I can't see any coherent arguments for why this should reach the app render so this should be OK?

@github-actions

github-actions Bot commented Jul 18, 2026 •

Copy link
Copy Markdown
Contributor

Stats from current PR

🔴 3 regressions

Metric Canary PR Change Trend
Webpack Build Time (cached) 26.285s 27.220s 🔴 +935ms (+4%) ▇▁▃██
Webpack Cold (First Request) 3.434s 3.617s 🔴 +183ms (+5%) ▃▁▁▂▃
Webpack Warm (First Request) 3.366s 3.513s 🔴 +147ms (+4%) ▄▁▁▂▂
📊 All Metrics
📖 Metrics Glossary

Dev Server Metrics:

  • Listen = TCP port starts accepting connections
  • First Request = HTTP server returns successful response
  • Cold = Fresh build (no cache)
  • Warm = With cached build artifacts

Build Metrics:

  • Fresh = Clean build (no .next directory)
  • Cached = With existing .next directory

Change Thresholds:

  • Time: Changes < 50ms AND < 10%, OR < 2% are insignificant
  • Size: Changes < 1KB AND < 1% are insignificant
  • All other changes are flagged to catch regressions

⚡ Dev Server

Metric Canary PR Change Trend
Cold (Listen) 1.065s 1.065s ✓ ▁▂▂▂▂
Cold (Ready in log) 1.045s 1.033s ✓ ▁▁▂▂▁
Cold (First Request) 1.709s 1.694s ✓ ▁▁▃▃▂
Warm (Listen) 1.065s 1.064s ✓ ▁▁▂▂▂
Warm (Ready in log) 1.035s 1.033s ✓ ▁▁▂▂▁
Warm (First Request) 1.681s 1.684s ✓ ▂▁▂▃▁
📦 Dev Server (Webpack) (Legacy)

📦 Dev Server (Webpack)

Metric Canary PR Change Trend
Cold (Listen) 812ms 863ms ✓ █▁▁██
Cold (Ready in log) 787ms 822ms ✓ █▂▃▇▇
Cold (First Request) 3.434s 3.617s 🔴 +183ms (+5%) ▃▁▁▂▃
Warm (Listen) 813ms 813ms ✓ █▁▁██
Warm (Ready in log) 773ms 798ms ✓ █▃▃▇█
Warm (First Request) 3.366s 3.513s 🔴 +147ms (+4%) ▄▁▁▂▂

⚡ Production Builds

Metric Canary PR Change Trend
Fresh Build 7.179s 7.088s ✓ ▁▁▂▄▂
Cached Build 7.225s 7.317s ✓ ▁▁▃▄▂
📦 Production Builds (Webpack) (Legacy)

📦 Production Builds (Webpack)

Metric Canary PR Change Trend
Fresh Build 26.400s 26.482s ✓ █▁▃▅▇
Cached Build 26.285s 27.220s 🔴 +935ms (+4%) ▇▁▃██
node_modules Size 527 MB 527 MB ✓ ▁▁▁▁▁
📦 Bundle Sizes

Bundle Sizes

⚡ Turbopack

Client

Main Bundles
Canary PR Change
020ywijt5nj6h.js gzip 162 B N/A -
0a-p0meqxynjz.js gzip 10.3 kB N/A -
0avtke4yefj4-.js gzip 13.1 kB N/A -
0cz1d0mv5g_q7.js gzip 39.4 kB 39.4 kB ✓
0k0mowevltnd8.js gzip 156 B N/A -
0pt5of3ch6d2u.js gzip 13.6 kB N/A -
0xnsdo-6y9r3k.js gzip 5.72 kB N/A -
0yi7is8u4covm.js gzip 8.77 kB N/A -
13o5mq9-iq7w7.js gzip 65.6 kB N/A -
1d1l5ssh5rr7h.js gzip 157 B N/A -
1elt1qium-r2m.css gzip 115 B 115 B ✓
1prv-wg6jgtt2.js gzip 8.77 kB N/A -
1q2o3jxzuohdw.js gzip 157 B N/A -
1t47vod02e_4a.js gzip 8.75 kB N/A -
1tf1phijqlx9j.js gzip 220 B 220 B ✓
1tw002sq5bo4l.js gzip 10 kB N/A -
1wlerik1n7zbr.js gzip 8.7 kB N/A -
1xc6xtfjdu27q.js gzip 156 B N/A -
22q0om86f2f2y.js gzip 157 B N/A -
28koph4jjzmen.js gzip 45.2 kB N/A -
2l12bmavujito.js gzip 154 B N/A -
2lsr7u4ksfdec.js gzip 156 B N/A -
2lviwqiqxm904.js gzip 3.52 kB N/A -
2pevdqjpbf3xk.js gzip 9.45 kB N/A -
2qd2anu9eizlz.js gzip 8.75 kB N/A -
2sv39ae5_8vfj.js gzip 8.81 kB N/A -
2utx6n7w65xqa.js gzip 8.7 kB N/A -
3-ukivglqkfeq.js gzip 8.78 kB N/A -
325xisqrylugu.js gzip 450 B N/A -
3i1km7gub3184.js gzip 156 B N/A -
3jc9ghvds1spf.js gzip 2.29 kB N/A -
3juwkvkgvywfe.js gzip 10.6 kB N/A -
3kbove7q58uqh.js gzip 13.2 kB N/A -
3ku0nvgf4mxxb.js gzip 157 B N/A -
3lmdscffnd80s.js gzip 162 B N/A -
3qn7vntp30gaj.js gzip 1.47 kB N/A -
3u9ziwgnnaj5j.js gzip 7.4 kB N/A -
3xwty9p9n9ocu.js gzip 169 B N/A -
3yo-64802foqw.js gzip 71.1 kB N/A -
3z3gybhple7ca.js gzip 158 B N/A -
turbopack-0h..d21z.js gzip 3.81 kB N/A -
turbopack-0r..9xgy.js gzip 3.81 kB N/A -
turbopack-0w..1anq.js gzip 3.81 kB N/A -
turbopack-0y.._pk2.js gzip 3.81 kB N/A -
turbopack-19..c7bm.js gzip 3.81 kB N/A -
turbopack-1l..6x32.js gzip 3.82 kB N/A -
turbopack-1w..nfcm.js gzip 3.81 kB N/A -
turbopack-2_..xhvq.js gzip 3.81 kB N/A -
turbopack-26..pdsv.js gzip 3.79 kB N/A -
turbopack-2h..r2vy.js gzip 3.81 kB N/A -
turbopack-2t..o_0c.js gzip 3.81 kB N/A -
turbopack-2u..l8cy.js gzip 3.8 kB N/A -
turbopack-3_..r6ym.js gzip 3.81 kB N/A -
turbopack-39..ix4o.js gzip 3.81 kB N/A -
02sdbrhey7j7e.js gzip N/A 156 B -
030a1njeh5b77.js gzip N/A 10 kB -
0793r14k-42g0.js gzip N/A 71.1 kB -
0a8n-jguxfekn.js gzip N/A 65.6 kB -
0hvpxe74qwhsv.js gzip N/A 153 B -
0ot0qt5np20h7.js gzip N/A 8.78 kB -
0qpfmgpnybc5k.js gzip N/A 155 B -
1-qxxhh8-axib.js gzip N/A 45.2 kB -
12heqfhr2fq7h.js gzip N/A 157 B -
12m9rr7_7cexd.js gzip N/A 8.7 kB -
14e9p7ipvwu28.js gzip N/A 170 B -
186r3y_rfvr0b.js gzip N/A 8.78 kB -
19v97848yrjp7.js gzip N/A 8.81 kB -
1l1ravpajbxlq.js gzip N/A 162 B -
1nd_0rwgz2ozk.js gzip N/A 9.45 kB -
1oi9pltr_e10m.js gzip N/A 10.6 kB -
1rd9tzqgzsz2w.js gzip N/A 8.77 kB -
1rq1g_cwv642d.js gzip N/A 10.3 kB -
1uojw_uabeg93.js gzip N/A 450 B -
1w-7lusgyl81y.js gzip N/A 8.7 kB -
1xrsv7h-eh212.js gzip N/A 156 B -
2_yiudjsfz4l7.js gzip N/A 157 B -
21h56wdn9zddo.js gzip N/A 8.75 kB -
26oztzwo5776p.js gzip N/A 162 B -
28ezo_zo5_x-9.js gzip N/A 156 B -
2lg10omv7xu14.js gzip N/A 3.52 kB -
2ovff1533zvno.js gzip N/A 13.2 kB -
2s4z8jy0z7v93.js gzip N/A 1.46 kB -
2y6gb5bs4mnam.js gzip N/A 8.75 kB -
341h71ln1jpl3.js gzip N/A 2.29 kB -
34ttpt_n7605h.js gzip N/A 5.72 kB -
36ckfq9o-ys-a.js gzip N/A 13.1 kB -
38n1na-4zsfpn.js gzip N/A 7.41 kB -
3bsuzxfl28c9h.js gzip N/A 157 B -
3nc76t01leeue.js gzip N/A 156 B -
3su2d458ivoh-.js gzip N/A 157 B -
3vdh6_u50pu5t.js gzip N/A 13.6 kB -
turbopack-0-..ddc3.js gzip N/A 3.81 kB -
turbopack-0c..3fxe.js gzip N/A 3.81 kB -
turbopack-15..1-ev.js gzip N/A 3.81 kB -
turbopack-16..7lk6.js gzip N/A 3.8 kB -
turbopack-1k..ch4l.js gzip N/A 3.81 kB -
turbopack-1l..lxpb.js gzip N/A 3.81 kB -
turbopack-2v..cmeo.js gzip N/A 3.81 kB -
turbopack-2z..ib9y.js gzip N/A 3.8 kB -
turbopack-38..nxjf.js gzip N/A 3.81 kB -
turbopack-3b..-u7x.js gzip N/A 3.82 kB -
turbopack-3h..p6gp.js gzip N/A 3.79 kB -
turbopack-3l..j1et.js gzip N/A 3.81 kB -
turbopack-3s..bcea.js gzip N/A 3.8 kB -
turbopack-41..9dnj.js gzip N/A 3.81 kB -
Total 448 kB 448 kB ⚠️ +28 B

Server

Middleware
Canary PR Change
middleware-b..fest.js gzip 780 B 775 B ✓
Total 780 B 775 B ✅ -5 B
Build Details
Build Manifests
Canary PR Change
_buildManifest.js gzip 431 B 431 B ✓
Total 431 B 431 B ✓

📦 Webpack

Client

Main Bundles
Canary PR Change
3322-HASH.js gzip 63.7 kB N/A -
4191.HASH.js gzip 169 B N/A -
7920-HASH.js gzip 4.68 kB N/A -
9784-HASH.js gzip 5.63 kB N/A -
b1ad9f4c-HASH.js gzip 62.9 kB N/A -
framework-HASH.js gzip 59.7 kB 59.7 kB ✓
main-app-HASH.js gzip 254 B 253 B ✓
main-HASH.js gzip 40 kB 40 kB ✓
webpack-HASH.js gzip 1.68 kB 1.68 kB ✓
3577.HASH.js gzip N/A 168 B -
578-HASH.js gzip N/A 64.4 kB -
8590-HASH.js gzip N/A 5.61 kB -
9750-HASH.js gzip N/A 4.69 kB -
a8984546-HASH.js gzip N/A 62.9 kB -
Total 239 kB 239 kB ⚠️ +636 B
Polyfills
Canary PR Change
polyfills-HASH.js gzip 39.4 kB 39.4 kB ✓
Total 39.4 kB 39.4 kB ✓
Pages
Canary PR Change
_app-HASH.js gzip 194 B 193 B ✓
_error-HASH.js gzip 181 B 182 B ✓
css-HASH.js gzip 334 B 331 B ✓
dynamic-HASH.js gzip 1.81 kB 1.81 kB ✓
edge-ssr-HASH.js gzip 255 B 253 B ✓
head-HASH.js gzip 349 B 351 B ✓
hooks-HASH.js gzip 382 B 384 B ✓
image-HASH.js gzip 581 B 582 B ✓
index-HASH.js gzip 260 B 259 B ✓
link-HASH.js gzip 2.48 kB 2.48 kB ✓
routerDirect..HASH.js gzip 317 B 318 B ✓
script-HASH.js gzip 384 B 386 B ✓
withRouter-HASH.js gzip 316 B 315 B ✓
1afbb74e6ecf..834.css gzip 106 B 106 B ✓
Total 7.95 kB 7.95 kB ⚠️ +1 B

Server

Edge SSR
Canary PR Change
edge-ssr.js gzip 128 kB 128 kB ✓
page.js gzip 287 kB 287 kB ✓
Total 415 kB 415 kB ✅ -570 B
Middleware
Canary PR Change
middleware-b..fest.js gzip 619 B 619 B ✓
middleware-r..fest.js gzip 156 B 156 B ✓
middleware.js gzip 45.5 kB 45.1 kB ✓
edge-runtime..pack.js gzip 842 B 842 B ✓
Total 47.1 kB 46.8 kB ✅ -338 B
Build Details
Build Manifests
Canary PR Change
_buildManifest.js gzip 718 B 718 B ✓
Total 718 B 718 B ✓
Build Cache
Canary PR Change
0.pack gzip 4.84 MB 4.84 MB ✓
index.pack gzip 120 kB 120 kB ✓
index.pack.old gzip 121 kB 119 kB 🟢 2.15 kB (-2%)
Total 5.08 MB 5.08 MB ✅ -603 B

🔄 Shared (bundler-independent)

Runtimes
Canary PR Change
app-page-exp...dev.js gzip 366 kB 366 kB ✓
app-page-exp..prod.js gzip 202 kB 202 kB ✓
app-page-tur...dev.js gzip 365 kB 365 kB ✓
app-page-tur..prod.js gzip 202 kB 202 kB ✓
app-page-tur...dev.js gzip 362 kB 362 kB ✓
app-page-tur..prod.js gzip 200 kB 200 kB ✓
app-page.run...dev.js gzip 362 kB 362 kB ✓
app-page.run..prod.js gzip 200 kB 200 kB ✓
app-route-ex...dev.js gzip 81.8 kB 81.8 kB ✓
app-route-ex..prod.js gzip 55.6 kB 55.6 kB ✓
app-route-tu...dev.js gzip 81.8 kB 81.8 kB ✓
app-route-tu..prod.js gzip 55.6 kB 55.6 kB ✓
app-route-tu...dev.js gzip 81.4 kB 81.4 kB ✓
app-route-tu..prod.js gzip 55.4 kB 55.4 kB ✓
app-route.ru...dev.js gzip 81.4 kB 81.4 kB ✓
app-route.ru..prod.js gzip 55.4 kB 55.4 kB ✓
dist_client_...dev.js gzip 324 B 324 B ✓
dist_client_...dev.js gzip 326 B 326 B ✓
dist_client_...dev.js gzip 318 B 318 B ✓
dist_client_...dev.js gzip 317 B 317 B ✓
pages-api-tu...dev.js gzip 45.4 kB 45.4 kB ✓
pages-api-tu..prod.js gzip 34.1 kB 34.1 kB ✓
pages-api.ru...dev.js gzip 45.4 kB 45.4 kB ✓
pages-api.ru..prod.js gzip 34.1 kB 34.1 kB ✓
pages-turbo....dev.js gzip 54.9 kB 54.9 kB ✓
pages-turbo...prod.js gzip 39.7 kB 39.7 kB ✓
pages.runtim...dev.js gzip 54.8 kB 54.8 kB ✓
pages.runtim..prod.js gzip 39.7 kB 39.7 kB ✓
server.runti..prod.js gzip 67.8 kB 68 kB ✓
use-cache-pr...dev.js gzip 71.6 kB 71.6 kB ✓
use-cache-pr...dev.js gzip 71.6 kB 71.6 kB ✓
use-cache-pr...dev.js gzip 69.9 kB 69.9 kB ✓
use-cache-pr...dev.js gzip 69.9 kB 69.9 kB ✓
Total 3.51 MB 3.51 MB ⚠️ +168 B
📝 Changed Files (1 file)

Files with changes:

  • server.runtime.prod.js
View diffs
server.runtime.prod.js

Diff too large to display

📎 Tarball URL
https://vercel-packages.vercel.app/next/commits/d2ea04f8157e9afda76f44e9c503a4ce32cd1f90/next

Commit: d2ea04f

@github-actions

github-actions Bot commented Jul 18, 2026 •

Copy link
Copy Markdown
Contributor

Tests Passed

Commit: d2ea04f

@gaearon
gaearon requested a review from timneutkens July 18, 2026 23:35
@gaearon
gaearon force-pushed the gaearon-not-found branch 2 times, most recently from 1a2b6ab to 28175f4 Compare July 19, 2026 00:33
elliott-with-the-longest-name-on-github pushed a commit to sveltejs/kit that referenced this pull request Jul 21, 2026
… subresource requests (#16463)

closes #13734

Diagnosis in the issue. Requests whose `Sec-Fetch-Dest` can never render
HTML now get a plain `Not Found` with `Vary: Sec-Fetch-Dest` instead of
the SSR'd error page, same shape as the `/_app/` 404 a few lines up.
Absent header, `empty`, `document` and unknown values are unchanged.
Also fixes the stale Safari comment in `errors.js` (sec-fetch-dest ships
since 16.4). Prior art: vercel/next.js#95930, vitejs/vite#20866.

Note: `handleError` no longer runs for these (the default hook discards
them anyway).
@gaearon
gaearon force-pushed the gaearon-not-found branch 3 times, most recently from fd931f3 to eb2fc4b Compare July 22, 2026 12:54
Comment thread packages/next/src/server/base-server.ts Resolved
@gaearon
gaearon force-pushed the gaearon-not-found branch from 768b5bc to 85ece44 Compare July 22, 2026 15:05
export function isNonHtmlSecFetchDest(
value: string | string[] | null | undefined
): boolean {
return typeof value === 'string' && NON_HTML_SEC_FETCH_DESTS.has(value)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could also be an allowlist instead like Vite does. vitejs/vite#20866

Blocklist felt a bit safer but maybe I'm being overly cautious

@gaearon
gaearon force-pushed the gaearon-not-found branch 6 times, most recently from c6bef1c to 9764ca0 Compare July 23, 2026 23:25
gaearon added 4 commits July 24, 2026 00:40
Requests for subresources like images or fonts (e.g. icons listed in a
web app manifest) currently render the full HTML not-found route when
the path doesn't match anything, even though the response body can
never be displayed. In dev this also triggers on-demand compilation of
the not-found route, so a single missing manifest icon can cost seconds
per request.

Short-circuit the 404 handling when the request's Sec-Fetch-Dest
indicates a destination that can't render HTML (image, font, manifest,
style, script, media, workers) and return a plain text 404, matching
the existing behavior for missing /_next/static assets. This is done
in two places: the router server catchall (next dev / next start), and
the base server's 404 render path for minimal mode, where the platform
routes unmatched paths directly to the not-found render.

Document navigations, iframes, fetch/XHR (including RSC requests, which
always use the "empty" destination since Sec-Fetch-* headers are
forbidden for fetch()), and clients that don't send the header are
unaffected and still render the not-found route.
Deployment platforms invoke the compiled not-found route entry directly
through its handler export, without going through the router server or
base server, so the plain text 404 short-circuits didn't apply when
deployed and subresource requests to unknown paths still rendered the
not-found route. Add the same check to the app page template handler
when it serves the not-found route.

Verified against a deployed app that manifest icon and favicon requests
no longer render the not-found route while document and RSC requests
are unaffected. The adapter test invokes the compiled route entry the
way platforms do, and the dynamic e2e suite asserts the plain text 404
in deploy mode where the platform can't serve a prerendered 404.
Resume requests for postponed shells use POST and must not be
intercepted. Also make the dynamic not-found fixtures build with cache
components by moving the dynamic access behind Suspense, so the suites
run in both modes. When deployed with cache components the platform
serves the prerendered not-found shell without invoking Next.js, which
the dynamic suite asserts in that mode.
@gaearon
gaearon force-pushed the gaearon-not-found branch from 9764ca0 to d2ea04f Compare July 23, 2026 23:44
@gaearon
gaearon requested a review from gnoff July 23, 2026 23:44
@gaearon
gaearon merged commit 0950e8a into canary Jul 24, 2026
205 of 226 checks passed
@gaearon
gaearon deleted the gaearon-not-found branch July 24, 2026 02:59
m-kawafuji pushed a commit to m-kawafuji/next.js that referenced this pull request Aug 8, 2026
…cel#95930)

Not sure if there's any downside to this.

Currently, there are situations when a browser requests something in a
particular format:

- Favicons
- Manifest icons
- `<img>` with broken `src`
- Wrong `<link>`s
- `new Worker()`

These requests have a `Sec-Fetch-Dest` header which can definitely
exclude things that can't be HTML. My proposal is to not return the HTML
for them (even in dev) so that this doesn't waste time in case rendering
the root layout is slow or expensive. Less work for the server, whether
dev or prod.

I believe this was happening in dev, and in prod if rendering
`not-found` requires something dynamic (e.g. in the root layout). And
for static self-hosted, I believe it would respond with static HTML.

After this change, we don't render the HTML.

## Before

Image in manifest hits the real `/not-found` route and runs application
code:

<img width="1055" height="639" alt="Screenshot 2026-07-19 at 00 23 01"
src="https://github.com/user-attachments/assets/869f85f8-a009-4266-a345-74e3342adb41"
/>

Notice 404 at the end with 2 seconds in application code.

## After

Image in manifest is still 404 but doesn't hit any app code.

<img width="1033" height="696" alt="Screenshot 2026-07-19 at 00 21 17"
src="https://github.com/user-attachments/assets/3a1817b4-f050-45c0-93ca-0507cf9de803"
/>

It's maybe a bit weird it doesn't show in the terminal log. In that
sense it's like `/_next/static/` because it's handled at an earlier
layer. Maybe we could still log it but it would have to happen from a
different place, and also breaks consistency, so I opted with keeping it
hidden.

---

I can't see any coherent arguments for why this should reach the app
render so this should be OK?
@github-actions github-actions Bot added the locked label Sep 9, 2026
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Sep 9, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants