Intent to Deprecate and Remove: importScripts() of new scripts after service worker installation

1,268 views
Skip to first unread message

Matt Falkenhagen

unread,
Aug 13, 2018, 2:33:18 AM8/13/18
to blink-dev, worke...@chromium.org
Contact emails
fal...@chromium.org

Summary
importScripts() is a synchronous API that fetches the given URLs for scripts and executes the scripts. Until this deprecation, a service worker could use importScripts() to import scripts at any time. After this deprecation, a service worker can only use importScripts() in the following situations:
  • Before installation finishes (from the initial execution of the service worker until it reaches the `installed` state): any script.
  • After installation finishes: scripts that were already imported in the previous case.
In disallowed cases after this deprecation, importScripts() would throw a NetworkError.

Motivation
Synchronous blocking APIs like importScripts() are generally forbidden in service workers as they can negatively impact performance. By limiting importScripts() to the installation phase, a service worker pays the cost of fetching the script from network only one time during installation.

importScripts() of new scripts are particularly harmful for installed service workers because two optimizations are nullified:
  • Known scripts for an installed worker are loaded from storage at service worker startup, before importScripts() is called for them.
  • Known scripts for an installed worker are stored with a “full” V8 bytecode cache.
Disallowing new scripts also simplifies the implementation, as all importScripts() of an installed service worker would be known at startup time.

Interoperability and Compatibility Risk
The specification has long forbidden importScripts() of new scripts after installation (spec issue).

For interoperability, the state of other browsers:
  • Edge: Not supported (i.e., they match the spec)
  • Firefox: Not supported since Firefox 62 (i.e., they match the spec now (current Beta))
  • Safari: Supported (I filed a bug)
For compatibility risk, there are sites that use this feature. Most of the usage I found was sites doing things like this at top-level:
  importScripts(‘https://example-cdn.example.com/script.js?’ + Math.random());

I believe the sites are trying to get the most updated version from the server. This use case would be addressed including imported scripts in the service worker update check, which we are implementing now.

We’ve already gotten reports of incompatibility with Edge (e.g., here). Since such sites would break on Firefox and Edge also, it’s a bit safer for Chrome to remove the feature now.

Alternative implementation suggestion for web developers
Use importScripts() to get all the scripts a service worker needs before installation.

Including imported scripts in the service worker update check is still being implemented for Chrome at bug 648295. A potential workaround is to force an update by changing something in the main script if it’s known that an imported script has changed.

Usage information from UseCounter
0.038%

Entry on the feature dashboard
https://www.chromestatus.com/feature/5748516353736704

Requesting approval to remove too?
Yes, in M71.

sligh...@chromium.org

unread,
Aug 14, 2018, 8:15:08 PM8/14/18
to blink-dev, worke...@chromium.org
LGTM

Philip Jägenstedt

unread,
Aug 15, 2018, 3:45:30 AM8/15/18
to Alex Russell, blink-dev, worke...@chromium.org
LGTM2
> --
> You received this message because you are subscribed to the Google Groups "blink-dev" group.
> To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/blink-dev/85a66b01-1ee6-4a3f-b941-6487c07b6e65%40chromium.org.

Yoav Weiss

unread,
Aug 15, 2018, 11:34:42 AM8/15/18
to Philip Jägenstedt, Alex Russell, blink-dev, worke...@chromium.org
LGTM3

To unsubscribe from this group and stop receiving emails from it, send an email to blink-dev+...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/blink-dev/CAARdPYdzMbP7C4_jJw5qZLs0gUjV%2B09z_RsDoq%2BwGssViHZN-Q%40mail.gmail.com.

Reply all
Reply to author
Forward
0 new messages