Repository navigation
fix(void): let app CORS middleware answer preflights in dev and preview - #997
Open
morinokami wants to merge 3 commits into
Open
morinokami wants to merge 3 commits into
morinokami wants to merge 3 commits into
Conversation
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]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.
The Hono guide puts Hono's
cors()in a global middleware file:In
vp devandvp preview, this middleware never sees a preflight request. Vite answers everyOPTIONSrequest with its own CORS settings before any plugin's middleware runs:This preflight, the same one from
http://localhost:3000, and aGETfromhttp://localhost:3000get these answers:mainhttps://example.com204withoutAccess-Control-Allow-Origin204withAccess-Control-Allow-Origin: https://example.comhttp://localhost:3000204withAccess-Control-Allow-Origin: http://localhost:3000204without itGETfromhttp://localhost:3000200withAccess-Control-Allow-Origin: http://localhost:3000200without itOn
main, both preflights also list thecorspackage's default methods (GET,HEAD,PUT,PATCH,POST,DELETE), where Hono'scors()lists its own (GET,HEAD,PUT,POST,DELETE,PATCH,QUERY).So in dev and preview, unlike in the deployed Worker:
PUTorDELETE, fail from allowed origins outside localhost.cors()options, such ascredentials,allowMethods,allowHeadersandmaxAge.Vite adds its CORS middleware, the
corspackage, ahead of every plugin's middleware in both servers. That middleware ends eachOPTIONSrequest with204instead of passing it on, and Vite's defaultserver.corsallows localhost origins only. Void never setserver.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, thevoid:envconfig hook sets it tofalsein Void apps. Vite then adds no CORS middleware, and every request, preflights included, reaches the app.vp previewusespreview.corswhen it is set andserver.corsotherwise, so preview gets the samefalse. Void never setspreview.cors, so either setting from the config applies as Vite documents./@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.corsturns Vite's CORS back on.Docs
docs/integrations/hono.md: under the CORS example, a paragraph says that the middleware answers CORS requests invp devandvp previewas in the deployed app, and that turning on Vite's CORS handling withserver.corsmakes Vite answer preflights first. It also says thatvp previewusespreview.corswhen it is set andserver.corsotherwise.packages/void/AGENTS.mdrecords the decision and its tests. The notes on the restart toast's token, there and inplugin-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'smiddleware/01.cors.ts. A preflight fromhttps://example.comgetsAccess-Control-Allow-Origin: https://example.com, and a preflight and aGETfromhttp://localhost:3000get none.@hono/node-server, whichpackages/voiddoesn't install.packages/void/test/unit/plugin-dev-cors.test.ts(8 cases): theserver.corsandpreview.corsthat Vite resolves with Void's plugins.falsein dev and in preview, for a Cloudflare app and for a Node app.server.corsfrom the config,trueor an options object, applies to both.preview.corsfrom the config applies, andserver.corsstaysfalse.7 of the 10 cases fail without this fix. The other 3 check what
mainalready does: aserver.corsfrom 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.playground/ssrwith the guide'smiddleware/01.cors.ts, throughvp dev, and throughvp previewaftervp build: the answers in the "This PR" column, with Hono's method list in both preflights.main, the integration test's servers (dev and preview of a Cloudflare app, dev of a Node app) gave the answers in themaincolumn.🤖 Generated with Claude Code