Re: [w3c/ServiceWorker] Handing fetch termination (#1178)

Hi all, sorry for resurrecting this old thread. Wondering if this behavior that I'm noticing is the one as intended according to the spec, as I'm noticing the same thing on all major browsers (Firefox, Chromium, Safari).

It seems that this doesn't catch situations where the browser itself is terminating a request.

Example:

### index.html

```html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Test fetch</title>
</head>
<body>

<button onclick="getVideo()">Get video</button>
<button onclick="stop()">Stop</button>
<div id="out"></div>

<script>
;(async function() {
    await navigator.serviceWorker.register('sw.js')
    console.info('Service worker registered')
    await navigator.serviceWorker.ready
    console.log('Service worker installed')
    document.getElementById('out').innerText = 'Ready'
})()

function getVideo() {
    document.getElementById('out').innerHTML = '<video autoplay controls  muted="muted" src="video.mp4" style="width: 400px; height: auto;"></video>'
}

function stop() {
    document.getElementById('out').innerHTML = ''
}
</script>

</body>
</html>
```

### sw.js

```js
self.addEventListener('install', () => {
    self.skipWaiting()
})

self.addEventListener('fetch', (event) => {
    console.log('fetch', event)
    event.request.signal.addEventListener('abort', (event) => {
        console.log('aborted', event)
    })
})

self.addEventListener('activate', () => self.clients.claim())
```

### Description

Place a video file in the same folder called `video.mp4`, ideally a large-r file (say, 100MB+). Start a server (e.g. `docker run --rm --name some-nginx -v $PWD:/usr/share/nginx/html:ro -p 8080:80 nginx`) and navigate to the page (http://localhost:8080).

Start playing the video (hint: it helps to throttle the network), then press the "stop" button before the video is done loading. Alternatively, skip forward in the video to a part that hasn't been buffered yet.

In the dev tools, you'll see the browser interrupted the old request (and made a new one if necessary).

However, the `abort` event is never triggered. And, in the console, even after the request is done, the `signal` objected nested into `event` still reports the request as not aborted.

Is this how it's supposed to behave according to specs? Or is it a bug in the browsers?

Why am I doing this? I have a SW that intercepts a request and makes a new one instead. I need to be able to understand if the browser has canceled the "original" request to cancel the new one too.

-- 
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/pull/1178#issuecomment-704664905

Received on Wednesday, 7 October 2020 03:09:59 UTC