diff options
Diffstat (limited to 'files/id/web/api/service_worker_api/index.html')
-rw-r--r-- | files/id/web/api/service_worker_api/index.html | 283 |
1 files changed, 0 insertions, 283 deletions
diff --git a/files/id/web/api/service_worker_api/index.html b/files/id/web/api/service_worker_api/index.html deleted file mode 100644 index 345f183563..0000000000 --- a/files/id/web/api/service_worker_api/index.html +++ /dev/null @@ -1,283 +0,0 @@ ---- -title: Service Worker API -slug: Web/API/Service_Worker_API -tags: - - API - - Landing - - NeedsTranslation - - Offline - - Overview - - Reference - - Service Workers - - TopicStub - - Workers -translation_of: Web/API/Service_Worker_API ---- -<div> -<p>{{ServiceWorkerSidebar}}</p> - -<p>{{ SeeCompatTable() }}</p> - -<p class="summary">Service workers essentially act as proxy servers that sit between web applications, and the browser and network (when available). They are intended to (amongst other things) enable the creation of effective offline experiences, intercepting network requests and taking appropriate action based on whether the network is available and updated assets reside on the server. They will also allow access to push notifications and background sync APIs.</p> -</div> - -<h2 id="Service_worker_concepts_and_usage">Service worker concepts and usage</h2> - -<p>A service worker is an event-driven <a href="https://developer.mozilla.org/en-US/docs/Web/API/Worker">worker</a> registered against an origin and a path. It takes the form of a JavaScript file that can control the web page/site it is associated with, intercepting and modifying navigation and resource requests, and caching resources in a very granular fashion to give you complete control over how your app behaves in certain situations (the most obvious one being when the network is not available.)</p> - -<p>A service worker is run in a worker context: it therefore has no DOM access, and runs on a different thread to the main JavaScript that powers your app, so it is not blocking. It is designed to be fully async; as a consequence, APIs such as synchronous <a href="/en-US/docs/Web/API/XMLHttpRequest">XHR</a> and <a href="https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage">localStorage</a> can't be used inside a service worker.</p> - -<p>Service workers only run over HTTPS, for security reasons. Having modified network requests wide open to man in the middle attacks would be really bad.</p> - -<div class="note"> -<p><strong>Note</strong>: Service Workers win over previous attempts in this area such as <a href="http://alistapart.com/article/application-cache-is-a-douchebag">AppCache</a> because they don't make assumptions about what you are trying to do and then break when those assumptions are not exactly right; you have granular control over everything.</p> -</div> - -<div class="note"> -<p><strong>Note</strong>: Service workers make heavy use of <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">promises</a>, as generally they will wait for responses to come through, after which they will respond with a success or failure action. The promises architecture is ideal for this.</p> -</div> - -<h3 id="Registration">Registration</h3> - -<p>A service worker is first registered using the {{domxref("ServiceWorkerContainer.register()")}} method. If successful, your service worker will be downloaded to the client and attempt installation/activation (see below) for URLs accessed by the user inside the whole origin, or inside a subset specified by you.</p> - -<h3 id="Download_install_and_activate">Download, install and activate</h3> - -<p>At this point, your service worker will observe the following lifecycle:</p> - -<ol> - <li>Download</li> - <li>Install</li> - <li>Activate</li> -</ol> - -<p>The service worker is immediately downloaded when a user first accesses a service worker–controlled site/page.</p> - -<p>After that it is downloaded every 24 hours or so. It *may* be downloaded more frequently, but it <strong>must</strong> be downloaded every 24h to prevent bad scripts from being annoying for too long.</p> - -<p>Installation is attempted when the downloaded file is found to be new — either different to an existing service worker (byte-wise compared), or the first service worker encountered for this page/site.</p> - -<p>If this is the first time a service worker has been made available, installation is attempted, then after a successful installation it is activated.</p> - -<p>If there is an existing service worker available, the new version is installed in the background, but not yet activated — at this point it is called the <em>worker in waiting</em>. It is only activated when there are no longer any pages loaded that are still using the old service worker. As soon as there are no more such pages still loaded, the new service worker activates (becoming the <em>active worker</em>).</p> - -<p>You can listen out for the {{domxref("InstallEvent")}}; a standard action is to prepare your service worker for usage when this fires, for example by creating a cache using the built in storage API, and placing assets inside it that you'll want for running your app offline.</p> - -<p>There is also an <code>activate</code> event. The point where this event fires is generally a good time to clean up old caches and other things associated with the previous version of your service worker.</p> - -<p>Your service worker can respond to requests using the {{domxref("FetchEvent")}} event. You can modify the response to these requests in any way you want, using the {{domxref("FetchEvent.respondWith") }} method.</p> - -<div class="note"> -<p><strong>Note</strong>: Because <code>oninstall</code>/<code>onactivate</code> could take a while to complete, the service worker spec provides a <code>waitUntil</code> method that, when called <code>oninstall</code> or <code>onactivate</code>, passes a promise. Functional events are not dispatched to the service worker until the promise successfully resolves.</p> -</div> - -<p>For a complete tutorial to show how to build up your first basic example, read <a href="/en-US/docs/Web/API/ServiceWorker_API/Using_Service_Workers">Using Service Workers</a>.</p> - -<h2 id="Other_use_case_ideas">Other use case ideas</h2> - -<p>Service workers are also intended to be used for such things as:</p> - -<ul> - <li>Background data synchronization</li> - <li>Responding to resource requests from other origins</li> - <li>Receiving centralized updates to expensive-to-calculate data such as geolocation or gyroscope, so multiple pages can make use of one set of data</li> - <li>Client-side compiling and dependency management of CoffeeScript, less, CJS/AMD modules, etc. for dev purposes</li> - <li>Hooks for background services</li> - <li>Custom templating based on certain URL patterns</li> - <li>Performance enhancements, for example pre-fetching resources that the user is likely to need in the near future, such as the next few pictures in a photo album.</li> -</ul> - -<p>In the future, service workers will be able to do a number of other useful things for the web platform that will bring it closer towards native app viability. Interestingly, other specifications can and will start to make use of the service worker context, for example:</p> - -<ul> - <li><a href="https://github.com/slightlyoff/BackgroundSync">Background synchronization</a>: Start up a service worker even when no users are at the site, so caches can be updated, etc.</li> - <li><a href="/en-US/docs/Web/API/Push_API">Reacting to push messages</a>: Start up a service worker to send users a message to tell them new content is available.</li> - <li>Reacting to a particular time & date</li> - <li>Entering a geo-fence</li> -</ul> - -<h2 id="Interfaces">Interfaces</h2> - -<dl> - <dt>{{domxref("Cache") }}</dt> - <dd>Represents the storage for {{domxref("Request")}} / {{domxref("Response")}} object pairs that are cached as part of the {{domxref("ServiceWorker")}} life cycle.</dd> - <dt>{{domxref("CacheStorage") }}</dt> - <dd>Represents the storage for {{domxref("Cache")}} objects. It provides a master directory of all the named caches that a {{domxref("ServiceWorker")}} can access and maintains a mapping of string names to corresponding {{domxref("Cache")}} objects.</dd> - <dt>{{domxref("Client") }}</dt> - <dd>Represents the scope of a service worker client. A service worker client is either a document in a browser context or a {{domxref("SharedWorker")}}, which is controlled by an active worker.</dd> - <dt>{{domxref("Clients") }}</dt> - <dd>Represents a container for a list of {{domxref("Client")}} objects; the main way to access the active service worker clients at the current origin.</dd> - <dt>{{domxref("ExtendableEvent") }}</dt> - <dd>Extends the lifetime of the <code>install</code> and <code>activate</code> events dispatched on the {{domxref("ServiceWorkerGlobalScope")}} as part of the service worker lifecycle. This ensures that any functional events (like {{domxref("FetchEvent")}}) are not dispatched to the {{domxref("ServiceWorker")}} until it upgrades database schemas, deletes outdated cache entries, etc.</dd> - <dt>{{domxref("ExtendableMessageEvent") }}</dt> - <dd>The event object of a {{event("message_(ServiceWorker)","message")}} event fired on a service worker (when a channel message is received on the {{domxref("ServiceWorkerGlobalScope")}} from another context) — extends the lifetime of such events.</dd> - <dt>{{domxref("FetchEvent") }}</dt> - <dd>The parameter passed into the {{domxref("ServiceWorkerGlobalScope.onfetch")}} handler, <code>FetchEvent</code> represents a fetch action that is dispatched on the {{domxref("ServiceWorkerGlobalScope")}} of a {{domxref("ServiceWorker")}}. It contains information about the request and resulting response, and provides the {{domxref("FetchEvent.respondWith", "FetchEvent.respondWith()")}} method, which allows us to provide an arbitrary response back to the controlled page.</dd> - <dt>{{domxref("InstallEvent") }}</dt> - <dd>The parameter passed into the {{domxref("ServiceWorkerGlobalScope.oninstall", "oninstall")}} handler, the <code>InstallEvent</code> interface represents an install action that is dispatched on the {{domxref("ServiceWorkerGlobalScope")}} of a {{domxref("ServiceWorker")}}. As a child of {{domxref("ExtendableEvent")}}, it ensures that functional events such as {{domxref("FetchEvent")}} are not dispatched during installation. </dd> - <dt>{{domxref("Navigator.serviceWorker") }}</dt> - <dd>Returns a {{domxref("ServiceWorkerContainer")}} object, which provides access to registration, removal, upgrade, and communication with the {{domxref("ServiceWorker")}} objects for the <a href="https://html.spec.whatwg.org/multipage/browsers.html#concept-document-window">associated document</a>.</dd> - <dt>{{domxref("NotificationEvent") }}</dt> - <dd>The parameter passed into the {{domxref("ServiceWorkerGlobalScope.onnotificationclick", "onnotificationclick")}} handler, the <code>NotificationEvent</code> interface represents a notification click event that is dispatched on the {{domxref("ServiceWorkerGlobalScope")}} of a {{domxref("ServiceWorker")}}.</dd> - <dt>{{domxref("ServiceWorker") }}</dt> - <dd>Represents a service worker. Multiple browsing contexts (e.g. pages, workers, etc.) can be associated with the same <code>ServiceWorker</code> object.</dd> - <dt>{{domxref("ServiceWorkerContainer") }}</dt> - <dd>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.</dd> - <dt>{{domxref("ServiceWorkerGlobalScope") }}</dt> - <dd>Represents the global execution context of a service worker.</dd> - <dt>{{domxref("ServiceWorkerMessageEvent")}}</dt> - <dd>Contains information about an event sent to a {{domxref("ServiceWorkerContainer")}} target. </dd> - <dt>{{domxref("ServiceWorkerRegistration") }}</dt> - <dd>Represents a service worker registration.</dd> - <dt>{{domxref("SyncEvent")}} {{non-standard_inline}}</dt> - <dd> - <p>The SyncEvent interface represents a sync action that is dispatched on the {{domxref("ServiceWorkerGlobalScope")}} of a ServiceWorker. </p> - </dd> - <dt>{{domxref("SyncManager")}} {{non-standard_inline}}</dt> - <dd>Provides an interface for registering and listing sync registrations.</dd> - <dt>{{domxref("WindowClient") }}</dt> - <dd>Represents the scope of a service worker client that is a document in a browser context, controlled by an active worker. This is a special type of {{domxref("Client")}} object, with some additional methods and properties available.</dd> -</dl> - -<h2 id="Specifications">Specifications</h2> - -<table class="standard-table"> - <tbody> - <tr> - <th scope="col">Specification</th> - <th scope="col">Status</th> - <th scope="col">Comment</th> - </tr> - <tr> - <td>{{SpecName('Service Workers')}}</td> - <td>{{Spec2('Service Workers')}}</td> - <td>Initial definition.</td> - </tr> - </tbody> -</table> - -<h2 id="Browser_compatibility">Browser compatibility</h2> - -<div>{{ CompatibilityTable() }}</div> - -<div id="compat-desktop"> -<table class="compat-table"> - <tbody> - <tr> - <th>Feature</th> - <th>Chrome</th> - <th>Firefox (Gecko)</th> - <th>Internet Explorer</th> - <th>Opera</th> - <th>Safari (WebKit)</th> - </tr> - <tr> - <td>Basic support</td> - <td>{{CompatChrome(40.0)}}</td> - <td>{{ CompatGeckoDesktop("44.0") }}<sup>[1]</sup></td> - <td>{{ CompatNo() }}</td> - <td>24</td> - <td>{{ CompatNo() }}</td> - </tr> - <tr> - <td>install/activate events</td> - <td>{{ CompatChrome(40.0) }}</td> - <td>{{ CompatGeckoDesktop("44.0") }}<sup>[1]</sup></td> - <td>{{ CompatNo() }}</td> - <td>{{ CompatVersionUnknown() }}</td> - <td>{{ CompatNo() }}</td> - </tr> - <tr> - <td>fetch event/request/<br> - <code>respondWith()</code></td> - <td>{{CompatChrome(40.0)}}</td> - <td>{{ CompatGeckoDesktop("44.0") }}<sup>[1]</sup></td> - <td>{{ CompatNo() }}</td> - <td>{{ CompatNo() }}</td> - <td>{{ CompatNo() }}</td> - </tr> - <tr> - <td>caches/cache</td> - <td> - <p class="p1">{{CompatChrome(42.0)}}</p> - </td> - <td>{{ CompatGeckoDesktop("39.0") }}<sup>[1]</sup></td> - <td>{{ CompatNo() }}</td> - <td>{{ CompatNo() }}</td> - <td>{{ CompatNo() }}</td> - </tr> - </tbody> -</table> -</div> - -<div id="compat-mobile"> -<table class="compat-table"> - <tbody> - <tr> - <th>Feature</th> - <th>Android</th> - <th>Chrome for Android</th> - <th>Firefox Mobile (Gecko)</th> - <th>Firefox OS</th> - <th>IE Phone</th> - <th>Opera Mobile</th> - <th>Safari Mobile</th> - </tr> - <tr> - <td>Basic support</td> - <td> </td> - <td>{{CompatChrome(40.0)}}</td> - <td>{{ CompatGeckoMobile("44.0") }}</td> - <td>{{ CompatVersionUnknown }}</td> - <td>{{ CompatNo() }}</td> - <td>{{ CompatVersionUnknown() }}</td> - <td>{{ CompatNo() }}</td> - </tr> - <tr> - <td> install/activate events</td> - <td>{{ CompatNo() }}</td> - <td>{{CompatChrome(40.0)}}</td> - <td>{{ CompatGeckoMobile("44.0") }}</td> - <td>{{ CompatVersionUnknown }}</td> - <td>{{ CompatNo() }}</td> - <td>{{ CompatVersionUnknown() }}</td> - <td>{{ CompatNo() }}</td> - </tr> - <tr> - <td>fetch event/request/<br> - <code>respondWith()</code></td> - <td>{{ CompatNo() }}</td> - <td>{{CompatChrome(40.0)}}</td> - <td>{{ CompatGeckoMobile("44.0") }}</td> - <td>{{ CompatVersionUnknown }}</td> - <td>{{ CompatNo() }}</td> - <td>{{ CompatNo() }}</td> - <td>{{ CompatNo() }}</td> - </tr> - <tr> - <td>caches/cache</td> - <td>{{ CompatNo() }}</td> - <td>{{CompatChrome(40.0)}}</td> - <td>{{ CompatGeckoMobile("39.0") }}</td> - <td>{{ CompatVersionUnknown }}</td> - <td>{{ CompatNo() }}</td> - <td>{{ CompatNo() }}</td> - <td>{{ CompatNo() }}</td> - </tr> - </tbody> -</table> -</div> - -<p>[1] Service workers (and <a href="/en-US/docs/Web/API/Push_API">Push</a>) have been disabled in the <a href="https://www.mozilla.org/en-US/firefox/organizations/">Firefox 45 Extended Support Release</a> (ESR.)</p> - -<h2 id="See_also">See also</h2> - -<ul> - <li><a href="https://serviceworke.rs">ServiceWorker Cookbook</a></li> - <li><a href="/en-US/docs/Web/API/ServiceWorker_API/Using_Service_Workers">Using Service Workers</a></li> - <li><a href="https://github.com/mdn/sw-test">Service workers basic code example</a></li> - <li><a href="https://jakearchibald.github.io/isserviceworkerready/">Is ServiceWorker ready?</a></li> - <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promises</a></li> - <li><a href="/en-US/docs/Web/Guide/Performance/Using_web_workers">Using web workers</a></li> - <li><a href="https://www.fastly.com/blog/best-practices-for-using-the-vary-header">Best Practices for using the VARY header</a></li> -</ul> |