Skip to content

fix(core): keep computed env reads out of graphs that do not import getEnv - #68

Open
martijn00 wants to merge 1 commit into
supabase:mainfrom
martijn00:fix/get-env-durable-cache
Open

martijn00 wants to merge 1 commit into
supabase:mainfrom
martijn00:fix/get-env-durable-cache

Conversation

@martijn00

Copy link
Copy Markdown

What changed

  • runtimeName no longer imports std-env at runtime. It runs the same checks as std-env 4.2 (Netlify, EdgeRuntime, the Workers user agent, fastly, Deno, Bun, process.versions.node, in that order) inline, with no environment reads. std-env stays a dependency for the RuntimeName type, which is re-exported unchanged.
  • getEnv moves from src/core/runtime.ts to src/core/env.ts. It reads process.env[key] directly where std-env's env proxy read it before. The signature, the resolution order (platform bindings, then process.env, then Deno.env.get) and the public exports are the same. runtime.ts keeps the captured platform object and exposes an internal getPlatformBinding for env.ts.
  • tsdown.config.ts sets unbundle: true, so the build emits one file per source module and each entry file only re-exports. The exports map and its paths are unchanged.
  • Docs: the README environment section, src/core/README.md and one path in the authoring guide.

Why

Next.js 16.5 makes "use cache" entries durable across deployments. Since vercel/next.js#102094, a cached function whose module graph contains a computed process.env read loses that durability, because Next.js can no longer list the variables the entry depends on.

I confirmed the report in #67 with a Next.js 16.5.0 build and TURBOPACK_PRINT_USE_CACHE_SUBTREE=1, using a page whose "use cache" function imports from this package:

Import in the cached graph main This branch
defineMiddleware std-env/dist/index.mjs with dynamic env access no dynamic env access
pipeline, defineComposite std-env/dist/index.mjs with dynamic env access no dynamic env access
withCors from /cors std-env/dist/index.mjs with dynamic env access no dynamic env access
getEnv std-env/dist/index.mjs with dynamic env access dist/core/env.mjs with dynamic env access

The source of the read on main is std-env itself: its module reads process.env by key at load time for CI and agent detection. Moving getEnv alone was not enough. With the bundled output, pipeline and defineComposite live in dist/index.mjs, which imports the getEnv chunk, and Turbopack kept that import. With one file per module, dist/index.mjs is a pure re-export barrel, and with sideEffects: false Turbopack leaves core/env.mjs out of any graph that does not use getEnv.

A graph that calls getEnv still carries a computed read. That is correct: the key is a runtime value, so Next.js cannot know which variable the entry depends on, and hiding the read would let a durable entry survive a change to that variable. The README now says so and points to reading a known variable by its literal name (process.env.SUPABASE_URL) inside durable entries, which Next.js tracks as part of the cache key. This matches the approach in supabase/server#236 for resolveEnv.

Closes #67

Tests

  • New src/core/env.test.ts loads every non-test source through import.meta.glob and fails if any module other than env.ts reads process.env or imports a value from std-env. It fails on main for runtime.ts.
  • pnpm typecheck, pnpm lint, pnpm format:check, pnpm test (137 tests), pnpm build, pnpm smoke, pnpm check-exports, pnpm typecheck:min, pnpm typecheck:consumer, pnpm test:bun, pnpm test:workers (reports workerd), pnpm docs (same 4 warnings as main) and pnpm dlx jsr@0.14.3 publish --dry-run --allow-dirty pass locally.
  • Not run locally: pnpm test:deno, because Deno is not installed here. CI covers it.

Notes

  • The CommonJS build cannot skip unused re-exports, so require('@supabase/middleware') still loads core/env.cjs. Next.js resolves the import condition for server code, which is the case above.
  • unbundle changes the files inside dist/ (for example dist/core/pipeline.mjs instead of a hashed shared chunk). Nothing outside the package should import those paths, since the exports map does not list them.
  • This overlaps textually with feat: honor the Workers ExecutionContext through waitUntil #65 and feat: export bufferRequestInPlace for frameworks that keep the same Request #66, which also edit src/core/runtime.ts, src/index.ts and src/core/index.ts. The changes are independent, so whichever lands second needs a small rebase.
  • @supabase/server's OAuth helpers call getEnv, so their graphs keep a computed read until they read their variables by literal name. That belongs in supabase/server.

🤖 Generated with Claude Code

…etEnv

Next.js 16.5 drops cross-deployment durability for a "use cache" function
whose module graph contains a computed process.env read. Every graph that
imported @supabase/middleware had one, through std-env, which reads
process.env by key when it loads.

runtimeName now uses std-env's detection order inline, with no env reads,
and std-env is only imported for its RuntimeName type. getEnv moves to its
own module, and the build emits one file per source module, so pipeline,
defineMiddleware and the built-in middleware no longer pull the computed
read into a graph. getEnv keeps its signature and resolution order.

Refs supabase#67

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

getEnv reads env by computed key, which may turn off durable "use cache" entries in Next.js 16.5

1 participant