Skip to content

feat: take a second argument with waitUntil as the execution context - #69

Open
martijn00 wants to merge 2 commits into
supabase:mainfrom
martijn00:feat/wait-until-second-argument
Open

martijn00 wants to merge 2 commits into
supabase:mainfrom
martijn00:feat/wait-until-second-argument

Conversation

@martijn00

@martijn00 martijn00 commented Dec 5, 2026 •

Copy link
Copy Markdown

Stacked on #65. This branch contains #65's commit, because the change needs its waitUntil(ctx, promise) and the execution context it keeps on the seeded context. The change for this PR is the last commit, 8a4a067. Once #65 merges I'll rebase and this diff shrinks to that commit. If you would rather have it as part of #65, say so and I'll move it there.

What

  • seedContext(platformArg, executionContext?): when no execution context came as the third argument and the second argument has a waitUntil method, that argument is the execution context. It is kept on the seeded context for waitUntil(ctx, promise) and is not stored as the module-scoped platform env that getEnv reads.
  • When a third argument carries waitUntil (Workers), nothing changes: the third argument is the execution context and the second is the bindings object.
  • README and doc comments say that a host can pass its execution context second.

Why

Next.js calls proxy(request, event), and the NextFetchEvent in second position is the object with waitUntil. Today the entry call takes it for the bindings object. I ran a proxy.ts that hands (request, event) to a defineMiddleware stack in a Next.js 16.5.0 app (next build, next start):

main This branch
getEnv("sourcePage") inside the stack /proxy (a field of the event) undefined
waitUntil(ctx, promise) reaches event.waitUntil no such export yes

I did not run #65 on its own in the app. By its code it treats the second argument as main does and looks for the execution context in third position only, so waitUntil would fall back to running the promise unattached. There is no third argument on this host, so the "not honored" warning on main never fires either. The details and the reproduction are in #53 (comment).

Bindings never carry a waitUntil function, so the check does not change what Workers see. A second argument with a waitUntil property that is not a function is still captured as bindings.

Refs #53

Testing

  • src/core/runtime.test.ts: an event-shaped second argument (class instance, waitUntil on the prototype, a string field of its own) receives the promises from a middleware and from the handler, its fields are not readable through getEnv, and bindings captured earlier survive; with waitUntil on both arguments the third wins and the second stays the bindings object; a second argument without a waitUntil function is captured as before.
  • Ran locally on Node 24.20.0: pnpm format:check, pnpm lint, pnpm typecheck, pnpm typecheck:min, pnpm build, pnpm typecheck:consumer, pnpm smoke, pnpm check-exports, pnpm test (131 tests), pnpm test:workers, pnpm test:bun and the JSR dry run. All pass.
  • The Next.js check above used the packed build of this branch.
  • Not run: pnpm test:deno (no Deno locally). I did not test Vercel or Netlify functions, whose handlers also take a second context argument; I only claim Next.js.

Not in this PR

The platform env is still one module-level slot, so the last entry call's second argument serves every later getEnv call. The issue comment proposes keeping the bindings on the seeded context too and an optional getEnv(key, ctx). That is an API addition and I have left it for your call.

Overlap with other open PRs

#65 (the base of this branch) and #68 both edit seedContext and the README environment section. #68 moves getEnv to its own file and does not change the lines this commit adds.

🤖 Generated with Claude Code

martijn00 and others added 2 commits December 1, 2026 23:17
The entry call now keeps the third fetch argument on the seeded context
behind a symbol instead of warning and dropping it. A new waitUntil(ctx,
promise) hands background work to it from any middleware or the handler,
and runs the promise unattached with a logged rejection when the host
passed no execution context.

Closes supabase#53

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Next.js calls proxy(request, event) with the event that carries waitUntil in
second position, where the entry call expects bindings. The event was stored
as the module-scoped platform env and its waitUntil could not be reached.

A second argument with a waitUntil method is now kept as the execution context
for waitUntil(ctx, promise) and is not captured as the platform env. A third
argument still wins when both carry waitUntil.

Refs supabase#53

Co-Authored-By: Claude Fable 5.2 <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.

1 participant