This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
The register()
method of the ServiceWorkerContainer
interface creates or updates a ServiceWorkerRegistration
for the given scriptURL
.
If successful, a service worker registration ties the provided script URL to a scope, which is subsequently used for navigation matching. You can call this method unconditionally from the controlled page. I.e., you don't need to first check whether there's an active registration.
There is frequent confusion surrounding the meaning and use of scope. Since a service worker can't have a scope broader than its own location, only use the scope
option when you need a scope that is narrower than the default.
ServiceWorkerContainer.register(scriptURL, options) .then(function(ServiceWorkerRegistration) { ... });
scriptURL
options
Optional
scope
: A USVString
representing a URL that defines a service worker's registration scope; that is, what range of URLs a service worker can control. This is usually a relative URL. It is relative to the base URL of the application. By default, the scope
value for a service worker registration is set to the directory where the service worker script is located. See the Examples section for more information on how it works.A Promise
that resolves with a ServiceWorkerRegistration
object.
The examples described here should be taken together to get a better understanding of how service workers scope applies to a page.
The following example uses the default value of scope
(by omitting it). The service worker in this case will control example.com/index.html
as well as pages underneath it, like example.com/product/description.html
.
if ('serviceWorker' in navigator) { // Register a service worker hosted at the root of the // site using the default scope. navigator.serviceWorker.register('/sw.js').then(function(registration) { console.log('Service worker registration succeeded:', registration); }, /*catch*/ function(error) { console.log('Service worker registration failed:', error); }); } else { console.log('Service workers are not supported.'); }
The following code, if included in a page at the root of a site, would apply to exactly the same pages as the example above. Remember the scope, when included, uses the page's location as its base. Alternatively, if this code were included in a page at example.com/product/description.html
, the scope of './'
would mean that the service worker only applies to resources under example.com/product
. If I needed to register a service worker on example.com/product/description.html
that applied to all of example.com
, I would leave off the scope as above.
if ('serviceWorker' in navigator) { // Register a service worker hosted at the root of the // site using a more restrictive scope. navigator.serviceWorker.register('/sw.js', {scope: './'}).then(function(registration) { console.log('Service worker registration succeeded:', registration); }, /*catch*/ function(error) { console.log('Service worker registration failed:', error); }); } else { console.log('Service workers are not supported.'); }
Specification | Status | Comment |
---|---|---|
Service Workers The definition of 'ServiceWorkerContainer: register' in that specification. | Working Draft | Initial definition. |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 40 | 17
|
44
|
No | 27 | 11.1 |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | 40 | 40 | ? | 44 | 27 | 11.1 | 4.0 |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/register