fix(client): stop native fetch from throwing Illegal invocation in browsers - #2112
Merged
dinwwwh merged 1 commit intoSep 28, 2026
Merged
Conversation
…owsers The fetch link transport called the custom `fetch` option as `this.fetch(...)`, so passing `fetch: globalThis.fetch` or `window.fetch` directly ran the native fetch with the transport as its receiver, which browsers reject. Call it without a receiver instead.
Contributor
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
- Receiver-free custom fetch —
FetchLinkTransport.sendnow hoiststhis.fetchinto a local before calling it, so native browserfetchno longer receives the transport instance asthis. In strict-mode ES modules the bare call yieldsthis === undefined, which is the accepted form for globalfetch; the default(globalThis.fetch)(...)wrapper was already correct. - Regression test — asserts
fetch.mock.contextsequals[undefined]. This genuinely pins the bug: the previousthis.fetch(...)invocation records the transport instance as context, so the test fails before the fix and passes after. - Coverage —
@orpc/openapi'sOpenAPILinkreuses the sameFetchLinkTransport, so it inherits the fix; a repo-wide search found no other site that invokes a customfetchas a method.
DeepSeek Flash (free via Pullfrog for OSS) | 𝕏
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
More templates
@orpc/ai-sdk
@orpc/arktype
@orpc/bun
@orpc/client
@orpc/cloudflare
@orpc/contract
@orpc/experimental-effect
@orpc/evlog
@orpc/hibernation
@orpc/json-schema
@orpc/experimental-lock
@orpc/experimental-msw
@orpc/nest
@orpc/next
@orpc/node
@orpc/openapi
@orpc/opentelemetry
@orpc/pinia-colada
@orpc/pino
@orpc/publisher
@orpc/ratelimit
@orpc/server
@orpc/shared
@orpc/swr
@orpc/tanstack-query
@orpc/trpc
@orpc/valibot
@orpc/zod
commit: |
Contributor
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.

Passing the browser's native fetch straight to a link, such as
fetch: globalThis.fetchorfetch: window.fetch, no longer fails every request withTypeError: Failed to execute 'fetch' on 'Window': Illegal invocation. The transport now calls the customfetchwithout a receiver, soRPCLinkandOpenAPILinkboth work with native fetch passed directly.Fixes
fetchoption was called as a method on the transport. Browsers reject nativefetchwhen itsthisis anything other than the global object. Node's fetch does not checkthis, so existing tests never caught this.(url, init) => globalThis.fetch(url, init), which the docs use, already worked and are unaffected.Testing
globalThis.fetchandwindow.fetchthrew Illegal invocation. After the fix, all three return the response.thisset toundefined. It fails with the oldthis.fetch(...)call and passes with the fix.@orpc/clienttype:check pass.