Skip to content

Type resolved cacheLife profiles, dropping runtime asserts#95428

Draft
unstubbable wants to merge 1 commit into
canaryfrom
hl/resolved-cache-life-profiles
Draft

Type resolved cacheLife profiles, dropping runtime asserts#95428
unstubbable wants to merge 1 commit into
canaryfrom
hl/resolved-cache-life-profiles

Conversation

@unstubbable

@unstubbable unstubbable commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

Runtime "use cache" code validated the default cache-life profile on every invocation because cacheLifeProfiles was typed as optional with partial profiles: assertDefaultCacheLife on the generate and RDC-read paths, a per-cacheLife() presence InvariantError, and optional chaining on reads.

Config normalization already guarantees this once: assignDefaultsAndValidate builds the config from {...defaultConfig, ...config}, so cacheLife is always present, and it backfills the default profile's stale, revalidate, and expire. This moves that guarantee into the type. A new ResolvedCacheLifeProfiles (in config-shared) types the default profile as Required<CacheLife> and overrides NextConfigComplete.cacheLife, and the type is threaded, non-optional, through the render options, work store, and the build, export, and dev workers. Runtime code now reads
cacheLifeProfiles.default directly, and the asserts, the presence guard, and the optional chaining are gone.

The proxy (middleware) work store is the one construction site without a resolved profile: the proxy does not support "use cache", so it never reads cacheLife. It is given a sentinel whose default getter throws if ever read, matching the "never read" sentinels already used for its other unused render options.

@github-actions

github-actions Bot commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

Failing test suites

Commit: 5e1a210 | About building and testing Next.js

pnpm test-dev test/development/middleware-errors/index.test.ts (job)

  • middleware - development errors > when middleware contains an unhandled rejection > logs the error correctly (DD)
  • middleware - development errors > when middleware throws synchronously > logs the error correctly (DD)
  • middleware - development errors > when running invalid dynamic code with eval > logs the error correctly (DD)
Expand output

● middleware - development errors › when middleware throws synchronously › logs the error correctly

expect(received).toContain(expected) // indexOf

Expected substring: "
⨯ Error: boom

  at default (../middleware.js:3:15)
    1 |"
  Received string:    "▲ Next.js 16.3.0-canary.75 (webpack)
  - Local:         http://localhost:42711
  - Network:       http://10.0.0.64:42711
  ✓ Ready in 339ms
  ✓ Running next.config took 15ms
  ⚠ The \"middleware\" file convention is deprecated. Please use \"proxy\" instead.·
    To migrate automatically, run:
    npx @next/codemod@canary middleware-to-proxy .·
    Learn more: https://nextjs.org/docs/messages/middleware-to-proxy
  - Experiments (use with caution):
    ✓ strictRouteTypes (enabled by `__NEXT_EXPERIMENTAL_STRICT_ROUTE_TYPES`)·
  ⨯ Error: boom
  at default (../middleware.js:3:15)
  at eval (../../../src/server/web/adapter.ts:356:36)
  at eval (../../../src/server/web/adapter.ts:355:43)
  at async adapter (../../../src/server/web/adapter.ts:271:14)
    1 |
    2 |       export default function () {
  > 3 |         throw new Error('boom')
      |               ^
    4 |       }
  "
  at Object.toContain (development/middleware-errors/index.test.ts:34:41)

● middleware - development errors › when middleware contains an unhandled rejection › logs the error correctly

expect(received).toContain(expected) // indexOf

Expected substring: "
⨯ unhandledRejection:  Error: async boom!

  at throwError (../middleware.js:4:15)
  at default (../middleware.js:7:9)
    2 |       import { NextResponse } from 'next/server'"
  Received string:    "▲ Next.js 16.3.0-canary.75 (webpack)
  - Local:         http://localhost:34571
  - Network:       http://10.0.0.64:34571
  ✓ Ready in 379ms
  ✓ Running next.config took 14ms
  ⚠ The \"middleware\" file convention is deprecated. Please use \"proxy\" instead.·
    To migrate automatically, run:
    npx @next/codemod@canary middleware-to-proxy .·
    Learn more: https://nextjs.org/docs/messages/middleware-to-proxy
  - Experiments (use with caution):
    ✓ strictRouteTypes (enabled by `__NEXT_EXPERIMENTAL_STRICT_ROUTE_TYPES`)·
  Error: async boom!
  at throwError (../middleware.js:4:15)
  at default (../middleware.js:7:9)
  at eval (../../../src/server/web/adapter.ts:356:36)
  at eval (../../../src/server/web/adapter.ts:355:43)
  at async adapter (../../../src/server/web/adapter.ts:271:14)
    2 |       import { NextResponse } from 'next/server'
    3 |       async function throwError() {
  > 4 |         throw new Error('async boom!')
      |               ^
    5 |       }
    6 |       export default function () {
    7 |         throwError()
  ⨯ unhandledRejection: Error: async boom!
  at throwError (../middleware.js:4:15)
  at default (../middleware.js:7:9)
  at eval (../../../src/server/web/adapter.ts:356:36)
  at eval (../../../src/server/web/adapter.ts:355:43)
  at async adapter (../../../src/server/web/adapter.ts:271:14)
    2 |       import { NextResponse } from 'next/server'
    3 |       async function throwError() {
  > 4 |         throw new Error('async boom!')
      |               ^
    5 |       }
    6 |       export default function () {
    7 |         throwError()
  ⨯ unhandledRejection:  Error: async boom!
  at throwError (../middleware.js:4:15)
  at default (../middleware.js:7:9)
  at eval (../../../src/server/web/adapter.ts:356:36)
  at eval (../../../src/server/web/adapter.ts:355:43)
  at async adapter (../../../src/server/web/adapter.ts:271:14)
    2 |       import { NextResponse } from 'next/server'
    3 |       async function throwError() {
  > 4 |         throw new Error('async boom!')
      |               ^
    5 |       }
    6 |       export default function () {
    7 |         throwError()
  "
  at Object.toContain (development/middleware-errors/index.test.ts:137:41)

● middleware - development errors › when running invalid dynamic code with eval › logs the error correctly

expect(received).toContain(expected) // indexOf

Expected substring: "
⨯ Error [ReferenceError]: test is not defined
    at eval (file://webpack-internal:///(middleware)/./middleware.js)

  at eval (../middleware.js:4:9)
  at default (../middleware.js:4:9)
    2 |       import { NextResponse } from 'next/server'"
  Received string:    "▲ Next.js 16.3.0-canary.75 (webpack)
  - Local:         http://localhost:45449
  - Network:       http://10.0.0.64:45449
  ✓ Ready in 463ms
  ✓ Running next.config took 19ms
  ⚠ The \"middleware\" file convention is deprecated. Please use \"proxy\" instead.·
    To migrate automatically, run:
    npx @next/codemod@canary middleware-to-proxy .·
    Learn more: https://nextjs.org/docs/messages/middleware-to-proxy
  - Experiments (use with caution):
    ✓ strictRouteTypes (enabled by `__NEXT_EXPERIMENTAL_STRICT_ROUTE_TYPES`)·
  ⚠ DynamicCodeEvaluationWarning: Dynamic Code Evaluation (e. g. 'eval', 'new Function') not allowed in Edge Runtime
  Learn More: https://nextjs.org/docs/messages/edge-dynamic-code-evaluation
  at default (../middleware.js:4:9)
  at eval (../../../src/server/web/adapter.ts:356:36)
  at eval (../../../src/server/web/adapter.ts:355:43)
  at async adapter (../../../src/server/web/adapter.ts:271:14)
    2 |       import { NextResponse } from 'next/server'
    3 |       export default function () {
  > 4 |         eval('test')
      |         ^
    5 |         return NextResponse.next()
    6 |       }
  ⨯ Error [ReferenceError]: test is not defined
      at eval (file://webpack-internal:///(middleware)/./middleware.js)
  at eval (../middleware.js:4:9)
  at default (../middleware.js:4:9)
  at eval (../../../src/server/web/adapter.ts:356:36)
  at eval (../../../src/server/web/adapter.ts:355:43)
  at async adapter (../../../src/server/web/adapter.ts:271:14)
    2 |       import { NextResponse } from 'next/server'
    3 |       export default function () {
  > 4 |         eval('test')
      |         ^
    5 |         return NextResponse.next()
    6 |       }
  "
  at Object.toContain (development/middleware-errors/index.test.ts:197:41)

@github-actions

github-actions Bot commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

Stats cancelled

Commit: 5e1a210
View workflow run

@unstubbable unstubbable force-pushed the hl/resolved-cache-life-profiles branch from 85e936a to e48fb66 Compare July 2, 2026 20:35
Base automatically changed from hl/fix-false-positive-nested-cache-error to canary July 2, 2026 21:53
Runtime `"use cache"` code validated the default cache-life profile on
every invocation because `cacheLifeProfiles` was typed as optional with
partial profiles: `assertDefaultCacheLife` on the generate and RDC-read
paths, a per-`cacheLife()` presence `InvariantError`, and optional
chaining on reads.

Config normalization already guarantees this once:
`assignDefaultsAndValidate` builds the config from `{ ...defaultConfig,
...config }`, so `cacheLife` is always present, and it backfills the
`default` profile's `stale`, `revalidate`, and `expire`. This moves that
guarantee into the type. A new `ResolvedCacheLifeProfiles` (in
`config-shared`) types the `default` profile as `Required<CacheLife>`
and overrides `NextConfigComplete.cacheLife`, and the type is threaded,
non-optional, through the render options, work store, and the build,
export, and dev workers. Runtime code now reads
`cacheLifeProfiles.default` directly, and the asserts, the presence
guard, and the optional chaining are gone.

The proxy (middleware) work store is the one construction site without a
resolved profile: the proxy does not support `"use cache"`, so it never
reads `cacheLife`. It is given a sentinel whose `default` getter throws
if ever read, matching the "never read" sentinels already used for its
other unused render options.
@unstubbable unstubbable force-pushed the hl/resolved-cache-life-profiles branch from e48fb66 to 5e1a210 Compare July 2, 2026 21:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant