Re: [w3c/ServiceWorker] Skip executing fetch handlers if it is no-op. (PR #1672)

@domenic commented on this pull request.



> +    <h3 id="empty-fetch-handler-identification-algorithm"><dfn>Empty Fetch Handler Identification</dfn></h3>
+
+      : Input
+      :: |workerGlobalScope|, a [=service worker/global object=].
+      : Output
+      :: a boolean
+
+      1. If |workerGlobalScope|'s [=set of event types to handle=] does not [=set/contain=] <code>fetch</code>, then returns false.
+      1. Let |eventListenerList| be an empty [=list=].
+      1. [=list/For each=] |eventListener| of |workerGlobalScope|'s [=set of event types to handle=]:
+          1. If |eventListener|'s <a spec="dom">type</a> is <code>fetch</code>, then [=list/append=] |eventListener| to |eventListenerList|.
+      1. [=list/For each=] |eventListener| of |eventListenerList|:
+          1. If |eventListener|'s [=callback=] is not null:
+              1. Set |callback| to the result of [=converting|convert to an ECMAScript value=] |eventListener|'s [=callback=] to an ECMAScript value.
+              1. If [$IsCallable$](|callback|) is false:
+                  1. Let |getResult| be [=Completion=]([$Get$](|callback|), <code>handleEvent</code>).
+                  1. If |getResult| is [=abrupt completion=], then return false.
+                  1. Set |callback| to |getResult|.
+              1. If [$IsCallable$](|callback|) is false, then return false.
+              1. If |callback|'s [=function body=] is not empty (i.e. either a [=statement=] or [=declaration=] exist), then return false.
+
+              Note: it detects something like `onfetch = () => {}`. Some sites have a fetch event listener with empty body to make them recognized as progressive web application (PWA).

Yes. We started discussing how to fix that (offline) but then got sidetracked into making the surrounding parts of the spec more precise.

To detech `onfetch = () => {}` I think the following would work, nested immediately under "If _eventListener_'s callback is not null":

> 1. If _eventListener_ is an `EventListener` of the type created by [activate an event handler](https://html.spec.whatwg.org/#activate-an-event-handler), then set |callback| to the result of [converting] _eventTarget_'s [event handler map](https://html.spec.whatwg.org/#event-handler-map)["`fetch`"]'s [value](https://html.spec.whatwg.org/#event-handler-value) to an ECMAScript value.
> 2. Otherwise, set |callback| to ...

(you could also factor out the [converting] into a separate step.) 

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

Message ID: <w3c/ServiceWorker/pull/1672/review/1308638927@github.com>

Received on Wednesday, 22 February 2023 07:12:12 UTC