Esta traducción está incompleta. Por favor, ayuda a traducir este artículo del inglés.

Esta es una tecnología experimental
Comprueba la Tabla de compabilidad de navegadores cuidadosamente antes de usarla en producción.

The ServiceWorkerContainer interface of the ServiceWorker API provides an object representing the service worker as an overall unit in the network ecosystem, including facilities to register, unregister and update service workers, and access the state of service workers and their registrations.

Most importantly, it exposes the ServiceWorkerContainer.register() method used to register service workers, and the ServiceWorkerContainer.controller property used to determine whether or not the current page is actively controlled.

Properties

ServiceWorkerContainer.controller Read only
Returns a ServiceWorker object if its state is activated (the same object returned by ServiceWorkerRegistration.active). This property returns null during a force-refresh request (Shift + refresh) or if there is no active worker.
ServiceWorkerContainer.ready Read only
Provides a way of delaying code execution until a service worker is active. It returns a Promise that will never reject, and which waits indefinitely until the ServiceWorkerRegistration associated with the current page has an ServiceWorkerRegistration.active worker. Once that condition is met, it resolves with the ServiceWorkerRegistration.

Event handlers

ServiceWorkerContainer.oncontrollerchange
Fired whenever a controllerchange event occurs — when the document's associated ServiceWorkerRegistration acquires a new active worker.
ServiceWorkerContainer.onerror
Fired whenever an error event occurs in the associated service workers.
ServiceWorkerContainer.onmessage
Fired whenever a message event occurs — when incoming messages are received to the ServiceWorkerContainer object (e.g. via a MessagePort.postMessage() call.)

Methods

ServiceWorkerContainer.register() 
Creates or updates a ServiceWorkerRegistration for the given scriptURL.
ServiceWorkerContainer.getRegistration()
Gets a ServiceWorkerRegistration object whose scope matches the provided document URL.  If the method can't return a ServiceWorkerRegistration, it returns a Promise
ServiceWorkerContainer.getRegistrations()
Returns all ServiceWorkerRegistration objects associated with a ServiceWorkerContainer in an array.  If the method can't return ServiceWorkerRegistration objects, it returns a Promise

Examples

The example below first checks to see if the browser supports service workers. If supported, the code registers the service worker and determines if the page is actively controlled by the service worker. If it isn't, it prompts the user to reload the page so the service worker can take control. The code also reports any registration failures.

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);

    // At this point, you can optionally do something 
    // with registration. See https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration
  }).catch(function(error) {
    console.log('Service worker registration failed:', error);
  });

  // Independent of the registration, let's also display
  // information about whether the current page is controlled
  // by an existing service worker, and when that
  // controller changes.

  // First, do a one-off check if there's currently a
  // service worker in control.
  if (navigator.serviceWorker.controller) {
    console.log('This page is currently controlled by:', navigator.serviceWorker.controller);
  }

  // Then, register a handler to detect when a new or
  // updated service worker takes control.
  navigator.serviceWorker.oncontrollerchange = function() {
    console.log('This page is now controlled by:', navigator.serviceWorker.controller);
  };
} else {
  console.log('Service workers are not supported.');
}

Specifications

Specification Status Comment
Service Workers
La definición de 'ServiceWorkerContainer' en esta especificación.
Working Draft Initial definition.

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
Soporte básico
Experimental
Chrome Soporte completo 40Edge Soporte completo 17
Soporte completo 17
Soporte completo 16
Deshabilitado
Deshabilitado From version 16: this feature is behind the Enable service workers preference.
Firefox Soporte completo 44
Notas
Soporte completo 44
Notas
Notas Service workers (and Push) have been disabled in the Firefox 45 and 52 Extended Support Releases (ESR).
IE Sin soporte NoOpera Soporte completo 27Safari Soporte completo 11.1WebView Android Soporte completo 40Chrome Android Soporte completo 40Edge Mobile ? Firefox Android Soporte completo 44Opera Android Soporte completo 27Safari iOS Soporte completo 11.1Samsung Internet Android Soporte completo 4.0
controller
Experimental
Chrome Soporte completo 40Edge Soporte completo 17
Soporte completo 17
Soporte completo 16
Deshabilitado
Deshabilitado From version 16: this feature is behind the Enable service workers preference.
Firefox Soporte completo 44
Notas
Soporte completo 44
Notas
Notas Service workers (and Push) have been disabled in the Firefox 45 and 52 Extended Support Releases (ESR).
IE Sin soporte NoOpera Soporte completo 27Safari Soporte completo 11.1WebView Android Soporte completo 40Chrome Android Soporte completo 40Edge Mobile ? Firefox Android Soporte completo 44Opera Android Soporte completo 27Safari iOS Soporte completo 11.1Samsung Internet Android Soporte completo 4.0
ready
Experimental
Chrome Soporte completo 40Edge Soporte completo 17
Soporte completo 17
Soporte completo 16
Deshabilitado
Deshabilitado From version 16: this feature is behind the Enable service workers preference.
Firefox Soporte completo 44
Notas
Soporte completo 44
Notas
Notas Service workers (and Push) have been disabled in the Firefox 45 and 52 Extended Support Releases (ESR).
IE Sin soporte NoOpera Soporte completo 27Safari Soporte completo 11.1WebView Android Soporte completo 40Chrome Android Soporte completo 40Edge Mobile ? Firefox Android Soporte completo 44Opera Android Soporte completo 27Safari iOS Soporte completo 11.1Samsung Internet Android Soporte completo 4.0
oncontrollerchange
Experimental
Chrome Soporte completo 40Edge Soporte completo 17
Soporte completo 17
Soporte completo 16
Deshabilitado
Deshabilitado From version 16: this feature is behind the Enable service workers preference.
Firefox Soporte completo 44
Notas
Soporte completo 44
Notas
Notas Service workers (and Push) have been disabled in the Firefox 45 and 52 Extended Support Releases (ESR).
IE Sin soporte NoOpera Soporte completo 27Safari Soporte completo 11.1WebView Android Soporte completo 40Chrome Android Soporte completo 40Edge Mobile ? Firefox Android Soporte completo 44Opera Android Soporte completo 27Safari iOS Soporte completo 11.1Samsung Internet Android Soporte completo 4.0
onerror
Experimental
Chrome Soporte completo 40Edge Soporte completo 17
Soporte completo 17
Soporte completo 16
Deshabilitado
Deshabilitado From version 16: this feature is behind the Enable service workers preference.
Firefox Soporte completo 44
Notas
Soporte completo 44
Notas
Notas Service workers (and Push) have been disabled in the Firefox 45 and 52 Extended Support Releases (ESR).
IE Sin soporte NoOpera Soporte completo 27Safari Soporte completo 11.1WebView Android Soporte completo 40Chrome Android Soporte completo 40Edge Mobile ? Firefox Android Soporte completo 44Opera Android Soporte completo 27Safari iOS Soporte completo 11.1Samsung Internet Android Soporte completo 4.0
onmessage
Experimental
Chrome Soporte completo 40Edge Soporte completo 17
Soporte completo 17
Soporte completo 16
Deshabilitado
Deshabilitado From version 16: this feature is behind the Enable service workers preference.
Firefox Soporte completo 44
Notas
Soporte completo 44
Notas
Notas Service workers (and Push) have been disabled in the Firefox 45 and 52 Extended Support Releases (ESR).
IE Sin soporte NoOpera Soporte completo 27Safari Soporte completo 11.1WebView Android Soporte completo 40Chrome Android Soporte completo 40Edge Mobile ? Firefox Android Soporte completo 44Opera Android Soporte completo 27Safari iOS Soporte completo 11.1Samsung Internet Android Soporte completo 4.0
register
Experimental
Chrome Soporte completo 40Edge Soporte completo 17
Soporte completo 17
Soporte completo 16
Deshabilitado
Deshabilitado From version 16: this feature is behind the Enable service workers preference.
Firefox Soporte completo 44
Notas
Soporte completo 44
Notas
Notas Service workers (and Push) have been disabled in the Firefox 45 and 52 Extended Support Releases (ESR).
IE Sin soporte NoOpera Soporte completo 27Safari Soporte completo 11.1WebView Android Soporte completo 40Chrome Android Soporte completo 40Edge Mobile ? Firefox Android Soporte completo 44Opera Android Soporte completo 27Safari iOS Soporte completo 11.1Samsung Internet Android Soporte completo 4.0
getRegistration
Experimental
Chrome Soporte completo 40Edge Soporte completo 17
Soporte completo 17
Soporte completo 16
Deshabilitado
Deshabilitado From version 16: this feature is behind the Enable service workers preference.
Firefox Soporte completo 44
Notas
Soporte completo 44
Notas
Notas Service workers (and Push) have been disabled in the Firefox 45 and 52 Extended Support Releases (ESR).
IE Sin soporte NoOpera Soporte completo 27Safari Soporte completo 11.1WebView Android Soporte completo 40Chrome Android Soporte completo 40Edge Mobile ? Firefox Android Soporte completo 44Opera Android Soporte completo 27Safari iOS Soporte completo 11.1Samsung Internet Android Soporte completo 4.0
getRegistrations
Experimental
Chrome Soporte completo 40Edge Soporte completo 17
Soporte completo 17
Soporte completo 16
Deshabilitado
Deshabilitado From version 16: this feature is behind the Enable service workers preference.
Firefox Soporte completo 44
Notas
Soporte completo 44
Notas
Notas Service workers (and Push) have been disabled in the Firefox 45 and 52 Extended Support Releases (ESR).
IE Sin soporte NoOpera Soporte completo 27Safari Soporte completo 11.1WebView Android Soporte completo 40Chrome Android Soporte completo 40Edge Mobile ? Firefox Android Soporte completo 44Opera Android Soporte completo 27Safari iOS Soporte completo 11.1Samsung Internet Android Soporte completo 4.0
onmessageerrorChrome ? Edge ? Firefox ? IE ? Opera ? Safari ? WebView Android ? Chrome Android ? Edge Mobile ? Firefox Android ? Opera Android ? Safari iOS ? Samsung Internet Android ?
startMessagesChrome ? Edge ? Firefox Soporte completo 64IE ? Opera ? Safari ? WebView Android ? Chrome Android ? Edge Mobile ? Firefox Android Soporte completo 64Opera Android ? Safari iOS ? Samsung Internet Android ?

Leyenda

Soporte completo  
Soporte completo
Sin soporte  
Sin soporte
Compatibility unknown  
Compatibility unknown
Experimental. Esperar que el comportamiento cambie en el futuro.
Experimental. Esperar que el comportamiento cambie en el futuro.
Ver notas de implementación.
Ver notas de implementación.
El usuario debe de habilitar explícitamente esta característica.
El usuario debe de habilitar explícitamente esta característica.

See also

Etiquetas y colaboradores del documento

Colaboradores en esta página: fscholz
Última actualización por: fscholz,