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/ja/web/api/web_periodic_background_synchronization_api | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/ja/web/api/web_periodic_background_synchronization_api')
-rw-r--r-- | files/ja/web/api/web_periodic_background_synchronization_api/index.html | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/files/ja/web/api/web_periodic_background_synchronization_api/index.html b/files/ja/web/api/web_periodic_background_synchronization_api/index.html new file mode 100644 index 0000000000..352325e234 --- /dev/null +++ b/files/ja/web/api/web_periodic_background_synchronization_api/index.html @@ -0,0 +1,126 @@ +--- +title: Web Periodic Background Synchronization API +slug: Web/API/Web_Periodic_Background_Synchronization_API +translation_of: Web/API/Web_Periodic_Background_Synchronization_API +--- +<p>{{securecontext_header}}</p> + +<div>{{draft}}{{DefaultAPISidebar("Periodic Background Sync")}}</div> + +<p class="summary">Web Periodic Background Synchonization APIは、 {{domxref('Service Worker API','service worker')}} 上で定期的にネットワーク通信ができる状況で走るタスクを登録する機能を提供します。それらのタスクを周期的なバックグラウンド同期リクエスト (periodic background sync requests) と呼びます。</p> + +<h2 id="Web_Periodic_Background_Synchronization_の概念と用法">Web Periodic Background Synchronization の概念と用法</h2> + +<p>Periodic Background Sync API により、ウェブアプリケーションが定期的にサービスワーカーにアップデートを行うよう知らせることができます。利用法としては、デバイスがWiFiに接続している間に最新のコンテンツを取得したり、アプリケーションをバックグラウンドでアップデートしたりすることが挙げられます。</p> + +<p>APIが呼び出された際には最小の時間間隔がセットされますが、サービスワーカーがそのイベントを受け取るタイミングに影響を与える様々な要素をユーザーエージェントは考慮します。その要素には、例えばウェブサイトのエンゲージメントや特定のネットワークへの接続などがあります。</p> + +<p>{{domxref('PeriodicSyncManager')}} インターフェースは {{domxref('ServiceWorkerRegistration.periodicSync')}} によって提供されます。一意のタグが sync イベントの 'name' として設定され、これは {{domxref('ServiceWorker')}} スクリプト内で取得することができます。イベントを受け取った際には、キャッシュのアップデートや新たなリソースの取得といった任意の利用可能な機能を実行することができます</p> + +<p>このAPIはサービスワーカーに依存しているため、このAPIも安全なコンテクスト (secure context) でしか利用できません。</p> + +<div class="note"> +<p>At the time of writing, the Web Periodic Background Synchronization API is only available through an installed <a href="https://developer.mozilla.org/en-US/docs/Web/Progressive_web_apps">Progressive Web App</a></p> +</div> + +<h2 id="Web_Periodic_Background_Synchronization_Interfaces">Web Periodic Background Synchronization Interfaces</h2> + +<dl> + <dt>{{domxref('PeriodicSyncManager')}}</dt> + <dd>Registers tasks to be run in a service worker at periodic intervals with network connectivity. These tasks are referred to as periodic background sync requests.</dd> + <dt>{{domxref('PeriodicSyncEvent')}}</dt> + <dd>Represents a synchronization event, sent to the {{domxref('ServiceWorkerGlobalScope', 'global scope')}} of a {{domxref('ServiceWorker')}}. It provides a way to run tasks in the service worker with network connectivity.</dd> +</dl> + +<h2 id="Service_Worker_Additions">Service Worker Additions</h2> + +<p>The following additions to the {{domxref('Service Worker API')}} are specified in the Periodic Background Sync specification to provide an entry point for using Periodic Background Sync.</p> + +<dl> + <dt>{{domxref("ServiceWorkerRegistration.periodicSync")}} {{readonlyinline}}</dt> + <dd>Returns a reference to the {{domxref("PeriodicSyncManager")}} interface for registering tasks to run at specific intervals.</dd> + <dt>{{domxref("ServiceWorkerGlobalScope.onperiodicsync")}}</dt> + <dd>An event handler fired whenever a {{Event("periodicsync")}} event occurs. This happens at timed intervals, that are specified when registering a {{domxref('PeriodicSyncManager')}}.</dd> +</dl> + +<h2 id="Examples">Examples</h2> + +<p>The following examples show how to use the interface.</p> + +<h3 id="Requesting_a_Periodic_Background_Sync">Requesting a Periodic Background Sync</h3> + +<p>The following asynchronous function registers a periodic background sync at a minimum interval of one day from a browsing context:</p> + +<pre class="brush: js notranslate">async function registerPeriodicNewsCheck() { + const registration = await navigator.serviceWorker.ready; + try { + await registration.periodicSync.register('fetch-news', { + minInterval: 24 * 60 * 60 * 1000, + }); + } catch { + console.log('Periodic Sync could not be registered!'); + } +} +</pre> + +<h3 id="Verifying_a_Background_Periodic_Sync_by_Tag">Verifying a Background Periodic Sync by Tag</h3> + +<p>This code checks to see if a Periodic Background Sync task with a given tag is registered.</p> + +<pre class="brush: js notranslate">navigator.serviceWorker.ready.then(registration => { + registration.periodicSync.getTags().then(tags => { + if (tags.includes('get-latest-news')) + skipDownloadingLatestNewsOnPageLoad(); + }); +}); +</pre> + +<h3 id="Removing_a_Periodic_Background_Sync_Task">Removing a Periodic Background Sync Task</h3> + +<p>The following code removes a Periodic Background Sync task to stop articles syncing in the background.</p> + +<pre class="brush: js notranslate">navigator.serviceWorker.ready.then(registration => { + registration.periodicSync.unregister('get-latest-news'); +}); +</pre> + +<h3 id="Listening_for_a_Periodic_Background_Sync_within_a_Service_Worker">Listening for a Periodic Background Sync within a Service Worker</h3> + +<p>The following example shows how to respond to a periodic sync event in the service worker.</p> + +<pre class="brush: js notranslate">self.addEventListener('periodicsync', event => { + if (event.tag == 'get-latest-news') { + event.waitUntil(fetchAndCacheLatestNews()); + } +}); +</pre> + +<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('Periodic Background Sync')}}</td> + <td>{{Spec2('Periodic Background Sync')}}</td> + <td>Initial definition.</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility">Browser compatibility</h2> + + + +<p>{{Compat("api.PeriodicSyncManager")}}</p> + +<h2 id="See_also">See also</h2> + +<ul> + <li><a href="https://web.dev/periodic-background-sync/">An article on using Periodic Background Sync</a></li> + <li><a href="https://webplatformapis.com/periodic_sync/periodicSync_improved.html">A Periodic Background Sync demo app</a></li> +</ul> |