Skip to content

Refactor extractExportedConstValue to return { value } | null instead of throwing#90510

Merged
sokra merged 3 commits intocanaryfrom
sokra/avoid-throw
Mar 3, 2026
Merged

Refactor extractExportedConstValue to return { value } | null instead of throwing#90510
sokra merged 3 commits intocanaryfrom
sokra/avoid-throw

Conversation

@sokra
Copy link
Member

@sokra sokra commented Feb 25, 2026

Summary

Refactors extractExportedConstValue and the internal extractValue function to use return values instead of exception-based control flow, improving performance during build analysis.

Commit 1: Refactor extractExportedConstValue

  • Returns { value: any } | null instead of throwing NoSuchDeclarationError
  • null when the exported const declaration is not found, { value } when found
  • Removes NoSuchDeclarationError class

Commit 2: Refactor extractValue

  • Returns ExtractValueResult ({ value: any } | { unsupported: string; path?: string }) instead of throwing UnsupportedValueError
  • Extracts path-formatting logic from UnsupportedValueError constructor into a standalone formatCodePath helper
  • All 6 throw sites become return statements; 3 recursive call sites propagate unsupported results
  • Removes UnsupportedValueError class entirely
  • Removes all try/catch blocks from the 4 callsites in get-page-static-info.ts
  • Updates warnAboutUnsupportedValue to accept the result type directly

Motivation

Both extractExportedConstValue and extractValue used throw/catch for expected control flow — a missing declaration and unsupported AST node types are normal cases during build analysis, not exceptional errors. Every page/route processed during build hits these functions multiple times. Constructing Error objects (which capture stack traces) and unwinding the stack is expensive in V8. Returning discriminated result types avoids this overhead entirely.

Test Plan

  • Existing unit tests pass (test/unit/parse-page-static-info.test.ts — 5/5)
  • No behavioral change: callsites produce identical warnings/errors for unsupported values and silently skip missing declarations, same as before

@nextjs-bot
Copy link
Collaborator

nextjs-bot commented Feb 25, 2026

Failing test suites

Commit: da277a1 | About building and testing Next.js

pnpm test-dev test/e2e/app-dir/app-basepath/index.test.ts (job)

  • app dir - basepath > should successfully hard navigate from pages -> app (DD)
Expand output

● app dir - basepath › should successfully hard navigate from pages -> app

page.waitForSelector: Timeout 10000ms exceeded.
Call log:
  - waiting for locator('#page-2') to be visible

  519 |
  520 |     return this.startChain(async () => {
> 521 |       const el = await page.waitForSelector(selector, {
      |                             ^
  522 |         timeout,
  523 |         state,
  524 |       })

  at waitForSelector (lib/browsers/playwright.ts:521:29)
  at Playwright._chain (lib/browsers/playwright.ts:651:23)
  at Playwright._chain [as startChain] (lib/browsers/playwright.ts:632:17)
  at Playwright.startChain [as waitForElementByCss] (lib/browsers/playwright.ts:520:17)
  at Object.waitForElementByCss (e2e/app-dir/app-basepath/index.test.ts:16:19)

@sokra sokra requested a review from timneutkens February 25, 2026 12:01
@sokra sokra marked this pull request as ready for review February 25, 2026 12:01
@nextjs-bot
Copy link
Collaborator

nextjs-bot commented Feb 25, 2026

Stats from current PR

🔴 1 regression, 1 improvement

Metric Canary PR Change Trend
Turbo Build Time 6.054s 6.357s 🔴 +303ms (+5%) ▁▁▁▁▁
node_modules Size 476 MB 475 MB 🟢 823 kB (0%) ▁▁▁▁▁
📊 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) 914ms 914ms ▁▁▁▁▁
Cold (Ready in log) 884ms 883ms ▁▁▁▁▁
Cold (First Request) 1.596s 1.620s ▁▂▁▁▂
Warm (Listen) 913ms 913ms ▁▁▁▁▁
Warm (Ready in log) 884ms 886ms ▁▁▁▁▁
Warm (First Request) 671ms 670ms ▁▁▁▁▁
📦 Dev Server (Webpack) (Legacy)

📦 Dev Server (Webpack)

Metric Canary PR Change Trend
Cold (Listen) 508ms 508ms ▁▁▁█▁
Cold (Ready in log) 469ms 469ms ▄▄▁▆▃
Cold (First Request) 2.131s 2.137s ▂▃▁▇▂
Warm (Listen) 508ms 508ms ▁▁▁█▁
Warm (Ready in log) 469ms 470ms ▅▄▁▇▄
Warm (First Request) 2.147s 2.150s ▃▃▁▆▂

⚡ Production Builds

Metric Canary PR Change Trend
Fresh Build 6.054s 6.357s 🔴 +303ms (+5%) ▁▁▁▁▁
Cached Build 6.417s 6.266s ▁▁▁▁▁
📦 Production Builds (Webpack) (Legacy)

📦 Production Builds (Webpack)

Metric Canary PR Change Trend
Fresh Build 15.813s 15.767s ▁▃▁▆▁
Cached Build 15.946s 15.922s ▁▃▁▆▁
node_modules Size 476 MB 475 MB 🟢 823 kB (0%) ▁▁▁▁▁
📦 Bundle Sizes

Bundle Sizes

⚡ Turbopack

Client

Main Bundles: **401 kB** → **400 kB** ✅ -943 B

83 files with content-based hashes (individual files not comparable between builds)

Server

Middleware
Canary PR Change
middleware-b..fest.js gzip 768 B 764 B
Total 768 B 764 B ✅ -4 B
Build Details
Build Manifests
Canary PR Change
_buildManifest.js gzip 450 B 452 B
Total 450 B 452 B ⚠️ +2 B

📦 Webpack

Client

Main Bundles
Canary PR Change
5528-HASH.js gzip 5.54 kB N/A -
6280-HASH.js gzip 58.7 kB N/A -
6335.HASH.js gzip 169 B N/A -
912-HASH.js gzip 4.59 kB N/A -
e8aec2e4-HASH.js gzip 62.6 kB N/A -
framework-HASH.js gzip 59.7 kB 59.7 kB
main-app-HASH.js gzip 256 B 254 B
main-HASH.js gzip 39.1 kB 39.1 kB
webpack-HASH.js gzip 1.68 kB 1.68 kB
262-HASH.js gzip N/A 4.59 kB -
2889.HASH.js gzip N/A 169 B -
5602-HASH.js gzip N/A 5.55 kB -
6948ada0-HASH.js gzip N/A 62.6 kB -
9544-HASH.js gzip N/A 59 kB -
Total 232 kB 233 kB ⚠️ +292 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 194 B
_error-HASH.js gzip 183 B 180 B 🟢 3 B (-2%)
css-HASH.js gzip 331 B 330 B
dynamic-HASH.js gzip 1.81 kB 1.81 kB
edge-ssr-HASH.js gzip 256 B 256 B
head-HASH.js gzip 351 B 352 B
hooks-HASH.js gzip 384 B 383 B
image-HASH.js gzip 580 B 581 B
index-HASH.js gzip 260 B 260 B
link-HASH.js gzip 2.5 kB 2.5 kB
routerDirect..HASH.js gzip 320 B 319 B
script-HASH.js gzip 386 B 386 B
withRouter-HASH.js gzip 315 B 315 B
1afbb74e6ecf..834.css gzip 106 B 106 B
Total 7.97 kB 7.97 kB ✅ -2 B

Server

Edge SSR
Canary PR Change
edge-ssr.js gzip 125 kB 125 kB
page.js gzip 255 kB 254 kB
Total 380 kB 379 kB ✅ -432 B
Middleware
Canary PR Change
middleware-b..fest.js gzip 618 B 612 B
middleware-r..fest.js gzip 156 B 155 B
middleware.js gzip 43.8 kB 44 kB
edge-runtime..pack.js gzip 842 B 842 B
Total 45.4 kB 45.6 kB ⚠️ +155 B
Build Details
Build Manifests
Canary PR Change
_buildManifest.js gzip 715 B 718 B
Total 715 B 718 B ⚠️ +3 B
Build Cache
Canary PR Change
0.pack gzip 4.05 MB 4.02 MB 🟢 22.9 kB (-1%)
index.pack gzip 104 kB 103 kB
index.pack.old gzip 102 kB 103 kB 🔴 +1.09 kB (+1%)
Total 4.25 MB 4.23 MB ✅ -22.3 kB

🔄 Shared (bundler-independent)

Runtimes
Canary PR Change
app-page-exp...dev.js gzip 321 kB 320 kB
app-page-exp..prod.js gzip 170 kB 170 kB
app-page-tur...dev.js gzip 320 kB 319 kB
app-page-tur..prod.js gzip 170 kB 169 kB
app-page-tur...dev.js gzip 317 kB 316 kB
app-page-tur..prod.js gzip 168 kB 168 kB
app-page.run...dev.js gzip 317 kB 316 kB
app-page.run..prod.js gzip 168 kB 168 kB
app-route-ex...dev.js gzip 70.8 kB 70.8 kB
app-route-ex..prod.js gzip 49.3 kB 49.2 kB
app-route-tu...dev.js gzip 70.9 kB 70.8 kB
app-route-tu..prod.js gzip 49.3 kB 49.2 kB
app-route-tu...dev.js gzip 70.5 kB 70.4 kB
app-route-tu..prod.js gzip 49 kB 49 kB
app-route.ru...dev.js gzip 70.4 kB 70.4 kB
app-route.ru..prod.js gzip 49 kB 49 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 43.2 kB 43.2 kB
pages-api-tu..prod.js gzip 32.9 kB 32.9 kB
pages-api.ru...dev.js gzip 43.2 kB 43.2 kB
pages-api.ru..prod.js gzip 32.9 kB 32.8 kB
pages-turbo....dev.js gzip 52.6 kB 52.5 kB
pages-turbo...prod.js gzip 38.5 kB 38.5 kB
pages.runtim...dev.js gzip 52.6 kB 52.5 kB
pages.runtim..prod.js gzip 38.5 kB 38.4 kB
server.runti..prod.js gzip 62 kB 62 kB
Total 2.83 MB 2.82 MB ✅ -6.07 kB
📝 Changed Files (25 files)

Files with changes:

  • app-page-exp..ntime.dev.js
  • app-page-exp..time.prod.js
  • app-page-tur..ntime.dev.js
  • app-page-tur..time.prod.js
  • app-page-tur..ntime.dev.js
  • app-page-tur..time.prod.js
  • app-page.runtime.dev.js
  • app-page.runtime.prod.js
  • app-route-ex..ntime.dev.js
  • app-route-ex..time.prod.js
  • app-route-tu..ntime.dev.js
  • app-route-tu..time.prod.js
  • app-route-tu..ntime.dev.js
  • app-route-tu..time.prod.js
  • app-route.runtime.dev.js
  • app-route.ru..time.prod.js
  • pages-api-tu..ntime.dev.js
  • pages-api-tu..time.prod.js
  • pages-api.runtime.dev.js
  • pages-api.ru..time.prod.js
  • ... and 5 more
View diffs
app-page-exp..ntime.dev.js
failed to diff
app-page-exp..time.prod.js
failed to diff
app-page-tur..ntime.dev.js
failed to diff
app-page-tur..time.prod.js
failed to diff
app-page-tur..ntime.dev.js
failed to diff
app-page-tur..time.prod.js
failed to diff
app-page.runtime.dev.js
failed to diff
app-page.runtime.prod.js
failed to diff
app-route-ex..ntime.dev.js

Diff too large to display

app-route-ex..time.prod.js

Diff too large to display

app-route-tu..ntime.dev.js

Diff too large to display

app-route-tu..time.prod.js

Diff too large to display

app-route-tu..ntime.dev.js

Diff too large to display

app-route-tu..time.prod.js

Diff too large to display

app-route.runtime.dev.js

Diff too large to display

app-route.ru..time.prod.js

Diff too large to display

pages-api-tu..ntime.dev.js

Diff too large to display

pages-api-tu..time.prod.js

Diff too large to display

pages-api.runtime.dev.js

Diff too large to display

pages-api.ru..time.prod.js

Diff too large to display

pages-turbo...ntime.dev.js

Diff too large to display

pages-turbo...time.prod.js

Diff too large to display

pages.runtime.dev.js

Diff too large to display

pages.runtime.prod.js

Diff too large to display

server.runtime.prod.js

Diff too large to display

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

… of throwing

Replace throw-based control flow with a return-based approach for better
performance. The function now returns `{ value: any } | null` — `null` when
the declaration is not found, `{ value }` when found. UnsupportedValueError
is still thrown for genuinely unsupported values.

Remove NoSuchDeclarationError (no longer needed) and update all callsites
in get-page-static-info.ts to unwrap the result.
…portedValueError

Replace throw-based control flow in extractValue with a discriminated
return type: `{ value: any } | { unsupported: string; path?: string }`.

- Extract path-formatting logic from UnsupportedValueError constructor
  into a standalone formatCodePath helper
- All 6 throw sites become return statements
- 3 recursive call sites propagate unsupported results
- Remove UnsupportedValueError class entirely
- Remove try/catch blocks from all 4 callsites in get-page-static-info.ts
- Update warnAboutUnsupportedValue to accept the result type directly
@sokra sokra force-pushed the sokra/avoid-throw branch from 6e366ed to 9f9107f Compare February 26, 2026 08:47
parseModule returns null when SWC parsing fails. After the refactor that
removed try-catch from get-page-static-info.ts, extractExportedConstValue
could crash on null ast. Accept Module | null and return null early.
@sokra sokra requested a review from mischnic March 3, 2026 10:52
@sokra sokra merged commit ac86a61 into canary Mar 3, 2026
285 of 287 checks passed
Copy link
Member Author

sokra commented Mar 3, 2026

Merge activity

@sokra sokra deleted the sokra/avoid-throw branch March 3, 2026 12:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants