Skip to content

Commit 5c6d27b

Browse files
authored
Add FetchEvent.handled (#1397) (#1496)
1 parent 910cdd8 commit 5c6d27b

File tree

1 file changed

+50
-25
lines changed

1 file changed

+50
-25
lines changed

docs/index.bs

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ spec: infra;
4040
text: list;
4141
for: set; text: append
4242
for: list; text: append
43+
spec: webidl;
44+
type: dfn;
45+
text: resolve;
4346
</pre>
4447

4548
<pre class="anchors">
@@ -1484,6 +1487,7 @@ spec: webappsec-referrer-policy; urlPrefix: https://w3c.github.io/webappsec-refe
14841487
readonly attribute DOMString clientId;
14851488
readonly attribute DOMString resultingClientId;
14861489
readonly attribute DOMString replacesClientId;
1490+
readonly attribute Promise&lt;void&gt; handled;
14871491

14881492
void respondWith(Promise&lt;Response&gt; r);
14891493
};
@@ -1495,6 +1499,7 @@ spec: webappsec-referrer-policy; urlPrefix: https://w3c.github.io/webappsec-refe
14951499
DOMString clientId = "";
14961500
DOMString resultingClientId = "";
14971501
DOMString replacesClientId = "";
1502+
Promise&lt;void&gt; handled;
14981503
};
14991504
</pre>
15001505

@@ -1536,6 +1541,12 @@ spec: webappsec-referrer-policy; urlPrefix: https://w3c.github.io/webappsec-refe
15361541
<dfn attribute for="FetchEvent"><code>replacesClientId</code></dfn> attribute *must* return the value it was initialized to. When an <a>event</a> is created the attribute *must* be initialized to the empty string.
15371542
</section>
15381543

1544+
<section>
1545+
<h4 id="fetch-event-handled">{{FetchEvent/handled|event.handled}}</h4>
1546+
1547+
<dfn attribute for="FetchEvent"><code>handled</code></dfn> attribute *must* return the value it was initialized to. When an <a>event</a> is created the attribute *must* be initialized to a pending [=promise=].
1548+
</section>
1549+
15391550
<section algorithm="fetch-event-respondwith">
15401551
<h4 id="fetch-event-respondwith">{{FetchEvent/respondWith(r)|event.respondWith(r)}}</h4>
15411552

@@ -2932,6 +2943,8 @@ spec: webappsec-referrer-policy; urlPrefix: https://w3c.github.io/webappsec-refe
29322943
1. Let |client| be |request|'s [=request/client=].
29332944
1. Let |reservedClient| be |request|'s [=request/reserved client=].
29342945
1. Let |preloadResponse| be a new [=promise=].
2946+
1. Let |workerRealm| be null.
2947+
1. Let |eventHandled| be null.
29352948
1. Let |fetchInstance| be the instance of the [=/fetch=] algorithm representing the ongoing fetch.
29362949
1. Assert: |request|'s [=request/destination=] is not "<code>serviceworker</code>".
29372950
1. If |request|'s [=request/destination=] is either "<code>embed</code>" or "<code>object</code>", then:
@@ -2982,34 +2995,46 @@ spec: webappsec-referrer-policy; urlPrefix: https://w3c.github.io/webappsec-refe
29822995
1. Return null.
29832996
1. If |activeWorker|'s <a>state</a> is "`activating`", wait for |activeWorker|'s <a>state</a> to become "`activated`".
29842997
1. If the result of running the [=Run Service Worker=] algorithm with |activeWorker| is *failure*, then set |handleFetchFailed| to true.
2985-
1. Else [=queue a task=] |task| to run the following substeps:
2986-
1. Let |e| be the result of <a>creating an event</a> with {{FetchEvent}}.
2987-
1. Let |requestObject| be a new {{Request}} object associated with |request| and a new associated {{Headers}} object whose [=guard=] is "`immutable`".
2988-
1. Initialize |e|’s {{Event/type}} attribute to {{fetch!!event}}.
2989-
1. Initialize |e|’s {{Event/cancelable}} attribute to true.
2990-
1. Initialize |e|’s {{FetchEvent/request}} attribute to |requestObject|.
2991-
1. Initialize |e|’s {{FetchEvent/preloadResponse}} to |preloadResponse|.
2992-
1. Initialize |e|'s {{FetchEvent/clientId}} attribute to |client|'s [=environment/id=].
2993-
1. If |request| is a <a>non-subresource request</a> and |request|'s [=request/destination=] is not {{RequestDestination/"report"}}, initialize |e|'s {{FetchEvent/resultingClientId}} attribute to |reservedClient|'s [=environment/id=], and to the empty string otherwise.
2994-
1. If |request| is a <a>navigation request</a>, initialize |e|'s {{FetchEvent/replacesClientId}} attribute to |request|'s [=request/replaces client id=], and to the empty string otherwise.
2995-
1. <a>Dispatch</a> |e| at |activeWorker|'s [=service worker/global object=].
2996-
1. Invoke [=Update Service Worker Extended Events Set=] with |activeWorker| and |e|.
2997-
1. If |e|'s [=FetchEvent/respond-with entered flag=] is set, set |respondWithEntered| to true.
2998-
1. If |e|'s [=FetchEvent/wait to respond flag=] is set, then:
2999-
1. Wait until |e|'s [=FetchEvent/wait to respond flag=] is unset.
3000-
1. If |e|'s [=FetchEvent/respond-with error flag=] is set, set |handleFetchFailed| to true.
3001-
1. Else, set |response| to |e|'s [=FetchEvent/potential response=].
3002-
1. If |e|'s <a>canceled flag</a> is set, set |eventCanceled| to true.
3003-
1. If |fetchInstance| is [=fetch/terminated=], then [=queue a task=] to [=AbortSignal/signal abort=] on |requestObject|'s {{Request/signal}}.
3004-
3005-
If |task| is discarded, set |handleFetchFailed| to true.
3006-
3007-
The |task| *must* use |activeWorker|'s <a>event loop</a> and the <a>handle fetch task source</a>.
2998+
1. Else:
2999+
1. Set |workerRealm| to the [=relevant realm=] of the |activeWorker|'s [=service worker/global object=].
3000+
1. Set |eventHandled| to [=a new promise=] in |workerRealm|.
3001+
1. [=Queue a task=] |task| to run the following substeps:
3002+
1. Let |e| be the result of <a>creating an event</a> with {{FetchEvent}}.
3003+
1. Let |requestObject| be a new {{Request}} object associated with |request| and a new associated {{Headers}} object whose [=guard=] is "`immutable`".
3004+
1. Initialize |e|’s {{Event/type}} attribute to {{fetch!!event}}.
3005+
1. Initialize |e|’s {{Event/cancelable}} attribute to true.
3006+
1. Initialize |e|’s {{FetchEvent/request}} attribute to |requestObject|.
3007+
1. Initialize |e|’s {{FetchEvent/preloadResponse}} to |preloadResponse|.
3008+
1. Initialize |e|'s {{FetchEvent/clientId}} attribute to |client|'s [=environment/id=].
3009+
1. If |request| is a <a>non-subresource request</a> and |request|'s [=request/destination=] is not {{RequestDestination/"report"}}, initialize |e|'s {{FetchEvent/resultingClientId}} attribute to |reservedClient|'s [=environment/id=], and to the empty string otherwise.
3010+
1. If |request| is a <a>navigation request</a>, initialize |e|'s {{FetchEvent/replacesClientId}} attribute to |request|'s [=request/replaces client id=], and to the empty string otherwise.
3011+
1. Initialize |e|’s {{FetchEvent/handled}} to |eventHandled|.
3012+
1. <a>Dispatch</a> |e| at |activeWorker|'s [=service worker/global object=].
3013+
1. Invoke [=Update Service Worker Extended Events Set=] with |activeWorker| and |e|.
3014+
1. If |e|'s [=FetchEvent/respond-with entered flag=] is set, set |respondWithEntered| to true.
3015+
1. If |e|'s [=FetchEvent/wait to respond flag=] is set, then:
3016+
1. Wait until |e|'s [=FetchEvent/wait to respond flag=] is unset.
3017+
1. If |e|'s [=FetchEvent/respond-with error flag=] is set, set |handleFetchFailed| to true.
3018+
1. Else, set |response| to |e|'s [=FetchEvent/potential response=].
3019+
1. If |e|'s <a>canceled flag</a> is set, set |eventCanceled| to true.
3020+
1. If |fetchInstance| is [=fetch/terminated=], then [=queue a task=] to [=AbortSignal/signal abort=] on |requestObject|'s {{Request/signal}}.
3021+
3022+
If |task| is discarded, set |handleFetchFailed| to true.
3023+
3024+
The |task| *must* use |activeWorker|'s <a>event loop</a> and the <a>handle fetch task source</a>.
30083025

30093026
1. Wait for |task| to have executed or for |handleFetchFailed| to be true.
30103027
1. If |shouldSoftUpdate| is true, then [=in parallel=] run the [=Soft Update=] algorithm with |registration|.
3011-
1. If |respondWithEntered| is false, then return a [=network error=] if |eventCanceled| is true and null otherwise.
3012-
1. If |handleFetchFailed| is true, then return a [=network error=].
3028+
1. If |respondWithEntered| is false, then:
3029+
1. If |eventCanceled| is true, then:
3030+
1. If |eventHandled| is not null, then [=reject=] |eventHandled| with a "{{NetworkError}}" {{DOMException}} in |workerRealm|.
3031+
2. Return a [=network error=].
3032+
1. If |eventHandled| is not null, then [=resolve=] |eventHandled|.
3033+
1. Return null.
3034+
1. If |handleFetchFailed| is true, then:
3035+
1. If |eventHandled| is not null, then [=reject=] |eventHandled| with a "{{NetworkError}}" {{DOMException}} in |workerRealm|.
3036+
2. Return a [=network error=].
3037+
1. If |eventHandled| is not null, then [=resolve=] |eventHandled|.
30133038
1. Return |response|.
30143039
</section>
30153040

0 commit comments

Comments
 (0)