Contact emails
fal...@chromium.orgSummary
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.
MotivationSynchronous 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 RiskThe 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 developersUse 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 UseCounter0.038%