When you're done, copy the following code to serviceworker-import.js
:
(request,
response)
parameters, but shouldCheckGit
only takes
a request
parameter.
For example, to vary which branch on GitHub to look on, you could write
something like:
self.gitBranch = req => req.url.startsWith("https://www.example.com") ? "release" : "development";
serviceworker-import.js
is not only the file where you can
configure Signed Web Apps. It is also the place to import other Service
Worker files (related or unrelated to Signed Web Apps), by calling:
await importScriptsFromSW("path/to/other/service-worker.js");The
await
means that all code after it will wait
until the Service Worker file is downloaded and run. If you don't need
that, you can remove it. You can also download and run multiple files
simultaneously, and wait until all of them have finished running, by
calling:
await importScriptsFromSW("first/service-worker.js", "second/service-worker.js");Note: there are some differences between code you can run in a normal Service Worker, and code you can run in this file (and files imported by it), namely:
You can only register for fetch
, message
, install
and
activate
events. If you want to register for other events, you
have to manually add them to the eventNames
list in
signed-web-apps/dist/sw/serviceworker.js
.
As shown in the examples above, you should use importScriptsFromSW() instead of importScripts(), because the latter doesn't support Subresource Integrity. This is also why generated config code above is wrapped in an asynchronous immediately-executed function expression.