[w3c/ServiceWorker] fetch() file stream fails when passing through fetch handler (#1246)

I am trying to stream a selected file using `fetch()` and when the serviceworker fetch handler intercepts the fetch it make the fetch fail. If I don't register the fetch handler then the fetch succeeds. How can I get a file to stream through a fetch while I'm online and using a service worker?

Error:
```
Uncaught (in promise) TypeError: Failed to fetch
PUT https://example.com/upload net::ERR_FAILED
```
HTML:
```html
<input type='file' onchange='upload(event)'>
```
Javascript:
```js
navigator.serviceWorker.register('service.js')

async function upload(event) {
  let file = event.target.files[0];
  let res = await fetch('upload', {
    method: 'PUT',
    headers: {
      'Content-Type': file.type || 'application/octet-stream'
    },
    body: file,
    mode: 'same-origin',
    credentials: 'same-origin'
  });
}
```
service.js:
```js
onfetch = event => {event.respondWith(fetchHandler(event))};

async function fetchHandler(event) {
  let response = await caches.match(event.request); // 'upload' not in cache
  return response || fetch(event.request);
}
```

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/w3c/ServiceWorker/issues/1246

Received on Monday, 11 December 2017 10:56:56 UTC