diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/fr/web/api/serviceworkerglobalscope | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/fr/web/api/serviceworkerglobalscope')
-rw-r--r-- | files/fr/web/api/serviceworkerglobalscope/index.html | 152 | ||||
-rw-r--r-- | files/fr/web/api/serviceworkerglobalscope/onnotificationclick/index.html | 77 |
2 files changed, 229 insertions, 0 deletions
diff --git a/files/fr/web/api/serviceworkerglobalscope/index.html b/files/fr/web/api/serviceworkerglobalscope/index.html new file mode 100644 index 0000000000..a114ee3ee4 --- /dev/null +++ b/files/fr/web/api/serviceworkerglobalscope/index.html @@ -0,0 +1,152 @@ +--- +title: ServiceWorkerGlobalScope +slug: Web/API/ServiceWorkerGlobalScope +tags: + - API + - Interface + - NeedsTranslation + - Offline + - Reference + - Service Workers + - ServiceWorkerGlobalScope + - TopicStub +translation_of: Web/API/ServiceWorkerGlobalScope +--- +<div>{{APIRef("Service Workers API")}}</div> + +<p>The <strong><code>ServiceWorkerGlobalScope</code></strong> interface of the <a href="/en-US/docs/Web/API/Service_Worker_API">Service Worker API</a> represents the global execution context of a service worker.</p> + +<p>Developers should keep in mind that the ServiceWorker state is not persisted across the termination/restart cycle, so each event handler should assume it's being invoked with a bare, default global state.</p> + +<p>Once successfully registered, a service worker can and will be terminated when idle to conserve memory and processor power. An active service worker is automatically restarted to respond to events, such as {{domxref("ServiceWorkerGlobalScope.onfetch")}} or {{domxref("ServiceWorkerGlobalScope.onmessage")}}.</p> + +<p>Additionally, synchronous requests are not allowed from within a service worker — only asynchronous requests, like those initiated via the {{domxref("GlobalFetch.fetch", "fetch()")}} method, can be used.</p> + +<p>This interface inherits from the {{domxref("WorkerGlobalScope")}} interface, and its parent {{domxref("EventTarget")}}, and therefore implements properties from {{domxref("WindowTimers")}}, {{domxref("WindowBase64")}}, and {{domxref("WindowEventHandlers")}}.</p> + +<p>{{InheritanceDiagram(700, 85, 20)}}</p> + +<h2 id="Properties">Properties</h2> + +<dl> + <dt>{{domxref("ServiceWorkerGlobalScope.caches")}} {{readonlyinline}}</dt> + <dd>Contains the {{domxref("CacheStorage")}} object associated with the service worker.</dd> + <dt>{{domxref("ServiceWorkerGlobalScope.clients")}} {{readonlyinline}}</dt> + <dd>Contains the {{domxref("Clients")}} object associated with the service worker.</dd> + <dt>{{domxref("ServiceWorkerGlobalScope.registration")}} {{readonlyinline}}</dt> + <dd>Contains the {{domxref("ServiceWorkerRegistration")}} object that represents the service worker's registration.</dd> +</dl> + +<h2 id="Events">Events</h2> + +<dl> + <dt>{{domxref("ServiceWorkerGlobalScope/activate_event", "activate")}}</dt> + <dd>Occurs when a {{domxref("ServiceWorkerRegistration")}} acquires a new {{domxref("ServiceWorkerRegistration.active")}} worker.<br> + Also available via the {{domxref("ServiceWorkerGlobalScope.onactivate")}} property.</dd> + <dt>{{domxref("ServiceWorkerGlobalScope/contentdelete_event", "contentdelete")}}</dt> + <dd>Occurs when an item is removed from the {{domxref("ContentIndex", "Content Index")}}.<br> + Also available via the {{domxref("ServiceWorkerGlobalScope.oncontentdelete")}} property.</dd> + <dt><code>fetch</code></dt> + <dd>Occurs when a {{domxref("GlobalFetch.fetch", "fetch()")}} is called.<br> + Also available via the {{domxref("ServiceWorkerGlobalScope.onfetch")}} property.</dd> + <dt>{{domxref("ServiceWorkerGlobalScope/install_event", "install")}}</dt> + <dd>Occurs when a {{domxref("ServiceWorkerRegistration")}} acquires a new {{domxref("ServiceWorkerRegistration.installing")}} worker.<br> + Also available via the {{domxref("ServiceWorkerGlobalScope.oninstall")}} property.</dd> + <dt>{{domxref("ServiceWorkerGlobalScope/message_event", "message")}}</dt> + <dd>Occurs when incoming messages are received. Controlled pages can use the {{domxref("MessagePort.postMessage()")}} method to send messages to service workers. The service worker can optionally send a response back via the {{domxref("MessagePort")}} exposed in <a href="https://html.spec.whatwg.org/multipage/comms.html#messageport"><code>event.data.port</code></a>, corresponding to the controlled page.<br> + Also available via the {{domxref("ServiceWorkerGlobalScope.onmessage")}} property.</dd> + <dt>{{domxref("ServiceWorkerGlobalScope/notificationclick_event", "notificationclick")}}</dt> + <dd>Occurs when a user clicks on a displayed notification.<br> + Also available via the {{domxref("ServiceWorkerGlobalScope.onnotificationclick")}} property.</dd> + <dt><code>notificationclose</code></dt> + <dd>Occurs — when a user closes a displayed notification.<br> + Also available via the {{domxref("ServiceWorkerGlobalScope.onnotificationclose")}} property.</dd> + <dt>{{domxref("ServiceWorkerGlobalScope/push_event", "push")}}</dt> + <dd>Occurs when a server push notification is received.<br> + Also available via the {{domxref("ServiceWorkerGlobalScope.onpush")}} property.</dd> + <dt>{{domxref("ServiceWorkerGlobalScope/pushsubscriptionchange_event", "pushsubscriptionchange")}}</dt> + <dd>Occurs when a push subscription has been invalidated, or is about to be invalidated (e.g. when a push service sets an expiration time).<br> + Also available via the {{domxref("ServiceWorkerGlobalScope.onpushsubscriptionchange")}} property.</dd> + <dt><code>sync</code></dt> + <dd>Triggered when a call to {{domxref("SyncManager.register")}} is made from a service worker client page. The attempt to sync is made either immediately if the network is available or as soon as the network becomes available.<br> + Also available via the {{domxref("ServiceWorkerGlobalScope.onsync")}} property.</dd> +</dl> + +<h2 id="Methods">Methods</h2> + +<dl> + <dt>{{domxref("ServiceWorkerGlobalScope.skipWaiting()")}}</dt> + <dd>Allows the current service worker registration to progress from waiting to active state while service worker clients are using it.</dd> +</dl> + +<p><code>ServiceWorkerGlobalScope</code> implements {{domxref("WorkerGlobalScope")}} — which implements {{domxref("WindowOrWorkerGlobalScope")}}. Therefore it also has the following property available to it:</p> + +<dl> + <dt>{{domxref("GlobalFetch.fetch()")}}</dt> + <dd>Starts the process of fetching a resource. This returns a promise that resolves to the {{domxref("Response")}} object representing the response to your request. This algorithm is the entry point for the fetch handling handed to the service worker context.</dd> +</dl> + +<h2 id="Examples">Examples</h2> + +<p>This code snippet is from the <a href="https://github.com/GoogleChrome/samples/blob/gh-pages/service-worker/prefetch/service-worker.js">service worker prefetch sample</a> (see <a href="https://googlechrome.github.io/samples/service-worker/prefetch/">prefetch example live</a>.) The {{domxref("ServiceWorkerGlobalScope.onfetch")}} event handler listens for the <code>fetch</code> event. When fired, the code returns a promise that resolves to the first matching request in the {{domxref("Cache")}} object. If no match is found, the code fetches a response from the network.</p> + +<p>The code also handles exceptions thrown from the {{domxref("GlobalFetch.fetch", "fetch()")}} operation. Note that an HTTP error response (e.g., 404) will not trigger an exception. It will return a normal response object that has the appropriate error code set.</p> + +<pre class="brush: js notranslate">self.addEventListener('fetch', function(event) { + console.log('Handling fetch event for', event.request.url); + + event.respondWith( + caches.match(event.request).then(function(response) { + if (response) { + console.log('Found response in cache:', response); + + return response; + } + console.log('No response found in cache. About to fetch from network...'); + + return fetch(event.request).then(function(response) { + console.log('Response from network is:', response); + + return response; + }, function(error) { + console.error('Fetching failed:', error); + + throw error; + }); + }) + ); +});</pre> + +<h2 id="Specifications">Specifications</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('Service Workers', '#serviceworkerglobalscope-interface', 'ServiceWorkerGlobalScope')}}</td> + <td>{{Spec2('Service Workers')}}</td> + <td>Initial definition</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility">Browser compatibility</h2> + + + +<p>{{Compat("api.ServiceWorkerGlobalScope")}}</p> + +<h2 id="See_also">See also</h2> + +<ul> + <li><a href="/en-US/docs/Web/API/ServiceWorker_API/Using_Service_Workers">Using Service Workers</a></li> + <li><a class="external external-icon" href="https://github.com/mdn/sw-test">Service workers basic code example</a></li> + <li><a class="external external-icon" href="https://jakearchibald.github.io/isserviceworkerready/">Is ServiceWorker ready?</a></li> + <li>{{jsxref("Promise")}}</li> +</ul> diff --git a/files/fr/web/api/serviceworkerglobalscope/onnotificationclick/index.html b/files/fr/web/api/serviceworkerglobalscope/onnotificationclick/index.html new file mode 100644 index 0000000000..d3c2e405b6 --- /dev/null +++ b/files/fr/web/api/serviceworkerglobalscope/onnotificationclick/index.html @@ -0,0 +1,77 @@ +--- +title: ServiceWorkerGlobalScope.onnotificationclick +slug: Web/API/ServiceWorkerGlobalScope/onnotificationclick +tags: + - API + - Experimental + - Interface + - Propriété + - Reference + - ServiceWorkerGlobalScope + - onnotificationclick +translation_of: Web/API/ServiceWorkerGlobalScope/onnotificationclick +--- +<p>{{APIRef("Service Workers API")}}</p> + +<p><span class="seoSummary">La propriété <strong><code>ServiceWorkerGlobalScope.onnotificationclick</code></strong> est un gestionnaire d'événements appelé chaque fois que l'événement {{Event("notificationclick")}} est envoyé sur l'objet {{domxref ("ServiceWorkerGlobalScope")}}, c'est-à-dire lorsqu'un utilisateur clique sur une notification affichée générée par {{domxref("ServiceWorkerRegistration.showNotification()")}}.</span></p> + +<p>Les notifications créées sur le thread principal ou dans un worker qui ne sont pas des service worker utilisant le constructeur {{domxref ("Notification.Notification", "Notification()")}} recevront à la place un événement {{Event("click")}} sur l'objet <code>Notification</code> lui-même.</p> + +<div class="note"> +<p><strong>Note</strong>: Essayer de créer une notification dans le {{domxref ("ServiceWorkerGlobalScope")}} en utilisant le constructeur {{domxref ("Notification.Notification", "Notification()")}} générera une erreur.</p> +</div> + +<h2 id="Syntaxe" style="line-height: 30px; font-size: 2.14285714285714rem;">Syntaxe</h2> + +<pre class="brush: js notranslate" style="font-size: 14px;">ServiceWorkerGlobalScope.onnotificationclick = function(NotificationEvent) { ... } +ServiceWorkerGlobalScope.onnotificationclick = (NotificationEvent) => { ... } +</pre> + +<h2 id="Exemple">Exemple</h2> + +<pre class="brush: js notranslate">self.onnotificationclick = (event) => { + console.log('On notification click: ', event.notification.tag) + event.notification.close() + + // This looks to see if the current is already open and + // focuses if it is + event.waitUntil( + clients + .matchAll({ + type: 'window' + }) + .then((clientList) => { + for (let i = 0; i < clientList.length; i++) { + const client = clientList[i] + if (client.url == '/' && 'focus' in client) return client.focus() + } + if (clients.openWindow) return clients.openWindow('/') + }) + ) +} +</pre> + +<h2 id="Spécifications">Spécifications</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col"><font face="Open Sans, sans-serif"><span style="font-weight: normal;">Spécification</span></font></th> + <th scope="col">État</th> + <th scope="col">Commentaire</th> + </tr> + <tr> + <td>{{SpecName('Web Notifications','#dom-serviceworkerglobalscope-onnotificationclick','onnotificationclick')}}</td> + <td>{{Spec2('Web Notifications')}}</td> + <td>Définition initiale. Cette propriété est spécifiée dans {{domxref ('Notifications_API')}} même si elle fait partie de {{domxref ('ServiceWorkerGlobalScope')}}.</td> + </tr> + </tbody> +</table> + +<h2 id="Compatibilité_des_navigateurs">Compatibilité des navigateurs</h2> + +<div> +<div class="hidden">Le tableau de compatibilité de cette page est généré à partir de données structurées. Si vous souhaitez contribuer aux données, veuillez consulter <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> et nous envoyer une pull request.</div> + +<p>{{Compat("api.ServiceWorkerGlobalScope.onnotificationclick")}}</p> +</div> |