Skip to content

Commit c0aa494

Browse files
authored
Minor improvements around importScripts. (#1380)
* Minor improvements around importScripts. - In the "perform the fetch" steps for importScripts(), return the response, not its unsafe response. These steps run inside "fetch a classic worker-imported script"[1], which extract the unsafe response, so getting the unsafe response twice is redundant or wrong. - Extract a "bad import script response" definition for resuability. - Rename variables to avoid shadowing. - Skip fetching the main resource again, when doing the import scripts update check. - Make the "perform the fetch" steps in Update asynchronously complete with a response rather than returning true, as is expected of these steps.[2] [1] https://html.spec.whatwg.org/multipage/webappapis.html#fetch-a-classic-worker-imported-script [2] https://html.spec.whatwg.org/multipage/webappapis.html#fetching-scripts-perform-fetch
1 parent 24b8617 commit c0aa494

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

docs/index.bs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2072,12 +2072,10 @@ spec: webappsec-referrer-policy; urlPrefix: https://w3c.github.io/webappsec-refe
20722072
* The [=current global object=]'s [=force bypass cache for importscripts flag=] is set.
20732073
* |registration|'s [=last update check time=] is not null and the time difference in seconds calculated by the current time minus |registration|’s [=last update check time=] is greater than 86400.
20742074
1. Let |response| be the result of [=fetch|fetching=] |request|.
2075-
1. Set |response| to |response|'s [=unsafe response=].
20762075
1. If |response|’s [=response/cache state=] is not "`local`", set |registration|’s [=service worker registration/last update check time=] to the current time.
2077-
1. [=Extract a MIME type=] from the |response|'s [=response/header list=]. If this MIME type (ignoring parameters) is not a [=JavaScript MIME type=], return a [=network error=].
2078-
1. If |response|'s [=response/type=] is not "`error`", and |response|'s [=response/status=] is an <a>ok status</a>, then:
2079-
1. [=map/Set=] |serviceWorker|'s [=script resource map=][|request|'s [=request/url=]] to |response|.
2080-
1. Set |serviceWorker|'s [=classic scripts imported flag=].
2076+
1. If |response|'s [=unsafe response=] is a [=bad import script response=], then return a [=network error=].
2077+
1. [=map/Set=] |serviceWorker|'s [=script resource map=][|request|'s [=request/url=]] to |response|.
2078+
1. Set |serviceWorker|'s [=classic scripts imported flag=].
20812079
1. Return |response|.
20822080
</section>
20832081
</section>
@@ -2204,6 +2202,14 @@ spec: webappsec-referrer-policy; urlPrefix: https://w3c.github.io/webappsec-refe
22042202

22052203
A <dfn id="dfn-scope-to-job-queue-map">scope to job queue map</dfn> is an <a>ordered map</a> where the keys are [=service worker registration/scope urls=], [=URL serializer|serialized=], and the values are [=job queues=].
22062204

2205+
A <dfn id="dfn-bad-import-script-response">bad import script response</dfn> is a [=/response=] for which any of the following conditions are met:
2206+
2207+
* |response|'s [=response/type=] is "`error`"
2208+
* |response|'s [=response/status=] is not an [=ok status=]
2209+
* The result of [=Extract a MIME type|extracting a MIME type=] from |response|'s [=response/header list=] is not a [=JavaScript MIME type=]
2210+
2211+
Note: Keep this definition in sync with [=fetch a classic worker-imported script=].
2212+
22072213
<section algorithm>
22082214
<h3 id="create-job-algorithm"><dfn>Create Job</dfn></h3>
22092215

@@ -2456,22 +2462,25 @@ spec: webappsec-referrer-policy; urlPrefix: https://w3c.github.io/webappsec-refe
24562462
1. If |response|'s [=response/cache state=] is not "`local`", set |registration|'s [=last update check time=] to the current time.
24572463
1. If |newestWorker| is null, or |newestWorker|'s [=script resource map=][|request|'s [=request/url=]]'s [=response/body=] is not byte-for-byte identical with |response|'s [=response/body=], set |hasUpdatedResources| to true.
24582464
1. Else if |newestWorker|'s [=classic scripts imported flag=] is set, then:
2459-
1. [=map/For each=] |url| → |storedResponse| of |newestWorker|'s [=script resource map=]:
2460-
1. Let |request| be a new [=/request=] whose [=request/url=] is |url|, [=request/client=] is |job|'s [=job/client=], [=request/destination=] is "`script`", [=request/parser metadata=] is "`not parser-inserted`", [=request/synchronous flag=] is set, and whose [=request/use-URL-credentials flag=] is set.
2461-
1. Set |request|'s [=request/cache mode=] to "`no-cache`" if any of the following are true:
2465+
2466+
Note: The following checks to see if an imported script has been updated, since the main script has not changed.
2467+
2468+
1. [=map/For each=] |importUrl| → |storedResponse| of |newestWorker|'s [=script resource map=]:
2469+
1. If |importUrl| is |request|'s [=request/url=], then continue.
2470+
1. Let |importRequest| be a new [=/request=] whose [=request/url=] is |importUrl|, [=request/client=] is |job|'s [=job/client=], [=request/destination=] is "`script`", [=request/parser metadata=] is "`not parser-inserted`", [=request/synchronous flag=] is set, and whose [=request/use-URL-credentials flag=] is set.
2471+
1. Set |importRequest|'s [=request/cache mode=] to "`no-cache`" if any of the following are true:
24622472
* |registration|'s [=service worker registration/update via cache mode=] is "`none`".
24632473
* |job|'s [=force bypass cache flag=] is set.
24642474
* |registration|'s [=last update check time=] is not null and the time difference in seconds calculated by the current time minus |registration|’s [=last update check time=] is greater than 86400.
2465-
1. Let |fetchedResponse| be the result of [=fetch|fetching=] |request|.
2475+
1. Let |fetchedResponse| be the result of [=fetch|fetching=] |importRequest|.
2476+
1. Set |updatedResourceMap|[|importRequest|'s [=request/url=]] to |fetchedResponse|.
24662477
1. Set |fetchedResponse| to |fetchedResponse|'s [=unsafe response=].
2467-
1. Set |updatedResourceMap|[|request|'s [=request/url=]] to |fetchedResponse|.
24682478
1. If |fetchedResponse|'s [=response/cache state=] is not "`local`", set |registration|’s [=last update check time=] to the current time.
2469-
1. [=Extract a MIME type=] from the |fetchedResponse|'s [=response/header list=]. If this MIME type (ignoring parameters) is not a [=JavaScript MIME type=], asynchronously complete these steps with a [=network error=].
2470-
1. If |fetchedResponse|'s [=response/type=] is "`error`", or |fetchedResponse|'s [=response/status=] is not an [=ok status=], asynchronously complete these steps with a [=network error=].
2471-
1. If |fetchedResponse|'s [=response/body=] is not byte-for-byte identical with |storedResponse|'s [=response/body=], set |hasUpdatedResources| to true.
2479+
1. If |fetchedResponse| is a [=bad import script response=], then asynchronously complete these steps with a [=network error=].
2480+
1. If |fetchedResponse|'s [=response/body=] is not byte-for-byte identical with |storedResponse|'s [=unsafe response=]'s [=response/body=], set |hasUpdatedResources| to true.
24722481

24732482
Note: The control does not break the loop in this step to continue with all the imported scripts to populate the cache.
2474-
1. Return true.
2483+
1. Asynchronously complete these steps with |response|.
24752484

24762485
If the algorithm asynchronously completes with null, then:
24772486

0 commit comments

Comments
 (0)