Repository navigation
fix(astro): apply the request scope and dev triggers to entries that call handle() - #951
Open
morinokami wants to merge 2 commits into
Open
morinokami wants to merge 2 commits into
morinokami wants to merge 2 commits into
Conversation
…call handle() An Astro app with a Worker entry of its own hands requests to Astro with `handle()` from `@astrojs/cloudflare/handler`: the entry that Astro's adapter guide shows. Void wraps only the adapter's server entry, so such an app ran without the wrapper: - With PostgreSQL or MySQL, a request ran outside Void's request scope and void/db opened a connection for every query: four for a request with four queries, where the adapter's own entry opens one. `astro dev` and the built Worker both did this, without a warning. - In dev, next to the app's own middleware, `/__void/scheduled` and `/__void/queue` were not answered: the request went on to Astro, which returned 404. voidzero-dev#434 listed this as a known limit and warned about it. The plugin that wraps the server entry now also resolves `@astrojs/cloudflare/handler` to a module that wraps `handle()` the same way. The server entry imports `handle` by a relative path, which the plugin does not see, so each request is still wrapped once. An entry that reaches Astro another way, such as `cf()` from `@astrojs/cloudflare/fetch`, still skips the wrapper. The dev warning for an app with its own middleware and its own entry now says which two calls work, and appears only when the entry's source names neither module. astro.test.ts runs its PostgreSQL tests with both entries, so its shard estimate goes up. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The wrapper that Void puts around `handle()` is a `\0` module, and Vite's dependency scan stops there: it never records `@astrojs/cloudflare/handler`, which the wrapper imports. The Worker's imports reveal it later, so on the first `astro dev` start of an app whose own entry is wrapped, the optimizer of the Worker environment ran a second time. Nothing reloaded, and later starts use the cache. Name the module in `optimizeDeps.include` of a Worker environment in dev, as the adapter names its server entry. The hook runs after the adapter's, which turns discovery off in the server that `astro build` and `astro sync` start to generate types. An include there would be prebundled for nothing, so that server is left alone. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
morinokami
marked this pull request as ready for review
November 28, 2026 07:57
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
An Astro app that needs a Worker entry of its own, for example to export a Durable Object class, hands requests to Astro with
handle(), as the adapter's guide shows:Such an app runs without the wrapper that Void puts around the adapter's Worker entry, and loses two things.
It opens a database connection for every query. This route runs four queries, and opens four connections to PostgreSQL for one request:
With the adapter's own entry, the same request opens one. This happens in
astro devand in the built Worker, and nothing warns about it.In dev, with a
src/middleware.tsof its own, the app also has no trigger endpoints. The request that Void's dev hint prints reaches Astro, which answers 404:#434 listed this as a known limit, with a warning that asks the user to rewrite the entry.
Void wraps
@astrojs/cloudflare/entrypoints/server, the Worker entry of an app that sets none. The wrapper opens Void's request scope for PostgreSQL and MySQL, where void/db keeps one pool for the request, and in dev, next to the app's own middleware, it answers the trigger endpoints before Astro. An entry of the app's own never imports that module. Outside a request scope, void/db builds a pool for every query.Fix
The plugin that wraps the server entry now also resolves
@astrojs/cloudflare/handlerto a module that exports a wrappedhandle(). Both wrappers come from the same code.handleby a relative path, which the plugin doesn't see. A request through that entry is wrapped once, as before, and so is the build-time prerender Worker, whose entry is always the server entry.Vite's dependency scan can't see through the wrapper, which is a
\0module, so it never records@astrojs/cloudflare/handler. The optimizer of the Worker environment then ran a second time on the firstastro devstart of an app whose entry is wrapped. The plugin now names the module for that optimizer, as the adapter names its server entry.astro buildandastro syncstart to generate types is left alone. The adapter turns discovery off there, and an include would be prebundled for nothing. The hook runs after the adapter's to see that.The dev warning for an app with its own middleware and its own entry changes too:
@astrojs/cloudflare/handlernor@astrojs/cloudflare/entrypoints/server.An entry that reaches Astro another way, such as
cf()from@astrojs/cloudflare/fetch, still runs without the wrapper. In dev, next to the app's own middleware, it gets the warning.Docs
packages/void/AGENTS.mdrecords thathandle()runs in the request scope too, and which entries skip the wrapper. No user-facing page describes the wrapper, so none changes.Tests
packages/void/test/integration/astro.test.ts(8 cases): the PostgreSQL suite now runs twice, with the adapter's entry and with thehandle()entry above. Each run queries through void/db in dev, counts one connection per request in dev and in the built Worker, and runs a cron through/__void/schedulednext to the app's own middleware.test/integration-timings.tsraises the file's estimate for the second dev server and build.packages/void/test/integration/plugin-vite-environment.test.ts(16 cases): the "Astro server entry" cases cover both modules.handle()'s module is named for the optimizer of a Worker environment that discovers dependencies in dev, and not when discovery is off or in a build.packages/void/test/unit/plugin-framework-triggers.test.ts(6 cases): next to the app's own middleware, an entry that callshandle()or the server entry'sfetch()gets no warning, and one built oncf()or one that can't be read gets it.14 of the 30 cases fail without this fix. The other 16 check what
mainalready does: the adapter's own entry, a D1 app that gets no wrapper, the warning cases that didn't change, and the environments where nothing is named for the optimizer.Verification
vp test run: 903 files pass, 2 skipped (18,756 tests pass, 34 skipped).vp run check:types: 36/36 pass.playground/astro(D1) with a middleware of its own, inastro dev:handle()entry above: pages answer 200,POST /__void/scheduledanswers{"ok":true}, and no warning appears.fetch()on the server entry's default export: the same.cf(): the warning appears, andPOST /__void/scheduledanswers 404.DEBUG=vite:depsand thehandle()entry:astro sync, the type-generation server has prebundled only Void's own four dependencies, not the handler.void deployagainst Cloudflare, a MySQL database (the wrapper cases run the MySQL wrapper in Node), and Windows.🤖 Generated with Claude Code