Skip to content

Response contructor is incompatible with other browser implementations #576

Closed

Description

@spearmootz

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.

Activity

  1. spearmootz commented on Oct 23, 2017

    @spearmootz
    Author

    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);
      }
    }
  2. mislav commented on Oct 27, 2017

    @mislav
    Contributor

    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?

  3. spearmootz commented on Oct 27, 2017

    @spearmootz
    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

  4. changed the title [-]new Response contructor is incompatible with other browser implementations[/-] [+]Response contructor is incompatible with other browser implementations[/+] on Oct 28, 2017
  5. dgraham commented on Oct 28, 2017

    @dgraham
    Contributor

    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

  6. added a commit that references this issue on Sep 3, 2018
    08bc742
  7. locked as resolved and limited conversation to collaborators on Oct 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions