id

サービスワーカー (Web)

仕様書

意味

[34] サービスワーカー (service worker) は、 ワーカーの一種です。サービスワーカーは、登録したサービスワーカークライアントの起源において実行されます。 >>33

状態

[35] サービスワーカーは次の状態を持ちます。

状態
サービスワーカーの状態。
スクリプトURL
URL。
型
classic または module。初期値は classic。
containing service worker registration
ID (id)
不透明な文字列。containing service worker registrationの寿命内において固有に当サービスワーカーを識別するものです。 >>33
大域オブジェクト
ServiceWorkerGlobalScope または null。
スクリプト資源
スクリプトまたは null。
スクリプト資源写像
skip waiting flag
imported scripts updated flag
set of event types to handle
set of extended events
list of foreign fetch scopes
list of foreign fetch origins

[38] サービスワーカーは、状態 (state) を持ち、 その値は構文解析済み (parsed) , インストール中 (installing) , インストール済み (installed) , 活性化中 (activating) , 活性化済み (activated) , 冗長 (redundant) のいずれかです。 初期状態は構文解析済みです。 >>33

handle fetch は、状態が活性化中から活性化済みになった時に次の動作を実行します。

[68] サービスワーカーは、走っている (running) か否か (not) のいずれかです。

「走っている」の意味はなぜか定義されていません。 サービスワーカーを走らせる処理やサービスワーカーの終端の処理で参照されます。

[39] サービスワーカーは、サービスワーカー登録に関連付けられています。

[40] サービスワーカー登録はインストール中ワーカー (installing worker) を持ち、 値はインストール中のサービスワーカーまたは null です。 初期値は null です。 >>37

[41] サービスワーカー登録は待機中ワーカー (waiting worker) を持ち、 値はインストール済みのサービスワーカーまたは null です。 初期値は null です。 >>37

[42] サービスワーカー登録は活性ワーカー (active worker) を持ち、 値は活性化中または活性化済みのサービスワーカーまたは null です。 初期値は null です。 >>37


[73] サービスワーカーは、 スクリプトURL (script url) を持ちます >>33。値は URL です。サービスワーカーの作成に使った JavaScript ファイルの URL を表しています。

[74] ジョブは、 スクリプトURL (script url) を持ちます >>72。 値は URL です。

サービスワーカーエージェント

[103] サービスワーカーエージェント (service worker agent) は、 ServiceWorkerGlobalScope を1つ含むエージェントです。 >>104

提供される場面

[23] Service Workers は、保安文脈でしか提供されません。つまり、 HTTPS でない HTTP の文書では原則として使えません。

[27] アプリ内ブラウザーでは提供されないことがあります。 (提供されない可能性が高いかもしれません。)

[102] しばしば Cloudflare Workers がサービスワーカーである、 Service Worker API を提供する、と紹介されることがあります。 実際には Cloudflare Workers はサービスワーカー風 API のうちの一部を、独自に拡張したものを提供しているのですが、 Cloudflare Workers は Web でいうサービスワーカーではありませんし、 サービスワーカーの動作モデルとは違うものです。

[105] なのでそのような解説は Cloudflare 寄りのマーケティング記事か、 無知な著者の記事だと思って構いません。

API

サービスワーカーを扱う API

[71]

サービスワーカーに晒されている API

[22] Sercice Workers のスクリプト (サービスワーカー環境) に晒されている API は、文書環境とは若干異なります。

処理

実行

[43] サービスワーカーを走らせる (Run Service Worker) 処理は、 サービスワーカーワーカーと force bypass cache for importscripts flag について、 次にようにします >>44。

  1. [46] ワーカーが走っているなら、
    1. [47] 成功を返し、ここで停止します。
  2. [45] スクリプトを、ワーカーのスクリプト資源に設定します。
  3. [48] 独立した並列に動作する実行環境を作成し、以後この環境で実行します。
  4. [50] ワーカー大域オブジェクトを、新しい ServiceWorkerGlobalScope に設定します。
  5. [49] Realm実行文脈を、 InitializeHostDefinedRealm を実行した結果に設定します。
    大域オブジェクト
    ワーカー大域オブジェクト
  6. [51] ワーカーの大域オブジェクトを、ワーカー大域オブジェクトに設定します。
  7. [52] 設定群を、新しい環境設定群オブジェクトを作成した結果に設定します。
    Realm実行文脈
    Realm実行文脈
    大域文脈
    ワーカー大域オブジェクト
    ワーカー
    ワーカー
    クライアント
  8. [53] ワーカー大域オブジェクトを、次の通り設定します。
    URL
    ワーカーのスクリプトURL
    HTTPS状態
    ワーカーのスクリプト資源のHTTPS状態
    参照元ポリシー
    ワーカーのスクリプト資源の参照元ポリシー
    型
    ワーカーの型
    force bypass cache for importscripts flag
    force bypass cache for importscripts flag
  9. [54] 新しい WorkerLocation を作成します。
    [55] WorkerLocation
    大域オブジェクト
    ワーカー大域オブジェクト
  10. [56] ワーカーが活性ワーカーで、 ワーカーの containing service worker registration のタスクキュー群の各キューについて、
    1. [57] キューの各タスクについて順に、
      1. [58] タスクをキューに追加します。
        タスクキュー
        ワーカーのイベントループのタスクキュー群のキューに相当するタスクキュー
        処理
        タスクの処理
        タスク源
        タスクのタスク源
  11. [59] スクリプトの種別により、
    古典スクリプト
    スクリプトについて古典スクリプトの実行をします。
    モジュールスクリプト
    スクリプトについてモジュールスクリプトの実行をします。
  12. [60] スクリプトの has ever been evaluated flag が偽の場合、
    1. [61] 設定群の大域オブジェクトのイベントリスナー群の各リスナーについて、
      1. [62] リスナーのイベント型を、ワーカーの set of event types to handle の末尾に追加します。
    2. [63] ワーカーの set of event types to handle が空なら、
      1. [64] 開発者コンソールに警告を出すべき (encouraged) です。
    3. [65] スクリプトの has ever been evaluated flag を、真に設定します。
  13. [66] 設定群の有責イベントループを実行します。
  14. [67] ワーカー大域オブジェクトの活性タイマー群のリストを、空にします。
  15. [76] 以上で実行時スクリプトエラーが捕獲されていなければ失敗を、 そうでなければ成功を返します。 >>77

[75] 次の場面で呼び出されます。

[78] 結果が失敗かどうかは、更新で参照されます。

終了

[36] サービスワーカーを終端させる手順参照。

イベントの処理

[69]

歴史

[21] AppCache も参照。

[1] slightlyoff/NavigationController · GitHub ( ( 版)) https://github.com/slightlyoff/NavigationController/

[2] IRC logs: freenode / #whatwg / 20130422 ( ( 版)) http://krijnhoetmer.nl/irc-logs/whatwg/20130422

[3] ServiceWorker Design https://github.com/slightlyoff/ServiceWorker/

[4] IRC logs: freenode / #whatwg / 20131029 ( ( 版)) http://krijnhoetmer.nl/irc-logs/whatwg/20131029#l-632

[5] Service Workers ( ( 版)) https://gist.github.com/slightlyoff/7fae65a908ac318f69a3

[6] Service Workers ( ( 版)) http://www.w3.org/TR/2014/WD-service-workers-20140508/

[7] Add serviceworker context · f4ffe55 · whatwg/fetch ( ( 版)) https://github.com/whatwg/fetch/commit/f4ffe55d27c6ce48838adaa7833f7de247e79921

[8] Service Workers ( ( 版)) http://www.w3.org/TR/2014/WD-service-workers-20141118/

[9] Service Workers ( ( 版)) http://www.w3.org/TR/2015/WD-service-workers-20150205/

[10] Service worker API reference - Mozilla | MDN ( ( 版)) https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Social_API/Service_worker_API_reference

[11] navigator.connect API ( 版) http://mkruisselbrink.github.io/navigator-connect/

[12] Service Worker Testing - The Chromium Projects ( 版) http://www.chromium.org/blink/serviceworker/testing

[13] Service Workers in Firefox Nightly - wanderview (Ben Kelly 著, 版) https://blog.wanderview.com/blog/2015/03/24/service-workers-in-firefox-nightly/

[14] Service Workers ( ( 版)) http://www.w3.org/TR/2015/WD-service-workers-20150625/

[15] MIX: Further explain the SW implications. · w3c/webappsec@4ead56c ( 版) https://github.com/w3c/webappsec/commit/4ead56ce75e24e29bff67e905ce2335026c6a00d

[16] RfC: Service Workers (Arthur Barstow 著, 版) https://lists.w3.org/Archives/Public/public-webapps/2015JulSep/0374.html

[17] R. NiwaさんはTwitterを使っています: "@slightlylate @hober @kennethrohde @anssik @firt @oriharel and now that the spec is near CR, we're reviewing the spec" ( 版) https://twitter.com/ryosukeniwa/status/648201610800185344

[18] スマートフォン向けサイトに「Service Worker」を日本初導入 「Add to Homescreen」「Offline Cache」の2機能で、よりリッチなオフライン体験が可能に|プレスリリース|リクルート住まいカンパニー ( 版) http://www.recruit-sumai.co.jp/press/2015/10/service-workeradd-to-homescreenoffline-cache2.html

不動産・住宅サイト『SUUMO(スーモ)』のスマートフォン用サイトのリニューアル版を本日10/7(水)公開し、 Webブラウザの最新技術 「Service Worker」 を日本で初導入、「Add to Homescreen」「Offline Cache」の2機能が使用可能となりました。

[19] Using Service Workers - Web APIs | MDN ( 版) https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers

[20] Service Worker の update() ( 版) http://blog.nhiroki.jp/2015/06/22/service-worker-update/

[25] Service Workers - MozillaWiki ( 版) https://wiki.mozilla.org/Service_Workers

[26] Service worker meeting notes - JakeArchibald.com () https://jakearchibald.com/2016/service-worker-meeting-notes/

[28] Service Workers Nightly () https://w3c.github.io/ServiceWorker/

[29] Replace skip-service-worker flag with service-workers mode (jakearchibald著, ) https://github.com/whatwg/fetch/commit/d41c2380dc828e7a23c6196a344b42b2d0e9beec

[30] Add <link rel=serviceworker>+associated attributes (sideshowbarker著, ) https://github.com/whatwg/html/commit/c5d4f5cbee270e81fd7ed49fd62ca2cd26741240

[79] Define agents and agent clusters (annevk著, ) https://github.com/whatwg/html/commit/4db86547f1184e7ceb4f1009cb3bef28101eda4f

[80] Work in progress on Web Platform re-charter; Service Workers WG charter (Advance Notice) (Wendy Seltzer著, ) https://lists.w3.org/Archives/Public/public-new-work/2017May/0006.html

[81] Service Workers Working Group Charter () http://w3c.github.io/charter-drafts/sw-charter.html

[82] Service Workers Working Group Charter Approved; Call for Participation (Xueyuan著, ) https://lists.w3.org/Archives/Public/public-webapps/2017JulSep/0018.html

[83] Service Workers Working Group Charter () https://www.w3.org/2017/08/sw-charter.html

[84] W3C Service Workers Working Group () https://www.w3.org/sw/

[85] Release Notes for Safari Technology Preview 46 | WebKit () https://webkit.org/blog/8042/release-notes-for-safari-technology-preview-46/

[86] Remove <link rel=serviceworker> for now (jakearchibald著, ) https://github.com/whatwg/html/commit/52d09c920e75eec84c037958b7f9aaad84f860d0

[87] Remove foreign fetch by jakearchibald · Pull Request #1207 · w3c/ServiceWorker () https://github.com/w3c/ServiceWorker/pull/1207

[88] Removing link rel=serviceworker for now. by jakearchibald · Pull Request #3233 · whatwg/html () https://github.com/whatwg/html/pull/3233

[89] Deprecations and removals in Chrome 66  |  Web  |  Google Developers () https://developers.google.com/web/updates/2018/03/chrome-66-deprecations

[90] New WebKit Features in Safari 11.1 | WebKit () https://webkit.org/blog/8216/new-webkit-features-in-safari-11-1/

[91] New WebKit Features in Safari 11.1 | WebKit () https://webkit.org/blog/8216/new-webkit-features-in-safari-11-1/

[92] One ServiceWorker object per service worker per realm (#1428) (jakearchibald著, ) https://github.com/w3c/ServiceWorker/commit/d65ed67c0a20078c5503cae6a78b4ab49cfe3038

[93] One ServiceWorker object per service worker per realm by jakearchibald · Pull Request #1428 · w3c/ServiceWorker () https://github.com/w3c/ServiceWorker/pull/1428

[94] Adding self.serviceWorker to service workers. (#1426) (jakearchibald著, ) https://github.com/w3c/ServiceWorker/commit/5580c1d73caa34ea8b9f51346e66a2debda75e27

[95] Adding self.serviceWorker to service workers. by jakearchibald · Pull Request #1426 · w3c/ServiceWorker () https://github.com/w3c/ServiceWorker/pull/1426

[96] Find a way for service workers to get their current state · Issue #1077 · w3c/ServiceWorker () https://github.com/w3c/ServiceWorker/issues/1077

[97] Define service worker events (#1452) (jungkees著, ) https://github.com/w3c/ServiceWorker/commit/b4add25149559ca48245633c0dc045e88ec23d9b

[98] Define service worker events by jungkees · Pull Request #1452 · w3c/ServiceWorker () https://github.com/w3c/ServiceWorker/pull/1452

[99] Add a note about termination during activation. (#1451) (josephliccini著, ) https://github.com/w3c/ServiceWorker/commit/70bf836a49ffa56f953f6c72bcdbb8aa22d170cc

[100] Add a note about termination during activation. by josephliccini · Pull Request #1451 · w3c/ServiceWorker · GitHub () https://github.com/w3c/ServiceWorker/pull/1451

[101] Clarification on what happens during a terminated activation · Issue #1372 · w3c/ServiceWorker · GitHub () https://github.com/w3c/ServiceWorker/issues/1372