Skip to content

Durable use cache: fix runtime env var mutation - #98558

Open
mischnic wants to merge 2 commits into
canaryfrom
mischnic/env-var-mutation
Open

Durable use cache: fix runtime env var mutation#98558
mischnic wants to merge 2 commits into
canaryfrom
mischnic/env-var-mutation

Conversation

@mischnic

@mischnic mischnic commented Sep 11, 2026

Copy link
Copy Markdown
Member

Problem

  1. Next computes the warmup cache key while SOME_ENV_VAR is unset.
  2. Rendering ends up setting SOME_ENV_VAR.
  3. Final prerender expects the cache entry to exist, but the env var changed, so the key changed and it's a miss.
  4. Unexpected miss and bailout.

This lead to

Error: Route "foo": Unexpected cache miss after cache warming phase during prerendering. This is
likely caused by non-deterministic arguments that differ between the cache warming phase and the
final prerender phase (e.g. unstable array order). Ensure that arguments passed to cached functions
are deterministic.

Error: Route "foo": Next.js encountered uncached or runtime data during
prerendering.

Solution

Instead, snapshot the env vars once at module evaluation time of the use cache function. This ensures that the cache key doesn't change over time. This is also how it worked thus far (with deployment id as the cache key): changing the env var over the lifetime of the process didn't lead to a reexecution of the use cache function

RDC already stores cache entries generated in the previous phase (be it during next build multi-phase rendering, or from prerender->resuming at runtime). Root params are already excluded from the cache keys when storing in RDC. Also exclude the env var hash bit, to conform to this system of preventing tearing (at the cost of potential staleness).

mischnic commented Sep 11, 2026

Copy link
Copy Markdown
Member Author

@github-actions

github-actions Bot commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Tests Passed

Commit: cc7aa3a

@mischnic mischnic changed the title Add test for runtime env var mutation Durable use cache: fix runtime env var mutation Sep 11, 2026
@mischnic mischnic added CI Bypass Graphite Optimization Ignore Graphite CI optimizations, run the full CI suite. https://graphite.dev/docs/stacking-and-ci CI Bypass PR Stack Optimization Run all CI checks mid-stack (GitHub+Graphite supported). Rebase after adding label to re-trigger. labels Sep 11, 2026 — with Graphite App
Comment on lines +242 to +245
expect(output).not.toContain(
'Unexpected cache miss after cache warming phase during prerendering'
)
expect(await browser.elementById('data').text()).toBe('test:unset')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't love these expect.not.toContain because if we ever change the error text the test stops offering protection. I think we need to snapshot something here but do it in a way that doesn't churn too much. Or we need a debug that prints an affirmation that we didn't get this error or something

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, true. I could also assert Error:, that is hopefully stable...

@mischnic
mischnic force-pushed the mischnic/env-var-mutation branch from 968dcf9 to a9c75c4 Compare September 11, 2026 17:18
@mischnic
mischnic force-pushed the mischnic/env-var-existence-e2e branch from e7699fe to 4068e2f Compare September 11, 2026 17:18
Base automatically changed from mischnic/env-var-existence-e2e to canary September 11, 2026 20:19
@mischnic
mischnic force-pushed the mischnic/env-var-mutation branch from a9c75c4 to cc7aa3a Compare September 11, 2026 21:28
@mischnic
mischnic marked this pull request as ready for review September 11, 2026 21:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI Bypass Graphite Optimization Ignore Graphite CI optimizations, run the full CI suite. https://graphite.dev/docs/stacking-and-ci CI Bypass PR Stack Optimization Run all CI checks mid-stack (GitHub+Graphite supported). Rebase after adding label to re-trigger.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants