- From: Jake Archibald <notifications@github.com>
- Date: Fri, 28 Jun 2019 01:43:34 -0700
- To: w3c/ServiceWorker <ServiceWorker@noreply.github.com>
- Cc: Subscribed <subscribed@noreply.github.com>
- Message-ID: <w3c/ServiceWorker/pull/1444/c506655298@github.com>
Here's how I think this works: (@littledan: correct me if I'm wrong)
When we run a service worker, we consider it as 'evaluated' and ready to receive events after synchronous execution + microtasks.
# Errors & evaluation
This error will prevent a service worker from installing:
```js
throw Error('…');
```
As will this:
```js
await undefined;
throw Error('…');
```
As will this:
```js
import './foo.js';
// If `./foo.js` is:
throw Error('…');
```
However, this error will not prevent a service worker from installing:
```js
await new Promise(r => setTimeout(r));
throw Error('…');
```
…as the error has happened after 'evaluation'.
# Adding listeners
```js
// This is fine
addEventListener('fetch', func);
// This is fine
await undefined;
addEventListener('fetch', func);
// This throws
await new Promise(r => setTimeout(r));
addEventListener('fetch', func);
```
However, the throw is asynchronous, so it wouldn't prevent install.
# Dispatching events
We consider the service worker ready to receive events once it's evaluated:
```js
addEventListener('fetch', () => {
console.log('A');
});
await new Promise(r => setTimeout(r, 5000));
console.log('B');
```
The console may log `A` before `B`.
# Concerns
```js
import './third-party-script-v1.js';
addEventListener('install', event => …);
addEventListener('fetch', event => …);
```
The above works as the developer expects. However, later, they update to `'./third-party-script-v2.js'`. This include a top-level await that waits beyond a microtask.
Now, their service worker updates successfully, but their `install` and `fetch` listeners are absent. They'll show as an error in the console, but the developer may miss them.
--
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/1444#issuecomment-506655298
Received on Friday, 28 June 2019 08:43:56 UTC