Skip to content

fix: respect user Vite CORS config - #16924

Merged
elliott-with-the-longest-name-on-github merged 5 commits into
version-3from
teemingc-fix-log-overridden-cors
Aug 25, 2026
Merged

elliott-with-the-longest-name-on-github merged 5 commits into
version-3from
teemingc-fix-log-overridden-cors

Conversation

@teemingc

@teemingc teemingc commented Aug 24, 2026 •

Copy link
Copy Markdown
Member

closes #12574

SvelteKit currently silently overrides user-provided server.cors and preview.cors values to ensure OPTIONS handlers work during development and preview. This change preserves explicit user configuration instead, while continuing to add preflightContinue: true when it has not been configured. A warning explains that OPTIONS handlers require preflightContinue: true when the user's CORS setting disables it.


Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Targeted verification: pnpm -F @sveltejs/kit test:unit (748 passed, 110 skipped).

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Edits

  • Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@pkg-svelte-dev

pkg-svelte-dev Bot commented Aug 24, 2026 •

Copy link
Copy Markdown

Install the latest version of @sveltejs/kit from 136c14a:

pnpm add https://pkg.svelte.dev/@sveltejs/kit/c/136c14a9fc0b3f1ce3c73bf666f49fdcbe0035e1

Open in pkg.svelte.dev: https://pkg.svelte.dev/repos/kit/pr/16924

@changeset-bot

changeset-bot Bot commented Aug 24, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 136c14a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@teemingc teemingc added the vite label Aug 24, 2026
@svelte-docs-bot

Copy link
Copy Markdown

Comment thread packages/kit/src/exports/vite/index.js Outdated
teemingc and others added 2 commits August 24, 2026 17:34
Co-authored-by: vercel[bot] <35613825+vercel[bot]@users.noreply.github.com>
Comment thread packages/kit/src/exports/vite/index.js Outdated
…ndlers during `vite build`, even though `server.cors`/`preview.cors` are irrelevant to building.

This commit fixes the issue reported at packages/kit/src/exports/vite/index.js:51

## Bug

In `packages/kit/src/exports/vite/index.js`, the plugin's `config` hook always builds and returns `new_config` regardless of `config_env.command`. It sets `is_build = config_env.command === 'build'` (line 335) but never uses it to gate CORS resolution:

```js
server: {
  cors: resolve_cors(config.server?.cors, 'server.cors'),
  ...
},
preview: {
  cors: resolve_cors(config.preview?.cors, 'preview.cors')
},
```

`resolve_cors` emits a `console.warn` whenever preflight is disabled:

```js
if (preflight_disabled) {
  console.warn(styleText(['yellow', 'bold'],
    `OPTIONS request handlers will not work unless ...`));
  return undefined;
}
```

### Trigger

A user who sets `server.cors: false` (or `true`, or an object with `preflightContinue: false`) in `vite.config.js` and runs **`vite build`** sees this yellow warning. During `build`, neither `server.cors` (dev) nor `preview.cors` (preview) has any effect, so the warning is pure noise unrelated to building.

## Fix

Added a `warn` parameter to `resolve_cors` and gated the `console.warn` on it. The two call sites pass `!is_build`, so the warning only fires for `serve`/`preview` commands where the CORS setting is actually used. The returned config value is unchanged, so build behavior is unaffected.

*(Rebased against the formatting-only commit `d2bde94`; `resolve_cors` is now defined at line 51 and the `preflight_disabled` check was collapsed onto a single line, but the warning remains ungated.)*

Co-authored-by: Vercel <vercel[bot]@users.noreply.github.com>
Co-authored-by: teemingc <chewteeming01@gmail.com>
Comment thread packages/kit/src/exports/vite/index.js
Co-authored-by: Nic Polumeyv <nicolas.polum@outlook.com>

@Nic-Polumeyv Nic-Polumeyv left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

@elliott-with-the-longest-name-on-github
elliott-with-the-longest-name-on-github merged commit 84411cc into version-3 Aug 25, 2026
40 checks passed
@elliott-with-the-longest-name-on-github
elliott-with-the-longest-name-on-github deleted the teemingc-fix-log-overridden-cors branch August 25, 2026 21:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Logging for overridden config in vite does not include cors options for dev and preview

3 participants