Skip to content

fix(astro): apply the request scope and dev triggers to entries that call handle() - #951

Open
morinokami wants to merge 2 commits into
voidzero-dev:mainfrom
morinokami:fix/astro-handle-worker-entry
Open

morinokami wants to merge 2 commits into
voidzero-dev:mainfrom
morinokami:fix/astro-handle-worker-entry

Conversation

@morinokami

Copy link
Copy Markdown
Collaborator

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:

// void.config.ts
import { defineConfig } from 'void/config';

export default defineConfig({
  database: 'pg',
  cloudflare: {
    name: 'my-app',
    main: './src/worker.ts',
    compatibility_date: '2026-02-24',
    compatibility_flags: ['nodejs_compat', 'nodejs_als'],
  },
});
// src/worker.ts
import { handle } from '@astrojs/cloudflare/handler';

export default {
  fetch(request, env, ctx) {
    return handle(request, env, ctx);
  },
} satisfies ExportedHandler<Env>;

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:

// src/pages/api/user-counts.ts
import { users } from '@schema';
import { db } from 'void/db';

export const prerender = false;

const allUsers = db.select().from(users);

export async function GET() {
  const results = await Promise.all([allUsers, allUsers, allUsers, allUsers]);
  return Response.json(results.map((rows) => rows.length));
}

With the adapter's own entry, the same request opens one. This happens in astro dev and in the built Worker, and nothing warns about it.

In dev, with a src/middleware.ts of its own, the app also has no trigger endpoints. The request that Void's dev hint prints reaches Astro, which answers 404:

curl -X POST http://localhost:4321/__void/scheduled \
  -H "x-void-dev-trigger: <token>" \
  -d '{"cron":"*/5 * * * *","scheduledTime":0}'

#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/handler to a module that exports a wrapped handle(). Both wrappers come from the same code.

  • The adapter's server entry imports handle by 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.
  • The module's other exports pass through.
  • A D1 app gets no wrapper in a build, and in dev only next to its own middleware, as before.

Vite's dependency scan can't see through the wrapper, which is a \0 module, so it never records @astrojs/cloudflare/handler. The optimizer of the Worker environment then ran a second time on the first astro dev start of an app whose entry is wrapped. The plugin now names the module for that optimizer, as the adapter names its server entry.

  • It is named in dev for every Astro app, with or without an entry of its own. The adapter's server entry already brings in the same code.
  • The server that astro build and astro sync start 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:

  • It used to appear for every such app. It now appears only when the entry's source names neither @astrojs/cloudflare/handler nor @astrojs/cloudflare/entrypoints/server.
  • It says that the triggers work when the entry serves requests with either, where it used to ask for the server entry.
  • Only the entry's own source is read. An entry that leaves the call to another module of the app still gets the warning, and so does one that can't be read.

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.md records that handle() 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 the handle() 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/scheduled next to the app's own middleware. test/integration-timings.ts raises 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.
    • In dev, both resolve to their wrapper for PostgreSQL, MySQL, and a D1 app with its own middleware, and each wrapper's own import resolves to the adapter's module.
    • Each wrapper, run in Node, answers inside a request scope for PostgreSQL and MySQL, and passes the module's other exports through.
    • Each wrapper sends a dev trigger to the generated dispatch before Astro, and a build has neither the dispatch nor, for D1, the wrapper.
    • With a stand-in for the adapter placed after Void's plugins, 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 calls handle() or the server entry's fetch() gets no warning, and one built on cf() or one that can't be read gets it.

14 of the 30 cases fail without this fix. The other 16 check what main already 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.
  • By hand on a copy of playground/astro (D1) with a middleware of its own, in astro dev:
    • With the handle() entry above: pages answer 200, POST /__void/scheduled answers {"ok":true}, and no warning appears.
    • With an entry that calls fetch() on the server entry's default export: the same.
    • With an entry built on cf(): the warning appears, and POST /__void/scheduled answers 404.
  • By hand on the same copy with DEBUG=vite:deps and the handle() entry:
    • The optimizer of the Worker environment runs once on the first start ("the scanner found every used dependency") and reuses its cache on the second. Without the include, it ran twice on the first start.
    • After astro sync, the type-generation server has prebundled only Void's own four dependencies, not the handler.
  • Not run: void deploy against Cloudflare, a MySQL database (the wrapper cases run the MySQL wrapper in Node), and Windows.

🤖 Generated with Claude Code

morinokami and others added 2 commits November 28, 2026 16:42
…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
morinokami marked this pull request as ready for review November 28, 2026 07:57

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