Possible bug in page Using Service Workers

Hello folks
I suspect a problem in this page : https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers
I understand I can edit it directly, but for my first contribution I’d prefer to discuss it first!

The snippet containing

  return resp || fetch(event.request).then(function(response) {
    caches.open('v1').then(function(cache) {
      cache.put(event.request, response.clone());
    });
    return response;
      });

yields this error (at least in Chrome) :

Uncaught (in promise) TypeError: Failed to execute ‘clone’ on ‘Response’: Response body is already used

I think a fine fix is found in the source of the linked demo https://mdn.github.io/sw-test/sw.js :

  return resp || fetch(event.request).then(function(response) {
    let responseClone = response.clone();
    caches.open('v1').then(function(cache) {
      cache.put(event.request, responseClone);
    });
    return response;
      });

So, I suggest I update the page.
What do you think?

Hi there @deleplace,

Yes, you are right. It looks like the code in the article has gotten out of sync with the code in the demo. If you are happy to update the page, then please go ahead — any help is much appreciated.

Best regards,

Chris

Thanks @chrisdavidmills
I edited it (though I had a lot of troubles in the preview, when adding a new line in the snippet)

Thanks!

I had a look, and edited the code block a bit. There was a lot of markup cruft in the source view. That’s one downside of it being a rich text editor - anything you copy from somewhere else brings all the styling with it.

One way to get round that is to copy it into a plain text editor, and then copy it out again before pasting it in.

you can choose “Paste as plain text” from the “context menu”. I think ( Ctrl + Shift + v) does the same.