Skip to content

fix(void): let app CORS middleware answer preflights in dev and preview - #997

Open
morinokami wants to merge 3 commits into
voidzero-dev:mainfrom
morinokami:fix/dev-cors-preflight
Open

morinokami wants to merge 3 commits into
voidzero-dev:mainfrom
morinokami:fix/dev-cors-preflight

Conversation

@morinokami

@morinokami morinokami commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

The Hono guide puts Hono's cors() in a global middleware file:

// middleware/01.cors.ts
import { cors } from 'hono/cors';
import { defineMiddleware } from 'void';

export default defineMiddleware(cors({ origin: 'https://example.com' }));

In vp dev and vp preview, this middleware never sees a preflight request. Vite answers every OPTIONS request with its own CORS settings before any plugin's middleware runs:

curl -i -X OPTIONS http://localhost:5173/api/hello \
  -H 'Origin: https://example.com' \
  -H 'Access-Control-Request-Method: POST' \
  -H 'Access-Control-Request-Headers: content-type'

This preflight, the same one from http://localhost:3000, and a GET from http://localhost:3000 get these answers:

Request main This PR
Preflight from https://example.com 204 without Access-Control-Allow-Origin 204 with Access-Control-Allow-Origin: https://example.com
Preflight from http://localhost:3000 204 with Access-Control-Allow-Origin: http://localhost:3000 204 without it
GET from http://localhost:3000 200 with Access-Control-Allow-Origin: http://localhost:3000 200 without it

On main, both preflights also list the cors package's default methods (GET,HEAD,PUT,PATCH,POST,DELETE), where Hono's cors() lists its own (GET,HEAD,PUT,POST,DELETE,PATCH,QUERY).

So in dev and preview, unlike in the deployed Worker:

  • Requests that need a preflight, such as a JSON body, a custom header, PUT or DELETE, fail from allowed origins outside localhost.
  • Every localhost origin passes, whatever the app allows, so a CORS setting that production rejects works locally.
  • Preflights ignore the app's cors() options, such as credentials, allowMethods, allowHeaders and maxAge.

Vite adds its CORS middleware, the cors package, ahead of every plugin's middleware in both servers. That middleware ends each OPTIONS request with 204 instead of passing it on, and Vite's default server.cors allows localhost origins only. Void never set server.cors, so neither the Cloudflare plugin's Worker handler nor the Node target's Hono middleware ever received a preflight.

Fix

When the config doesn't set server.cors, the void:env config hook sets it to false in Void apps. Vite then adds no CORS middleware, and every request, preflights included, reaches the app.

  • vp preview uses preview.cors when it is set and server.cors otherwise, so preview gets the same false. Void never sets preview.cors, so either setting from the config applies as Vite documents.
  • React Router, TanStack Start and vinext apps keep Vite's default, since the framework owns their HTTP layer and Void documents no CORS setup for them. SvelteKit, Nuxt, Analog and Astro apps don't run this hook.
  • Modules that Vite serves itself, such as /@vite/client, no longer carry CORS headers for other localhost origins. Only a page on another origin that loads modules from the dev server notices. server.cors turns Vite's CORS back on.

Docs

  • docs/integrations/hono.md: under the CORS example, a paragraph says that the middleware answers CORS requests in vp dev and vp preview as in the deployed app, and that turning on Vite's CORS handling with server.cors makes Vite answer preflights first. It also says that vp preview uses preview.cors when it is set and server.cors otherwise.
  • packages/void/AGENTS.md records the decision and its tests. The notes on the restart toast's token, there and in plugin-restart.ts, add that Void apps turn Vite's CORS middleware off by default.

Tests

  • packages/void/test/integration/dev-cors.test.ts (2 cases): an app with the guide's middleware/01.cors.ts. A preflight from https://example.com gets Access-Control-Allow-Origin: https://example.com, and a preflight and a GET from http://localhost:3000 get none.
    • A Cloudflare app, in dev and in preview after a build.
    • A Node app, in dev. Its preview isn't covered: the built server imports @hono/node-server, which packages/void doesn't install.
  • packages/void/test/unit/plugin-dev-cors.test.ts (8 cases): the server.cors and preview.cors that Vite resolves with Void's plugins.
    • Both are false in dev and in preview, for a Cloudflare app and for a Node app.
    • A server.cors from the config, true or an options object, applies to both.
    • In preview, a preview.cors from the config applies, and server.cors stays false.
    • A framework app keeps Vite's default.

7 of the 10 cases fail without this fix. The other 3 check what main already does: a server.cors from the config, and a framework app's default.

Verification

  • vp run test: builds the packages and runs the platform, unit and integration groups; 190/190 tasks pass.
  • vp run check:types: 36/36 pass.
  • vp check: passes.
  • By hand on a copy of playground/ssr with the guide's middleware/01.cors.ts, through vp dev, and through vp preview after vp build: the answers in the "This PR" column, with Hono's method list in both preflights.
  • On main, the integration test's servers (dev and preview of a Cloudflare app, dev of a Node app) gave the answers in the main column.
  • Not run: a browser, a framework app, a deployed Worker, and Windows.

🤖 Generated with Claude Code

morinokami and others added 3 commits December 2, 2026 00:39
Vite adds its own CORS middleware ahead of every plugin middleware and
ends each OPTIONS request there, so in `vp dev` and `vp preview` no
preflight reached a Void app. A `cors()` in `middleware/` never ran for
them: the origins it allows failed the preflight, any localhost origin
passed, and options such as `credentials` had no effect, unlike in the
deployed Worker. Node targets had the same problem.

The `void:env` config hook now sets `server.cors: false` for Void apps
unless the config sets `server.cors`. Preview falls back to
`server.cors`, so it follows, and a `preview.cors` from the config
still applies. Class A framework apps keep Vite's default.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The unit test now checks the `server.cors` and `preview.cors` that Vite
resolves, including the fallback to `server.cors` and an explicit
`preview.cors`, instead of the config hook's return shape. The
integration fixture uses `createPackageFixtureDir()`, so it resolves
`hono` without a link, and the Node fixture drops the `worker` settings.

The Hono guide now says that Vite answers preflights first only when
`server.cors` turns its CORS handling on, and that `vp preview` uses
`preview.cors` when it is set and `server.cors` otherwise. The restart
toast comment and the package guide now say only that Void apps turn
Vite's CORS middleware off by default, since `server.headers` can still
add CORS headers.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

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