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

Response contructor is incompatible with other browser implementations #576

Closed
spearmootz opened this issue Oct 23, 2017 · 4 comments · Fixed by #651
Closed

Response contructor is incompatible with other browser implementations #576

spearmootz opened this issue Oct 23, 2017 · 4 comments · Fixed by #651

Comments

@spearmootz
Copy link

spearmootz commented Oct 23, 2017

if you use new Response directly and use 204 responses you get the following issue.

new Response('', Init)
fails on chrome because it expects no body. you can do

new Response(null, init)
fails on Firefox because body cannot be null

but you can do in both firefox, chrome, Edge, safari
new Response(init);

but that fails when using this polyfill.

@spearmootz
Copy link
Author

spearmootz commented Oct 23, 2017

i get around it with the following code.

const polyfilled = fetch.toString().includes('XMLHttpRequest');
if (polyfilled) {
  const getClassOf = Function.prototype.call.bind(Object.prototype.toString);
  const isClassOf = (instance, className) => getClassOf(instance).split(' ')[1].includes(className);
  const PolyfilledResponse = window.Response;
  window.Response = (first, second) => {
    if (isClassOf(first, 'Object')) {
      return new PolyfilledResponse(null, first);
    }

    return new PolyfilledResponse(first, second);
  }
}

@mislav
Copy link
Contributor

mislav commented Oct 27, 2017

Thanks for reporting! In the meantime, I think you can use new Response(undefined, {status: 204}) as a workaround in your app. It's a shame null doesn't work in Firefox; is this known to Mozilla?

@spearmootz
Copy link
Author

so my original assumption that doing new Response(options) works on other browsers. it actually returns a response that is a 200, no matter if you did 204 or 400. so it will always be resp.ok

@dgraham dgraham changed the title new Response contructor is incompatible with other browser implementations Response contructor is incompatible with other browser implementations Oct 28, 2017
@dgraham
Copy link
Contributor

dgraham commented Oct 28, 2017

Reopening to track updating our Response constructor to match the specification changes.

We need to handle the optional first BodyInit argument here.

https://github.com/github/fetch/blob/7a692dc519a3a110526a26c7e252f99991510c2a/fetch.js#L377-L380

@dgraham dgraham reopened this Oct 28, 2017
mislav added a commit that referenced this issue Sep 3, 2018
The polyfill used to throw an exception, but the spec dictates that
unsupported body types are simply converted to string.

Closes #576
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 2, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
3 participants