Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: When headers is native Headers object, they won't get send #860

Closed
FokkeZB opened this issue Nov 13, 2020 · 3 comments
Closed

Bug: When headers is native Headers object, they won't get send #860

FokkeZB opened this issue Nov 13, 2020 · 3 comments

Comments

@FokkeZB
Copy link

FokkeZB commented Nov 13, 2020

Steps to Reproduce

Code: https://codesandbox.io/s/determined-cdn-7k3gx?file=/src/index.js
App: https://7k3gx.csb.app/

Theories

We do new Headers(options.headers):

https://github.com/github/fetch/blob/75d9455d380f365701151f3ac85c5bda4bbbde76/fetch.js#L367-L369

Which will use the local polyfill function Headers which is also what this line will check against:

https://github.com/github/fetch/blob/75d9455d380f365701151f3ac85c5bda4bbbde76/fetch.js#L86-L90

But if options.headers is the builtin Headers object, the instanceof check will fail, as well as the fallback case because getOwnPropertyNames() will return an empty object:

https://github.com/github/fetch/blob/75d9455d380f365701151f3ac85c5bda4bbbde76/fetch.js#L94-L98

Solution

Don't polyfill Headers when there's a builtin, native version.

@apisim
Copy link

apisim commented Jan 18, 2021

I came across the problem, too. Before I saw this Issue, it took a few debugging sessions stepping through the code to discover that, indeed, the if (headers instanceof Headers) checks fails when unknowingly using the native Headers object.

Attempting to use an Array doesn't work neither because !(init.headers instanceof Headers) in
https://github.com/github/fetch/blob/a8aa427de0ed808ff26c0e3eb2e59c122c44488a/fetch.js#L573-L581
...evaluates to true and the execution goes through the if block, and the header elements end up being somethig like 0: accept, application/json (0 is the index in the array) for something that should be accept: application/json.

What worked was to alias polyfill's Headers. For example:

import { Headers as FetchPolyfillHeaders } from 'whatwg-fetch';

...and use FetchPolyfillHeaders everywhere in the code.

Hope this helps.

@JakeChampion
Copy link
Owner

@FokkeZB and/or @apisim -- Which browser or runtime has Headers defined but does not have fetch defined?

JakeChampion pushed a commit that referenced this issue Jul 17, 2023
JakeChampion pushed a commit that referenced this issue Jul 17, 2023
JakeChampion pushed a commit that referenced this issue Jul 17, 2023
JakeChampion pushed a commit that referenced this issue Jul 18, 2023
JakeChampion pushed a commit that referenced this issue Jul 18, 2023
@JakeChampion
Copy link
Owner

Resolved via #1358

cr313 added a commit to cr313/fetch-Js-flow that referenced this issue Apr 19, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
@JakeChampion @FokkeZB @apisim and others