Skip to content

fix: make Mode::default() cors, like fetch, and deprecate Mode::Navigate - #121

Open
QuantumExplorer wants to merge 1 commit into
devashishdxt:mainfrom
QuantumExplorer:fix/mode-default-cors
Open

QuantumExplorer wants to merge 1 commit into
devashishdxt:mainfrom
QuantumExplorer:fix/mode-default-cors

Conversation

@QuantumExplorer

Copy link
Copy Markdown

Fixes #107.

Problem

Mode::default() is NoCors. fetch's default mode is cors, and a grpc-web call does not work in no-cors mode across origins, because its headers are not CORS-safelisted and the response is opaque. FetchOptions::new().mode(Mode::default()) makes every call fail with JS API error: TypeError: Failed to fetch. Mode::Navigate can never work either, since fetch rejects every request whose mode is navigate. #107 has a reproduction for both.

Fix

  • Cors is the #[default] variant, matching fetch. The NoCors docs say why a grpc-web call cannot work in that mode.
  • Navigate is deprecated rather than removed, so code that names it keeps compiling.

FetchOptions::default() leaves the mode unset, so only code that asks for Mode::default() sees a change.

Before and after

Code Before After
FetchOptions::new().mode(Mode::default()) no-cors, every call fails with Failed to fetch cors, calls work
FetchOptions::new() mode unset, the browser's default (cors) unchanged
FetchOptions::new().mode(Mode::Navigate) every call fails every call fails, and the compiler warns that Navigate is deprecated

Tests

Unit test default_is_cors_like_fetch in src/options/mode.rs.

cargo fmt --check, cargo clippy --all-targets -- -D warnings, cargo clippy --target wasm32-unknown-unknown -- -D warnings and cargo test are clean, and the browser suite in test-suite/simple passes in headless Chrome 153.

This is one of 14 PRs from the review asked for in #97. They are independent, and all 14 also merge together cleanly; with all of them merged, both browser suites (test-suite/simple and test-suite/gzip) pass.

🤖 Generated with Claude Code

Mode::default() was NoCors. fetch's default mode is cors, and a
grpc-web call cannot work in no-cors mode across origins: its headers
are not CORS-safelisted and the response is opaque, so every call made
with FetchOptions::new().mode(Mode::default()) failed with "Failed to
fetch". FetchOptions leaves the mode unset by default, so only code that
asks for Mode::default() is affected.

Mode::Navigate can never work: fetch rejects every request whose mode is
navigate. It is deprecated rather than removed, so code that names it
keeps compiling.

Fixes devashishdxt#107

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

Mode::default() is NoCors, which makes every call fail, and Mode::Navigate can never work

1 participant