-
Notifications
You must be signed in to change notification settings - Fork 313
/
index.bs
4223 lines (3722 loc) · 300 KB
/
index.bs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<pre class='metadata'>
Title: Service Workers Nightly
Status: ED
ED: https://slightlyoff.github.io/ServiceWorker/spec/service_worker/
TR: https://www.w3.org/TR/service-workers/
Shortname: service-workers
Level: 2
Editor: Alex Russell, Google, slightlyoff@chromium.org
Editor: Jungkee Song, Samsung Electronics, jungkee.song@samsung.com
Editor: Jake Archibald, Google, jakearchibald@chromium.org
Editor: Marijn Kruisselbrink, Google, mek@chromium.org
Repository: slightlyoff/ServiceWorker
Group: webplatform
Status Text: This is a living document addressing new features including foreign fetch and header-based installation, etc. Readers need to be aware that this specification includes unstable features. Implementers interested in implementing this specification should refer to the latest TR of <a href="https://slightlyoff.github.io/ServiceWorker/spec/service_worker_1/">Service Workers 1</a> instead.
Abstract: This specification describes a method that enables applications to take advantage of persistent background processing, including hooks to enable bootstrapping of web applications while offline.
Abstract:
Abstract: The core of this system is an event-driven <a>Web Worker</a>, which responds to events dispatched from documents and other sources. A system for managing installation, versions, and upgrades is provided.
Abstract:
Abstract: The service worker is a generic entry point for event-driven background processing in the Web Platform that is <a href="#extensibility">extensible by other specifications</a>.
Ignored Terms: javascript global environment, worker environment, worker environments, document environment, document environments, referrer source, script execution environment, script execution environments, code entry-point, jump to a code entry point, used flag, process response body, http equivalent codes
Markup Shorthands: css no
</pre>
<pre class=biblio>
{
"promises-guide": {
"href": "https://www.w3.org/2001/tag/doc/promises-guide",
"title": "Writing Promise-Using Specifications",
"date": "24 July 2015",
"status": "Finding of the W3C TAG",
"publisher": "W3C TAG"
},
"unsanctioned-tracking": {
"href": "https://www.w3.org/2001/tag/doc/unsanctioned-tracking",
"title": "Unsanctioned Web Tracking",
"date": "17 July 2015",
"status": "Finding of the W3C TAG",
"publisher": "W3C TAG"
}
}
</pre>
<pre class=link-defaults>
spec: dom
type: dfn
text: invoke
type: interface
text: Document
text: Event
text: EventTarget
spec: html; type: dfn
text: about:blank
text: active document
text: allowed to show a popup
text: api base url
text: api url character encoding
text: application cache
text: auxiliary browsing context
text: browsing context
text: discard a document
text: effective script origin
text: entry settings object
text: environment settings object
text: event handler
text: event handler event type
text: event handler idl attributes
text: event loop
text: exceptions enabled
text: focusing steps
text: global object
text: incumbent settings object
text: in parallel
text: kill a worker
text: list of active timers
text: navigate
text: nested browsing context
text: neutered
text: parent browsing context
text: queue a task
text: replacement enabled
text: responsible browsing context
text: responsible document
text: responsible event loop
text: same origin
text: script
text: settings object
text: source browsing context
text: structured clone
text: task sources
text: tasks
text: the worker's documents
text: top-level browsing context
text: transfer a transferable object
text: trusted event
text: unicode serialisation of an origin
text: unload a document
text: user interaction task source
text: utf-8 decode
spec: url; type: dfn
text: is local
spec: webidl; type: dfn;
text: exception
text: partial interface
text: present
</pre>
<pre class=anchors>
spec: dom-ls; urlPrefix: https://dom.spec.whatwg.org/
type: dfn; text: ASCII case-insensitive; url: ascii-case-insensitive
spec: ecma-262; urlPrefix: http://www.ecma-international.org/ecma-262/6.0/
type: dfn
text: Assert; url: sec-algorithm-conventions
text: [[Call]]; url: sec-ecmascript-function-objects-call-thisargument-argumentslist
text: map objects; url: sec-map-objects
text: promise; url: sec-promise-objects
url: sec-list-and-record-specification-type
text: List
text: Record
spec: csp2; urlPrefix: https://w3c.github.io/webappsec-csp/2/
type: dfn
text: enforce
text: monitor
spec: fetch; urlPrefix: https://fetch.spec.whatwg.org/
type: dfn
text: basic filtered response; url: concept-filtered-response-basic
text: CORS filtered response; url: concept-filtered-response-cors
text: disturbed; url: concept-body-disturbed
text: empty; url: concept-empty-readablestream
text: extract a mime type; url: concept-header-extract-mime-type
text: fetch; url: concept-fetch
text: filtered response; url: concept-filtered-response
text: get a reader; url: concept-get-reader
text: header; url: concept-header
text: http fetch; url: concept-http-fetch
text: locked; url: concept-body-locked
text: navigation request
text: network error; url: concept-network-error
text: non-subresource request
text: opaque filtered response; url: concept-filtered-response-opaque
text: potential-navigation-or-subresource request
text: process response
text: process response end-of-file
text: read all bytes; url: concept-read-all-bytes-from-readablestream
text: ReadableStream; url: concept-readablestream
text: request; for: fetch; url: concept-request
text: response; for: fetch; url: concept-response
text: skip service worker flag
text: stream; url: concept-body-stream
text: subresource request
text: synchronous flag
text: terminate; url: concept-fetch-terminate
text: guard; for: headers; url: concept-headers-guard
for: header; urlPrefix: #concept-header-
text: name
text: parsing; url: parse
for: request; urlPrefix: #concept-request-
text: cache mode
text: client
text: destination
text: header list
text: initiator
text: method
text: origin
text: redirect mode
text: request
text: url
for: response; urlPrefix: #concept-response-
text: body
text: cache state
text: header list
text: response
text: status
text: termination reason
text: type
type: interface
text: Headers
text: Request
text: RequestInfo
text: Response
type: attribute; for: Request
text: headers; url: dom-request-headers
type: method
text: get(name); for: Headers; url: dom-headers-get
text: fetch(input, init); for: GlobalFetch; url: dom-global-fetch
spec: html; urlPrefix: https://html.spec.whatwg.org/multipage/
type: dfn
urlPrefix: browsers.html
text: origin; for: resource; url: origin-2
urlPrefix: infrastructure.html
text: read only array; url: dfn-read-only-array
urlPrefix: interaction.html
text: has focus steps
urlPrefix: webappapis.html
text: classic script
text: creation url
text: dom manipulation task source
text: fetch a classic worker script
text: fetch a module script tree
text: fire a simple event
text: https state
text: module script
text: realm execution context
text: report the error
text: run a classic script
text: run a module script
text: script; url: concept-script
text: set up the request; url: fetching-scripts-set-up-request
text: task queue; for: event loop
text: validate the response; url: fetching-scripts-validate-response
urlPrefix: workers.html
text: get a fetch result
text: https state
text: import scripts into worker global scope
text: kill a worker
text: postprocess the fetch result
text: runtime script errors handling; url: runtime-script-errors-2
text: shared workers
text: terminate a worker
text: validate the state
text: web worker; url: workers
for: workerglobalscope; urlPrefix: #concept-workerglobalscope-
text: https state
text: type
text: url
type: event
urlPrefix: indices.html
text: DOMContentLoaded; for: Document; url: event-domcontentloaded
text: message; for: Window; url: event-message
spec: page-visibility; urlPrefix: https://www.w3.org/TR/page-visibility/
type: enum; text: VisibilityState; url: VisibilityState
type: attribute; text: visibilityState; for: Document; url: dom-document-visibilitystate
spec: powerful-features; urlPrefix: https://w3c.github.io/webappsec/specs/powerfulfeatures/#
type: dfn
text: is origin potentially trustworthy; url: is-origin-trustworthy
text: risks associated with insecure contexts; url: threat-risks
text: secure context
spec: promises-guide; urlPrefix: https://www.w3.org/2001/tag/doc/promises-guide#
type: dfn
text: waiting for all
text: transforming; url: transforming-by
spec: quota-api; urlPrefix: http://www.w3.org/TR/quota-api/
type: attribute; for: ServiceWorkerGlobalScope
text: onbeforeevicted; url: widl-ServiceWorkerGlobalScope-onbeforeevicted
text: onevicted; url: widl-ServiceWorkerGlobalScope-onevicted
spec: rfc7230; urlPrefix: https://tools.ietf.org/html/rfc7230
type: dfn
text: field-value; for: http; url: section-3.2
spec: rfc7231; urlPrefix: https://tools.ietf.org/html/rfc7231
type: dfn
text: Vary; url: section-7.1.4
spec: url; urlPrefix: https://url.spec.whatwg.org/
type: dfn
text: parsing; for: url; url: concept-url-parser
text: serialized; for: url; url: concept-url-serializer
spec: webidl; urlPrefix: https://heycam.github.io/webidl/
type: dfn
text: throw; url: dfn-throw
type: exception
text: DataCloneError
text: InvalidAccessError
text: InvalidStateError
text: NetworkError
text: NotFoundError
text: QuotaExceededError
text: SecurityError
</pre>
<section>
<h2 id='motivations'>Motivations</h2>
<em>This section is non-normative.</em>
<p>Web Applications traditionally assume that the network is reachable. This assumption pervades the platform. HTML documents are loaded over HTTP and traditionally fetch all of their sub-resources via subsequent HTTP requests. This places web content at a disadvantage versus other technology stacks.</p>
<p>The <a href="#dfn-service-worker">service worker</a> is designed first to redress this balance by providing a Web Worker context, which can be started by a runtime when navigations are about to occur. This event-driven worker is registered against an origin and a path (or pattern), meaning it can be consulted when navigations occur to that location. Events that correspond to network requests are dispatched to the worker and the responses generated by the worker may override default network stack behavior. This puts the <a href="#dfn-service-worker">service worker</a>, conceptually, between the network and a document renderer, allowing the <a href="#dfn-service-worker">service worker</a> to provide content for documents, even while offline.</p>
<p>Web developers familiar with previous attempts to solve the offline problem have reported a deficit of flexibility in those solutions. As a result, the <a href="#dfn-service-worker">service worker</a> is highly procedural, providing a maximum of flexibility at the price of additional complexity for developers. Part of this complexity arises from the need to keep <a href="#dfn-service-worker">service workers</a> responsive in the face of a single-threaded execution model. As a result, APIs exposed by <a href="#dfn-service-worker">service workers</a> are almost entirely asynchronous, a pattern familiar in other JavaScript contexts but accentuated here by the need to avoid blocking document and resource loading.</p>
<p>Developers using the <a lt="application cache">HTML5 Application Cache</a> have also <a href="http://alistapart.com/article/application-cache-is-a-douchebag">reported that several attributes</a> of the design contribute to <a href="http://alistapart.com/article/application-cache-is-a-douchebag#section6">unrecoverable errors</a>. A key design principle of the <a href="#dfn-service-worker">service worker</a> is that errors should <em>always</em> be recoverable. Many details of the update process of <a href="#dfn-service-worker">service workers</a> are designed to avoid these hazards.</p>
<p><a href="#dfn-service-worker">Service workers</a> are started and kept alive by their relationship to events, not documents. This design borrows heavily from developer and vendor experience with <a>Shared Workers</a> and <a href="https://developer.chrome.com/extensions/background_pages">Chrome Background Pages</a>. A key lesson from these systems is the necessity to time-limit the execution of background processing contexts, both to conserve resources and to ensure that background context loss and restart is top-of-mind for developers. As a result, <a href="#dfn-service-worker">service workers</a> bear more than a passing resemblance to <a href="https://developer.chrome.com/extensions/event_pages">Chrome Event Pages</a>, the successor to Background Pages. <a href="#dfn-service-worker">Service workers</a> may be started by user agents <em>without an attached document</em> and may be killed by the user agent at nearly any time. Conceptually, <a href="#dfn-service-worker">service workers</a> can be thought of as Shared Workers that can start, process events, and die without ever handling messages from documents. Developers are advised to keep in mind that <a href="#dfn-service-worker">service workers</a> may be started and killed many times a second.</p>
<p><a href="#dfn-service-worker">Service workers</a> are generic, event-driven, time-limited script contexts that run at an origin. These properties make them natural endpoints for a range of runtime services that may outlive the context of a particular document, e.g. handling push notifications, background data synchronization, responding to resource requests from other origins, or receiving centralized updates to expensive-to-calculate data (e.g., geolocation or gyroscope).</p>
</section>
<section>
<h2 id="model">Model</h2>
<section dfn-for="service worker">
<h3 id="service-worker-concept">Service Worker</h3>
<p>A <dfn id="dfn-service-worker">service worker</dfn> is a type of <a>web worker</a>. A <a href="#dfn-service-worker">service worker</a> executes in the registering <a href="#dfn-service-worker-client">service worker client</a>'s <a for="resource">origin</a>.</p>
<p>A <a href="#dfn-service-worker">service worker</a> has an associated <dfn id="dfn-state">state</dfn>, which is one of <em>parsed</em>, <em>installing</em>, <em>installed</em>, <em>activating</em>, <em>activated</em>, and <em>redundant</em>. It is initially <em>parsed</em>.</p>
<p>A <a href="#dfn-service-worker">service worker</a> has an associated <dfn id="dfn-script-url">script url</dfn> (a <a for="url">URL</a>).</p>
<p>A <a href="#dfn-service-worker">service worker</a> has an associated <dfn id="dfn-type">type</dfn> which is either "<code>classic</code>" or "<code>module</code>". Unless stated otherwise, it is "<code>classic</code>".</p>
<p>A <a href="#dfn-service-worker">service worker</a> has an associated <dfn id="dfn-containing-service-worker-registration">containing service worker registration</dfn> (a <a href="#dfn-service-worker-registration">service worker registration</a>), which contains itself.</p>
<p>A <a href="#dfn-service-worker">service worker</a> has an associated <dfn id="dfn-service-worker-id">id</dfn> (an opaque string), which uniquely identifies itself during the lifetime of its <a href="#dfn-containing-service-worker-registration">containing service worker registration</a>.</p>
<p>A <a href="#dfn-service-worker">service worker</a> is dispatched a set of <dfn id="dfn-lifecycle-events">lifecycle events</dfn>, <a href="#service-worker-global-scope-install-event">install</a> and <a href="#service-worker-global-scope-activate-event">activate</a>, and <dfn id="dfn-functional-events">functional events</dfn> including <a href="#service-worker-global-scope-fetch-event">fetch</a>.</p>
<p>A <a href="#dfn-service-worker">service worker</a> has an associated <dfn id="dfn-script-resource">script resource</dfn> (a <a>script</a>), which represents its own script resource. It is initially set to null. A <a href="#dfn-script-resource">script resource</a> has an associated <dfn id="dfn-has-ever-been-evaluated-flag">has ever been evaluated flag</dfn>. It is initially unset. A <a href="#dfn-script-resource">script resource</a> has an associated <dfn id="dfn-https-state">HTTPS state</dfn> which is "<code>none</code>", "<code>deprecated</code>", or "<code>modern</code>". Unless stated otherwise, it is "<code>none</code>".</p>
<p>A <a href="#dfn-service-worker">service worker</a> has an associated <dfn id="dfn-script-resource-map">script resource map</dfn> which is a <a>List</a> of the <a>Record</a> {\[[key]], \[[value]]} where \[[key]] is a <a for="url">URL</a> and \[[value]] is a script resource.</p>
<p>A <a href="#dfn-service-worker">service worker</a> has an associated <dfn id="dfn-skip-waiting-flag">skip waiting flag</dfn>. Unless stated otherwise it is unset.</p>
<p>A <a href="#dfn-service-worker">service worker</a> has an associated <dfn id="dfn-imported-scripts-updated-flag">imported scripts updated flag</dfn>. It is initially unset.</p>
<p>A <a href="#dfn-service-worker">service worker</a> has an associated <dfn id="dfn-set-of-event-types-to-handle">set of event types to handle</dfn> whose element type is an <a>event listener</a>'s event type. It is initially set to null.</p>
<p>A <a href="#dfn-service-worker">service worker</a> has an associated <dfn id="dfn-foreign-fetch-scopes">list of foreign fetch scopes</dfn> whose element type is a <a for="url">URL</a>. It is initially empty.</p>
<p>A <a href="#dfn-service-worker">service worker</a> has an associated <dfn id="dfn-foreign-fetch-origins">list of foreign fetch origins</dfn> whose element type is a <a for="url">URL</a>. It is initially empty.</p>
<section>
<h4 id="service-worker-lifetime">Lifetime</h4>
<p>The lifetime of a <a href="#dfn-service-worker">service worker</a> is tied to the execution lifetime of events and not references held by <a href="#dfn-service-worker-client">service worker clients</a> to the <code><a href="#service-worker-interface">ServiceWorker</a></code> object.</p>
<p>A user agent <em class="rfc2119" title="MAY">may</em> <a href="#terminate-service-worker-algorithm">terminate</a> <a href="#dfn-service-worker">service workers</a> at any time it:</p>
<ul>
<li>Has no event to handle.</li>
<li>Detects abnormal operation: such as infinite loops and tasks exceeding imposed time limits (if any) while handling the events.</li>
</ul>
</section>
</section>
<section dfn-for="service worker registration">
<h3 id="service-worker-registration-concept">Service Worker Registration</h3>
<p>A <dfn id="dfn-service-worker-registration">service worker registration</dfn> is a tuple of a <a href="#dfn-scope-url">scope url</a> and a set of <a href="#dfn-service-worker">service workers</a>, an <a href="#dfn-installing-worker">installing worker</a>, a <a href="#dfn-waiting-worker">waiting worker</a>, and an <a href="#dfn-active-worker">active worker</a>. A user agent <em class="rfc2119" title="MAY">may</em> enable many <a href="#dfn-service-worker-registration">service worker registrations</a> at a single origin so long as the <a href="#dfn-scope-url">scope url</a> of the <a href="#dfn-service-worker-registration">service worker registration</a> differs. A <a href="#dfn-service-worker-registration">service worker registration</a> of an identical <a href="#dfn-scope-url">scope url</a> when one already exists in the user agent causes the existing <a href="#dfn-service-worker-registration">service worker registration</a> to be replaced.</p>
<p>A <a href="#dfn-service-worker-registration">service worker registration</a> has an associated <dfn id="dfn-scope-url">scope url</dfn> (a <a for="url">URL</a>).</p>
<p>A <a href="#dfn-service-worker-registration">service worker registration</a> has an associated <dfn id="dfn-registration-script-url">registering script url</dfn> (a <a for="url">URL</a>).</p>
<p>A <a href="#dfn-service-worker-registration">service worker registration</a> has an associated <dfn id="dfn-installing-worker">installing worker</dfn> (a <a href="#dfn-service-worker">service worker</a>) whose <a href="#dfn-state">state</a> is <em>installing</em>. It is initially set to null.</p>
<p>A <a href="#dfn-service-worker-registration">service worker registration</a> has an associated <dfn id="dfn-waiting-worker">waiting worker</dfn> (a <a href="#dfn-service-worker">service worker</a>) whose <a href="#dfn-state">state</a> is <em>installed</em>. It is initially set to null.</p>
<p>A <a href="#dfn-service-worker-registration">service worker registration</a> has an associated <dfn id="dfn-active-worker">active worker</dfn> (a <a href="#dfn-service-worker">service worker</a>) whose <a href="#dfn-state">state</a> is either <em>activating</em> or <em>activated</em>. It is initially set to null.</p>
<p>A <a href="#dfn-service-worker-registration">service worker registration</a> has an associated <dfn id="dfn-last-update-check-time">last update check time</dfn>. It is initially set to null.</p>
<p>A <a href="#dfn-service-worker-registration">service worker registration</a> has an associated <dfn id="dfn-uninstalling-flag">uninstalling flag</dfn>. It is initially unset.</p>
<p>A <a href="#dfn-service-worker-registration">service worker registration</a> has one or more <dfn id="dfn-service-worker-registration-task-queue">task queues</dfn> that back up the <a>tasks</a> from its <a href="#dfn-active-worker">active worker</a>'s <a>event loop</a>'s corresponding <a for="event loop">task queues</a>. (The target task sources for this back up operation are the <a href="#dfn-handle-fetch-task-source">handle fetch task source</a> and the <a href="#dfn-handle-functional-event-task-source">handle functional event task source</a>.) The user agent dumps the <a href="#dfn-active-worker">active worker</a>'s <a>tasks</a> to the <a href="#dfn-service-worker-registration">service worker registration</a>'s <a href="#dfn-service-worker-registration-task-queue">task queues</a> when the <a href="#dfn-active-worker">active worker</a> is <a href="#terminate-service-worker-algorithm">terminated</a> and <a lt="queue a task">re-queues those tasks</a> to the <a href="#dfn-active-worker">active worker</a>'s <a>event loop</a>'s corresponding <a for="event loop">task queues</a> when the <a href="#dfn-active-worker">active worker</a> spins off. Unlike the <a for="event loop">task queues</a> owned by <a>event loops</a>, the <a href="#dfn-service-worker-registration">service worker registration</a>'s <a href="#dfn-service-worker-registration-task-queue">task queues</a> are not processed by any <a>event loops</a> in and of itself.</p>
<section>
<h4 id="service-worker-registration-lifetime">Lifetime</h4>
<p>A user agent <em class="rfc2119" title="MUST">must</em> persistently keep a list of <a href="#register-algorithm">registered</a> <a href="#dfn-service-worker-registration">service worker registrations</a> unless otherwise they are explicitly <a href="#unregister-algorithm">unregistered</a>. A user agent has a <a href="#dfn-scope-to-registration-map">scope to registration map</a> that stores the entries of the tuple of <a href="#dfn-service-worker-registration">service worker registration</a>'s <a href="#dfn-scope-url">scope url</a> and the corresponding <a href="#dfn-service-worker-registration">service worker registration</a>. The lifetime of <a href="#dfn-service-worker-registration">service worker registrations</a> is beyond that of the <code><a href="#service-worker-registration-interface">ServiceWorkerRegistration</a></code> objects which represent them within the lifetime of their corresponding <a href="#dfn-service-worker-client">service worker clients</a>.</p>
</section>
</section>
<section dfn-for="service worker client">
<h3 id="service-worker-client-concept">Service Worker Client</h3>
<p>A <dfn id="dfn-service-worker-client">service worker client</dfn> is an <a>environment settings object</a> that specifies various settings for its <a>JavaScript global environment</a>.</p>
<p>A <a href="#dfn-service-worker-client">service worker client</a> has an associated <dfn id="dfn-service-worker-client-active-worker">active worker</dfn> (an <a href="#dfn-active-worker">active worker</a>) which currently <a href="#dfn-control">controls</a> it. It is initially set to null.</p>
<p>A <a href="#dfn-service-worker-client">service worker client</a> has an associated <dfn id="dfn-service-worker-client-id">id</dfn> (an opaque string), which uniquely identifies itself during its lifetime. It is initially set to a new unique value when the corresponding <a>environment settings object</a> that it represents is created.</p>
<p>A <a href="#dfn-service-worker-client">service worker client</a> has an associated <dfn id="dfn-service-worker-client-frame-type">frame type</dfn>, which is one of <em>auxiliary</em>, <em>top-level</em>, <em>nested</em>, and <em>none</em>. Unless stated otherwise it is <em>none</em>.
<p>A <dfn id="dfn-window-client">window client</dfn> is a <a href="#dfn-service-worker-client">service worker client</a> whose <a>global object</a> is a {{Window}} object.</p>
<p>A <dfn id="dfn-dedicatedworker-client">dedicated worker client</dfn> is a <a href="#dfn-service-worker-client">service worker client</a> whose <a>global object</a> is a {{DedicatedWorkerGlobalScope}} object.</p>
<p>A <dfn id="dfn-sharedworker-client">shared worker client</dfn> is a <a href="#dfn-service-worker-client">service worker client</a> whose <a>global object</a> is a {{SharedWorkerGlobalScope}} object.</p>
<p>A <dfn id="dfn-worker-client">worker client</dfn> is either a <a href="#dfn-dedicatedworker-client">dedicated worker client</a> or a <a href="#dfn-sharedworker-client">shared worker client</a>.</p>
</section>
<section>
<h3 id="selection">Selection and Use</h3>
<p>A <a href="#dfn-service-worker-client">service worker client</a> independently <a href="#dfn-service-worker-registration-selection">selects</a> and <a href="#dfn-use">uses</a> a <a href="#dfn-service-worker-registration">service worker registration</a> for its own loading and its subresources. The <dfn id="dfn-service-worker-registration-selection">selection</dfn> of a <a href="#dfn-service-worker-registration">service worker registration</a>, upon a <a>non-subresource request</a>, is a process of either <a href="#scope-match-algorithm">matching</a> a <a href="#dfn-service-worker-registration">service worker registration</a> from <a href="#dfn-scope-to-registration-map">scope to registration map</a> or inheriting an existing <a href="#dfn-service-worker-registration">service worker registration</a> from its parent or owner context depending on the request's <a for="request">url</a>.</p>
<p>When the request's <a for="request">url</a> is not <a lt="is local">local</a>, a <a href="#dfn-service-worker-client">service worker client</a> <a href="#scope-match-algorithm">matches</a> a <a href="#dfn-service-worker-registration">service worker registration</a> from <a href="#dfn-scope-to-registration-map">scope to registration map</a>. That is, the <a href="#dfn-service-worker-client">service worker client</a> attempts to consult a <a href="#dfn-service-worker-registration">service worker registration</a> whose <a href="#dfn-scope-url">scope url</a> <a href="#scope-match-algorithm">matches</a> its <a>creation url</a>.</p>
<p>When the request's <a for="request">url</a> <a>is local</a>, if the <a href="#dfn-service-worker-client">service worker client</a>'s <a>responsible browsing context</a> is a <a>nested browsing context</a> or the <a href="#dfn-service-worker-client">service worker client</a> is a <a href="#dfn-worker-client">worker client</a>, the <a href="#dfn-service-worker-client">service worker client</a> inherits the <a href="#dfn-service-worker-registration">service worker registration</a> from its <a>parent browsing context</a>'s environment or one of <a>the worker's Documents</a>' environment, respectively, if it exists.</p>
<p>If the <a href="#dfn-service-worker-registration-selection">selection</a> was successful, the <a href="#dfn-service-worker-registration-selection">selected</a> <a href="#dfn-service-worker-registration">service worker registration</a>'s <a href="#dfn-active-worker">active worker</a> starts to <dfn id="dfn-control">control</dfn> the <a href="#dfn-service-worker-client">service worker client</a>. Otherwise, the flow returns to <a>fetch</a> where it falls back to the default behavior. When a <a href="#dfn-service-worker-client">service worker client</a> is <a href="#dfn-control">controlled</a> by an <a href="#dfn-active-worker">active worker</a>, it is considered that the <a href="#dfn-service-worker-client">service worker client</a> is <dfn id="dfn-use">using</dfn> the <a href="#dfn-active-worker">active worker</a>'s <a href="#dfn-containing-service-worker-registration">containing service worker registration</a>.</p>
</section>
<section>
<h3 id="task-sources">Task Sources</h3>
<p>The following additional <a>task sources</a> are used by <a href="#dfn-service-worker">service workers</a>.</p>
<dl>
<dt>The <dfn id="dfn-handle-fetch-task-source">handle fetch task source</dfn></dt>
<dd>This <a>task source</a> is used for <a>dispatching</a> <a href="#service-worker-global-scope-fetch-event">fetch</a> events to <a href="#dfn-service-worker">service workers</a>.</dd>
<dt>The <dfn id="dfn-handle-functional-event-task-source">handle functional event task source</dfn></dt>
<dd>This <a>task source</a> is used for features that <a>dispatch</a> other <a href="#dfn-functional-events">functional</a> events, e.g. <a href="https://w3c.github.io/push-api/#the-push-event">push</a> events, to <a href="#dfn-service-worker">service workers</a>.
<p class="note">A user agent may use a separate task source for each functional event type in order to avoid a head-of-line blocking phenomenon for certain functional events. For instance, a user agent may use a different task source for <a href="http://www.w3.org/2012/sysapps/web-alarms/#firing-task-event-to-service-worker">task</a> events from other task sources.</p>
</dd>
</dl>
</section>
<section>
<h3 id="user-agent-shutdown">User Agent Shutdown</h3>
<p>A user agent <em class="rfc2119" title="MUST">must</em> maintain the state of its stored <a href="#dfn-service-worker-registration">service worker registrations</a> across restarts with the following rules:
<ul>
<li>An <a>installing worker</a> does not persist but discarded. If the <a>installing worker</a> was the only <a href="#dfn-service-worker">service worker</a> for the <a href="#dfn-service-worker-registration">service worker registration</a>, the <a href="#dfn-service-worker-registration">service worker registration</a> is discarded.</li>
<li>A <a>waiting worker</a> promotes to an <a href="#dfn-active-worker">active worker</a>.</li>
</ul>
</p>
<p>To attain this, the user agent <em class="rfc2119" title="MUST">must</em> invoke <a href="#on-user-agent-shutdown-algorithm">Handle User Agent Shutdown</a> when it terminates.</p>
</section>
</section>
<section>
<h2 id="document-context">Client Context</h2>
<div class="example">
Bootstrapping with a ServiceWorker:
<pre class="lang-js">
// scope defaults to the path the script sits in
// "/" in this example
navigator.serviceWorker.register("/serviceworker.js").then(
function(registration) {
console.log("success!");
if (registration.installing) {
registration.installing.postMessage("Howdy from your installing page.");
}
},
function(why) {
console.error("Installing the worker failed!:", why);
});
</pre>
</div>
<section dfn-for="ServiceWorker">
<h3 id="service-worker-obj">{{ServiceWorker}}</h3>
<pre class="idl">
[Exposed=(Window,Worker)]
interface ServiceWorker : EventTarget {
readonly attribute USVString scriptURL;
readonly attribute ServiceWorkerState state;
void postMessage(any message, optional sequence<Transferable> transfer);
// event
attribute EventHandler onstatechange;
};
ServiceWorker implements AbstractWorker;
enum ServiceWorkerState {
"installing",
"installed",
"activating",
"activated",
"redundant"
};
</pre>
<p>A <code><dfn interface id="service-worker-interface">ServiceWorker</dfn></code> object represents a <a href="#dfn-service-worker">service worker</a>. Each {{ServiceWorker}} object is associated with a <a href="#dfn-service-worker">service worker</a>. Multiple separate objects implementing the {{ServiceWorker}} interface across <a>document environments</a> and <a>worker environments</a> can all be associated with the same <a href="#dfn-service-worker">service worker</a> simultaneously.</p>
<p>A {{ServiceWorker}} object has an associated {{ServiceWorkerState}} object which is itself associated with <a href="#dfn-service-worker">service worker</a>'s <a href="#dfn-state">state</a>.</p>
<p>A {{ServiceWorker}} object has an associated <dfn id="dfn-service-worker-interface-client">service worker client</dfn> (a <a href="#dfn-service-worker-client">service worker client</a>).</p>
<section>
<h4 id="service-worker-url">{{ServiceWorker/scriptURL}}</h4>
<p>The <dfn attribute id="service-worker-url-attribute"><code>scriptURL</code></dfn> attribute <em class="rfc2119" title="MUST">must</em> return the <a href="#dfn-service-worker">service worker</a>'s <a for="url">serialized</a> <a href="#dfn-script-url">script url</a>.</p>
<div class="example">
<p>For example, consider a document created by a navigation to <code>https://example.com/app.html</code> which <a href="#on-fetch-request-algorithm">matches</a> via the following registration call which has been previously executed:</p>
<pre class="lang-js">
// Script on the page https://example.com/app.html
navigator.serviceWorker.register("/service_worker.js", { scope: "/" });
</pre>
<p>The value of <code>navigator.serviceWorker.controller.scriptURL</code> will be "<code>https://example.com/service_worker.js</code>".</p>
</div>
</section>
<section>
<h4 id="service-worker-state">{{ServiceWorker/state}}</h4>
<p>The <dfn attribute id="service-worker-state-attribute"><code>state</code></dfn> attribute <em class="rfc2119" title="MUST">must</em> return the value (in <dfn enum id="service-worker-state-enum"><code>ServiceWorkerState</code></dfn> enumeration) corresponding to the first matching statement, switching on the <a href="#dfn-service-worker">service worker</a>'s <a href="#dfn-state">state</a>:</p>
<dl>
<dt><em>installing</em></dt>
<dd>"<code>installing</code>"
<p class="note">The <a href="#dfn-service-worker">service worker</a> in this state is considered an <a href="#dfn-installing-worker">installing worker</a>. During this state, <code><a href="#extendable-event-waituntil-method">event.waitUntil(<var>f</var>)</a></code> can be called inside the <code><a href="#service-worker-global-scope-oninstall-attribute">oninstall</a></code> <a>event handler</a> to extend the life of the <a href="#dfn-installing-worker">installing worker</a> until the passed <a>promise</a> resolves successfully. This is primarily used to ensure that the <a href="#dfn-service-worker">service worker</a> is not active until all of the core caches are populated.</p></dd>
<dt><em>installed</em></dt>
<dd>"<code>installed</code>"
<p class="note">The <a href="#dfn-service-worker">service worker</a> in this state is considered a <a href="#dfn-waiting-worker">waiting worker</a>.</p></dd>
<dt><em>activating</em></dt>
<dd>"<code>activating</code>"
<p class="note">The <a href="#dfn-service-worker">service worker</a> in this state is considered an <a href="#dfn-active-worker">active worker</a>. During this state, <code><a href="#extendable-event-waituntil-method">event.waitUntil(<var>f</var>)</a></code> can be called inside the <code><a href="#service-worker-global-scope-onactivate-attribute">onactivate</a></code> <a>event handler</a> to extend the life of the <a href="#dfn-active-worker">active worker</a> until the passed <a>promise</a> resolves successfully. No <a href="#dfn-functional-events">functional events</a> are dispatched until the state becomes <em>activated</em>.</p></dd>
<dt><em>activated</em></dt>
<dd>"<code>activated</code>"
<p class="note">The <a href="#dfn-service-worker">service worker</a> in this state is considered an <a href="#dfn-active-worker">active worker</a> ready to handle <a href="#dfn-functional-events">functional events</a>.</p></dd>
<dt><em>redundant</em></dt>
<dd>"<code>redundant</code>"
<p class="note">A new <a href="#dfn-service-worker">service worker</a> is replacing the current <a href="#dfn-service-worker">service worker</a>, or the current <a href="#dfn-service-worker">service worker</a> is being discarded due to an install failure.</p></dd>
</dl>
</section>
<section algorithm="service-worker-postmessage">
<h4 id="service-worker-postmessage">{{ServiceWorker/postMessage(message, transfer)}}</h4>
<p>The <dfn method id="service-worker-postmessage-method"><code>postMessage(<var>message</var>, <var>transfer</var>)</code></dfn> method <em class="rfc2119" title="MUST">must</em> run these steps or their <a href="#dfn-processing-equivalence">equivalent</a>:</p>
<ol>
<li>If the {{ServiceWorker/state}} attribute value of the <a>context object</a> is "<code>redundant</code>", <a>throw</a> an "{{InvalidStateError}}" exception and abort these steps. </li>
<li>Let <var>newPorts</var> be an empty array.</li>
<li>Let <var>transferMap</var> be an empty association list of {{Transferable}} objects to placeholder objects.</li>
<li>If the method was invoked with a second argument <var>transfer</var>, run these substeps:
<ol>
<li>If any object is listed in <var>transfer</var> more than once, or any of the {{Transferable}} objects listed in <var>transfer</var> are marked as <a>neutered</a>, then <a>throw</a> a "{{DataCloneError}}" exception and abort these steps.</li>
<li>For each object <var>x</var> in <var>transfer</var> in turn, add a mapping from <var>x</var> to a new unique placeholder object created for <var>x</var> to <var>transferMap</var>, and if <var>x</var> is a {{MessagePort}} object, also append the placeholder object to <var>newPorts</var>.</li>
</ol>
</li>
<li>Let <var>clonedMessage</var> be a <a>structured clone</a> of <var>message</var> with <var>transferMap</var> as the <var>transferMap</var>. If this <a>throws</a> an exception, <a lt="throw">rethrow</a> that exception and abort these steps.</li>
<li>Let <var>serviceWorker</var> be the <a href="#dfn-service-worker">service worker</a> represented by the <a>context object</a>.</li>
<li>Invoke <a href="#run-service-worker-algorithm">Run Service Worker</a> algorithm with <var>serviceWorker</var> as the argument.</li>
<li>Let <var>destination</var> be the {{ServiceWorkerGlobalScope}} object associated with <var>serviceWorker</var>.</li>
<li>If the method was invoked with a second argument <var>transfer</var>, run these substeps:
<ol>
<li>Let <var>newOwner</var> be the <var>destination</var>'s <a>environment settings object</a>.</li>
<li>For each object <var>x</var> in <var>transfer</var> in turn, obtain a new object <var>y</var> by <a lt="transfer a transferable object">transferring</a> the object <var>x</var> to <var>newOwner</var>, and replace the placeholder object that was created for the object <var>x</var> by the new object <var>y</var> wherever the placeholder exists (i.e. in <var>clonedMessage</var> and in <var>newPorts</var>).</li>
</ol>
</li>
<li>Make <var>newPorts</var> into a <a>read only array</a>.</li>
<li><a>Queue a task</a> that runs the following steps:
<ol>
<li>Create an event <var>e</var> that uses the {{ExtendableMessageEvent}} interface, with the event type <a href="#service-worker-global-scope-message-event">message</a>, which does not bubble, is not cancelable, and has no default action.</li>
<li>Let the {{ExtendableMessageEvent/data}} attribute of <var>e</var> be initialized to <var>clonedMessage</var>.</li>
<li>Let the {{ExtendableMessageEvent/origin}} attribute of <var>e</var> be initialized to the <a lt="unicode serialisation of an origin">Unicode serialisation</a> of the <a for="resource">origin</a> specified by the <a>incumbent settings object</a>.</li>
<li>If the <a>global object</a> <var>globalObject</var> specified by the <a>incumbent settings object</a> is a {{ServiceWorkerGlobalScope}} object, let the {{ExtendableMessageEvent/source}} attribute of <var>e</var> be initialized to a new {{ServiceWorker}} object that represents <var>globalObject</var>'s <a href="#dfn-service-worker-global-scope-service-worker">service worker</a>.</li>
<li>Else if <var>globalObject</var> is a {{Window}} object, let the {{ExtendableMessageEvent/source}} attribute of <var>e</var> be initialized to a new {{WindowClient}} object that represents <var>globalObject</var>'s <a>browsing context</a>.</li>
<li>Else, let it be initialized to a new {{Client}} object that represents <var>globalObject</var>'s <a>worker environment</a>.</li>
<li>Let the {{ExtendableMessageEvent/ports}} attribute of <var>e</var> be initialized to <var>newPorts</var>.</li>
<li><a>Dispatch</a> <var>e</var> at <var>destination</var>.</li>
</ol>
<p>The <a>task</a> <em class="rfc2119" title="MUST">must</em> use the <a>DOM manipulation task source</a>.</p>
</li>
</ol>
</section>
<section>
<h4 id="service-worker-event-handler">Event handler</h4>
<p>The following is the <a>event handler</a> (and its corresponding <a>event handler event type</a>) that <em class="rfc2119" title="MUST">must</em> be supported, as <a>event handler IDL attributes</a>, by all objects implementing {{ServiceWorker}} interface:</p>
<table class="data">
<thead>
<tr>
<th><a>event handler</a></th>
<th><a>event handler event type</a></th>
</tr>
</thead>
<tbody>
<tr>
<td><dfn attribute id="service-worker-onstatechange-attribute"><code>onstatechange</code></dfn></td>
<td><code><a href="#service-worker-statechange-event">statechange</a></code></td>
</tr>
</tbody>
</table>
</section>
</section>
<section dfn-for="ServiceWorkerRegistration">
<h3 id="service-worker-registration-obj">{{ServiceWorkerRegistration}}</h3>
<pre class="idl">
[Exposed=(Window,Worker)]
interface ServiceWorkerRegistration : EventTarget {
[Unforgeable] readonly attribute ServiceWorker? installing;
[Unforgeable] readonly attribute ServiceWorker? waiting;
[Unforgeable] readonly attribute ServiceWorker? active;
readonly attribute USVString scope;
[NewObject] Promise<void> update();
[NewObject] Promise<boolean> unregister();
// event
attribute EventHandler onupdatefound;
};
</pre>
<p>A <code><dfn interface id="service-worker-registration-interface">ServiceWorkerRegistration</dfn></code> object represents a <a href="#dfn-service-worker-registration">service worker registration</a>. Each {{ServiceWorkerRegistration}} object is associated with a <dfn id="dfn-service-worker-registration-interface-service-worker-registration">service worker registration</dfn> (a <a href="#dfn-service-worker-registration">service worker registration</a>). Multiple separate objects implementing the {{ServiceWorkerRegistration}} interface across <a>document environments</a> and <a>worker environments</a> can all be associated with the same <a href="#dfn-service-worker-registration">service worker registration</a> simultaneously.</p>
<p>A {{ServiceWorkerRegistration}} object has an associated <dfn id="dfn-service-worker-registration-interface-client">service worker client</dfn> (a <a href="#dfn-service-worker-client">service worker client</a>).</p>
<section algorithm="navigator-service-worker-installing">
<h4 id="navigator-service-worker-installing">{{ServiceWorkerRegistration/installing}}</h4>
<p><dfn attribute id="service-worker-registration-installing-attribute"><code>installing</code></dfn> attribute <em class="rfc2119" title="MUST">must</em> return the result of running these steps or their <a href="#dfn-processing-equivalence">equivalent</a>:</p>
<ol>
<li>Let <var>installingWorker</var> be the {{ServiceWorker}} object that represents the <a href="#dfn-service-worker-registration-interface-service-worker-registration">service worker registration</a>'s <a href="#dfn-installing-worker">installing worker</a>.</li>
<li>Set <var>installingWorker</var>'s <a href="#dfn-service-worker-interface-client">service worker client</a> to the <a href="#dfn-service-worker-registration-interface-client">service worker client</a>.</li>
<li>Return <var>installingWorker</var>.</li>
</ol>
<p class="note">The {{ServiceWorker}} objects returned from this attribute getter that represent the same <a href="#dfn-service-worker">service worker</a> are the same objects.</p>
</section>
<section algorithm="navigator-service-worker-waiting">
<h4 id="navigator-service-worker-waiting">{{ServiceWorkerRegistration/waiting}}</h4>
<p><dfn attribute id="service-worker-registration-waiting-attribute"><code>waiting</code></dfn> attribute <em class="rfc2119" title="MUST">must</em> return the result of running these steps or their <a href="#dfn-processing-equivalence">equivalent</a>:</p>
<ol>
<li>Let <var>waitingWorker</var> be the {{ServiceWorker}} object that represents the <a href="#dfn-service-worker-registration-interface-service-worker-registration">service worker registration</a>'s <a href="#dfn-waiting-worker">waiting worker</a>.</li>
<li>Set <var>waitingWorker</var>'s <a href="#dfn-service-worker-interface-client">service worker client</a> to the <a href="#dfn-service-worker-registration-interface-client">service worker client</a>.</li>
<li>Return <var>waitingWorker</var>.</li>
</ol>
<p class="note">The {{ServiceWorker}} objects returned from this attribute getter that represent the same <a href="#dfn-service-worker">service worker</a> are the same objects.</p>
</section>
<section algorithm="navigator-service-worker-active">
<h4 id="navigator-service-worker-active">{{ServiceWorkerRegistration/active}}</h4>
<p><dfn attribute id="service-worker-registration-active-attribute"><code>active</code></dfn> attribute <em class="rfc2119" title="MUST">must</em> return the result of running these steps or their <a href="#dfn-processing-equivalence">equivalent</a>:</p>
<ol>
<li>Let <var>activeWorker</var> be the {{ServiceWorker}} object that represents the <a href="#dfn-service-worker-registration-interface-service-worker-registration">service worker registration</a>'s <a href="#dfn-active-worker">active worker</a>.</li>
<li>Set <var>activeWorker</var>'s <a href="#dfn-service-worker-interface-client">service worker client</a> to the <a href="#dfn-service-worker-registration-interface-client">service worker client</a>.</li>
<li>Return <var>activeWorker</var>.</li>
</ol>
<p class="note">The {{ServiceWorker}} objects returned from this attribute getter that represent the same <a href="#dfn-service-worker">service worker</a> are the same objects.</p>
</section>
<section algorithm="service-worker-registration-scope">
<h4 id="service-worker-registration-scope">{{ServiceWorkerRegistration/scope}}</h4>
<p>The <dfn attribute id="service-worker-registration-scope-attribute"><code>scope</code></dfn> attribute <em class="rfc2119" title="MUST">must</em> return <a href="#dfn-service-worker-registration-interface-service-worker-registration">service worker registration</a>'s <a for="url">serialized</a> <a href="#dfn-scope-url">scope url</a>.</p>
<p class="example">In the example in section 3.1.1, the value of <code>registration.scope</code>, obtained from <code>navigator.serviceWorker.ready.then(function(registration) { console.log(registration.scope); })</code> for example, will be "<code>https://example.com/</code>".</p>
</section>
<section algorithm="service-worker-registration-update">
<h4 id="service-worker-registration-update">{{ServiceWorkerRegistration/update()}}</h4>
<p><dfn method id="service-worker-registration-update-method"><code>update()</code></dfn> method <em class="rfc2119" title="MUST">must</em> run these steps or their <a href="#dfn-processing-equivalence">equivalent</a>:</p>
<ol>
<li>Let <var>p</var> be a <a>promise</a>.</li>
<li>Let <var>registration</var> be the <a href="#dfn-service-worker-registration-interface-service-worker-registration">service worker registration</a>.</li>
<li>Let <var>newestWorker</var> be the result of running <a href="#get-newest-worker-algorithm">Get Newest Worker</a> algorithm passing <var>registration</var> as its argument.</li>
<li>If <var>newestWorker</var> is null, reject <var>p</var> with an "{{InvalidStateError}}" exception and abort these steps.</li>
<li>Let <var>job</var> be the result of running <a href="#create-job-algorithm">Create Job</a> with <em>update</em>, <var>registration</var>'s <a href="#dfn-scope-url">scope url</a>, <var>newestWorker</var>'s <a href="#dfn-script-url">script url</a>, <var>p</var>, and the <a href="#dfn-service-worker-registration-interface-client">service worker client</a> <var>client</var>.</li>
<li>Run the following substep <a>in parallel</a>:
<ol>
<li>Invoke <a href="#schedule-job-algorithm">Schedule Job</a> with <var>job</var>.</li>
</ol>
</li>
<li>Return <var>p</var>.</li>
</ol>
</section>
<section algorithm="navigator-service-worker-unregister">
<h4 id="navigator-service-worker-unregister">{{ServiceWorkerRegistration/unregister()}}</h4>
<p class="note">The {{ServiceWorkerRegistration/unregister()}} method unregisters the <a href="#dfn-service-worker-registration">service worker registration</a>. It is important to note that the currently <a href="#dfn-control">controlled</a> <a href="#dfn-service-worker-client">service worker client</a>'s <a href="#dfn-service-worker-client-active-worker">active worker</a>'s <a href="#dfn-containing-service-worker-registration">containing service worker registration</a> is effective until all the <a href="#dfn-service-worker-client">service worker clients</a> (including itself) using this <a href="#dfn-service-worker-registration">service worker registration</a> unload. That is, the {{ServiceWorkerRegistration/unregister()}} method only affects subsequent <a lt="navigate">navigations</a>.</p>
<p><dfn method id="service-worker-registration-unregister-method"><code>unregister()</code></dfn> method <em class="rfc2119" title="MUST">must</em> return the result of running these steps or their <a href="#dfn-processing-equivalence">equivalent</a>:</p>
<ol>
<li>Let <var>p</var> be a <a>promise</a>.</li>
<li>Let <var>job</var> be the result of running <a href="#create-job-algorithm">Create Job</a> with <em>unregister</em>, the <a href="#dfn-scope-url">scope url</a> of the <a href="#dfn-service-worker-registration-interface-service-worker-registration">service worker registration</a>, null, <var>p</var>, and the <a href="#dfn-service-worker-registration-interface-client">service worker client</a> <var>client</var>.</li>
<li>Run the following substep <a>in parallel</a>:
<ol>
<li>Invoke <a href="#schedule-job-algorithm">Schedule Job</a> with <var>job</var>.</li>
</ol>
</li>
<li>Return <var>p</var>.</li>
</ol>
</section>
<section>
<h4 id="service-worker-registration-event-handler">Event handler</h4>
<p>The following is the <a>event handler</a> (and its corresponding <a>event handler event type</a>) that <em class="rfc2119" title="MUST">must</em> be supported, as <a>event handler IDL attributes</a>, by all objects implementing <code><a href="#service-worker-registration-interface">ServiceWorkerRegistration</a></code> interface:</p>
<table class="data">
<thead>
<tr>
<th><a>event handler</a></th>
<th><a>event handler event type</a></th>
</tr>
</thead>
<tbody>
<tr>
<td><dfn attribute id="service-worker-registration-onupdatefound-attribute"><code>onupdatefound</code></dfn></td>
<td><code><a href="#service-worker-registration-updatefound-event">updatefound</a></code></td>
</tr>
</tbody>
</table>
</section>
</section>
<section>
<h3 id="navigator-service-worker">{{Navigator/serviceWorker|navigator.serviceWorker}}</h3>
<pre class="idl">
partial interface Navigator {
[SameObject] readonly attribute ServiceWorkerContainer serviceWorker;
};
partial interface WorkerNavigator {
[SameObject] readonly attribute ServiceWorkerContainer serviceWorker;
};
</pre>
<p>The <dfn attribute for="Navigator,WorkerNavigator" id="navigator-service-worker-attribute"><code>serviceWorker</code></dfn> attribute <em class="rfc2119" title="MUST">must</em> return the {{ServiceWorkerContainer}} object that is associated with the <a>context object</a>.</p>
</section>
<section dfn-for="ServiceWorkerContainer">
<h3 id="service-worker-container">{{ServiceWorkerContainer}}</h3>
<pre class="idl">
[Exposed=(Window,Worker)]
interface ServiceWorkerContainer : EventTarget {
[Unforgeable] readonly attribute ServiceWorker? controller;
[SameObject] readonly attribute Promise<ServiceWorkerRegistration> ready;
[NewObject] Promise<ServiceWorkerRegistration> register(USVString scriptURL, optional RegistrationOptions options);
[NewObject] Promise<any> getRegistration(optional USVString clientURL = "");
[NewObject] Promise<sequence<ServiceWorkerRegistration>> getRegistrations();
// events
attribute EventHandler oncontrollerchange;
attribute EventHandler onerror;
attribute EventHandler onmessage; // event.source of message events is ServiceWorker object
};
</pre>
<pre class="idl" id="registration-option-list-dictionary">
dictionary RegistrationOptions {
USVString scope;
WorkerType type = "classic";
};
</pre>
<p>The user agent <em class="rfc2119" title="MUST">must</em> create a {{ServiceWorkerContainer}} object when a {{Navigator}} object or a {{WorkerNavigator}} object is created and associate it with that object.</p>
<p>A <code><dfn interface id="service-worker-container-interface">ServiceWorkerContainer</dfn></code> provides capabilities to register, unregister, and update the <a href="#dfn-service-worker-registration">service worker registrations</a>, and provides access to the state of the <a href="#dfn-service-worker-registration">service worker registrations</a> and their associated <a href="#dfn-service-worker">service workers</a>.</p>
<p>A {{ServiceWorkerContainer}} has an associated <dfn id="dfn-service-worker-container-interface-client">service worker client</dfn>, which is a <a href="#dfn-service-worker-client">service worker client</a> whose <a>global object</a> is associated with the {{Navigator}} object or the {{WorkerNavigator}} object that the {{ServiceWorkerContainer}} is retrieved from.</p>
<p>A {{ServiceWorkerContainer}} object has an associated <dfn id="dfn-ready-promise">ready promise</dfn> (a <a>promise</a>). It is initially set to a new <a>promise</a>.</p>
<section algorithm="navigator-service-worker-controller">
<h4 id="navigator-service-worker-controller">{{ServiceWorkerContainer/controller}}</h4>
<p><dfn attribute id="service-worker-container-controller-attribute"><code>controller</code></dfn> attribute <em class="rfc2119" title="MUST">must</em> return the result of running these steps or their <a href="#dfn-processing-equivalence">equivalent</a>:</p>
<ol>
<li>Let <var>client</var> be the <a>context object</a>'s <a href="#dfn-service-worker-container-interface-client">service worker client</a>.</li>
<li>If <var>client</var> is not a <a>secure context</a>, return undefined.</li>
<li>Return the {{ServiceWorker}} object that represents <var>client</var>'s <a href="#dfn-service-worker-client-active-worker">active worker</a>.</li>
</ol>
<p class="note">{{ServiceWorkerContainer/controller|navigator.serviceWorker.controller}} returns <code>null</code> if the request is a force refresh (shift+refresh). The {{ServiceWorker}} objects returned from this attribute getter that represent the same <a href="#dfn-service-worker">service worker</a> are the same objects.</p>
<p class="issue">The behavior of the attribute getter in non-secure contexts is in discussion.</p>
</section>
<section algorithm="navigator-service-worker-ready">
<h4 id="navigator-service-worker-ready">{{ServiceWorkerContainer/ready}}</h4>
<p><dfn attribute id="service-worker-container-ready-attribute"><code>ready</code></dfn> attribute <em class="rfc2119" title="MUST">must</em> return the result of running these steps or their <a href="#dfn-processing-equivalence">equivalent</a>:</p>
<ol>
<li>If the <a>context object</a>'s <a href="#dfn-ready-promise">ready promise</a> is settled, return the <a>context object</a>'s <a href="#dfn-ready-promise">ready promise</a>.</li>
<li>Let <var>client</var> be the <a>context object</a>'s <a href="#dfn-service-worker-container-interface-client">service worker client</a>.</li>
<li>If <var>client</var> is not a <a>secure context</a>, return the <a>context object</a>'s <a href="#dfn-ready-promise">ready promise</a> rejected with a "{{SecurityError}}" exception.</li>
<li>Let <var>registration</var> be null.</li>
<li>Let <var>clientURL</var> be <var>client</var>'s <a>creation url</a>.</li>
<li>Run the following substeps <a>in parallel</a>:
<ol>
<li><em>CheckRegistration</em>: If the result of running <a href="#scope-match-algorithm">Match Service Worker Registration</a> algorithm, or its <a href="#dfn-processing-equivalence">equivalent</a>, with <var>clientURL</var> as its argument is not null, then:
<ol>
<li>Set <var>registration</var> to the result value.</li>
</ol>
</li>
<li>Else:
<ol>
<li>Wait until <a href="#dfn-scope-to-registration-map">scope to registration map</a> has a new entry.</li>
<li>Jump to the step labeled <em>CheckRegistration</em>.</li>
</ol>
</li>
<li>If <var>registration</var>'s <a href="#dfn-active-worker">active worker</a> is null, wait until <var>registration</var>'s <a href="#dfn-active-worker">active worker</a> changes.
<p class="note">Implementers should consider this condition is met when the corresponding registration request gets to the step 7 of <a href="#activation-algorithm">Activate</a> algorithm.</p>
</li>
<li>Resolve <a>context object</a>'s <a href="#dfn-ready-promise">ready promise</a> with the {{ServiceWorkerRegistration}} object, setting its <a href="#dfn-service-worker-registration-interface-client">service worker client</a> to <a href="#dfn-service-worker-container-interface-client">service worker client</a>, which represents <var>registration</var>.</li>
</ol>
</li>
<li>Return <a>context object</a>'s <a href="#dfn-ready-promise">ready promise</a>.</li>
</ol>
<p class="note">When the <code><a href="#service-worker-container-ready-attribute">ready</a></code> attribute is accessed in a secure context, the returned <a>promise</a> will never reject. Instead, it waits until the <a>promise</a> resolves with a <a href="#dfn-service-worker-registration">service worker registration</a> that has an <a href="#dfn-active-worker">active worker</a>.</p>
</section>
<section algorithm="navigator-service-worker-register">
<h4 id="navigator-service-worker-register">{{ServiceWorkerContainer/register(scriptURL, options)}}</h4>
<p class="note">The {{ServiceWorkerContainer/register(scriptURL, options)}} method creates or updates a <a href="#dfn-service-worker-registration">service worker registration</a> for the given <a href="#dfn-scope-url">scope url</a>. If successful, a <a href="#dfn-service-worker-registration">service worker registration</a> ties the provided <var>scriptURL</var> to a <a href="#dfn-scope-url">scope url</a>, which is subsequently used for <a href="#on-fetch-request-algorithm">navigation matching</a>.</p>
<p><dfn method id="service-worker-container-register-method"><code>register(<var>scriptURL</var>, <var>options</var>)</code></dfn> method <em class="rfc2119" title="MUST">must</em> run these steps or their <a href="#dfn-processing-equivalence">equivalent</a>:</p>
<ol>
<li>Let <var>p</var> be a <a>promise</a>.</li>
<li>Let <var>client</var> be the <a>context object</a>'s <a href="#dfn-service-worker-container-interface-client">service worker client</a>.</li>
<li>If <var>client</var> is not a <a>secure context</a>, reject <var>p</var> with a "{{SecurityError}}" exception and abort these steps.</li>
<li>Let <var>scriptURL</var> be the result of <a for="url">parsing</a> <var>scriptURL</var> with <a>entry settings object</a>'s <a>API base URL</a>.</li>
<li>If <var>scriptURL</var> is failure, reject <var>p</var> with a <code>TypeError</code> and abort these steps.</li>
<li>If <var>scriptURL</var>'s <a for="url">scheme</a> is not one of "<code>http</code>" and "<code>https</code>", reject <var>p</var> with a <code>TypeError</code> and abort these steps.</li>
<li>If any of the strings in <var>scriptURL</var>'s <a for="url">path</a> contains either <a>ASCII case-insensitive</a> "<code>%2f</code>" or <a>ASCII case-insensitive</a> "<code>%5c</code>", reject <var>p</var> with a <code>TypeError</code> and abort these steps.</li>
<li>Let <var>scopeURL</var> be null.</li>
<li>If <var>options</var>.{{RegistrationOptions/scope}} is <a lt="present">not present</a>, set <var>scopeURL</var> to the result of <a for="url">parsing</a> a string "<code>./</code>" with <var>scriptURL</var>.
<p class="note">The scope url for the registration is set to the location of the service worker script by default.</p>
</li>
<li>Else, set <var>scopeURL</var> to the result of <a for="url">parsing</a> <var>options</var>.{{RegistrationOptions/scope}} with <a>entry settings object</a>'s <a>API base URL</a>.</li>
<li>If <var>scopeURL</var> is failure, reject <var>p</var> with a <code>TypeError</code> and abort these steps.</li>
<li>If <var>scopeURL</var>'s <a for="url">scheme</a> is not one of "<code>http</code>" and "<code>https</code>", reject <var>p</var> with a <code>TypeError</code> and abort these steps.</li>
<li>If any of the strings in <var>scopeURL</var>'s <a for="url">path</a> contains either <a>ASCII case-insensitive</a> "<code>%2f</code>" or <a>ASCII case-insensitive</a> "<code>%5c</code>", reject <var>p</var> with a <code>TypeError</code> and abort these steps.</li>
<li>Let <var>job</var> be the result of running <a href="#create-job-algorithm">Create Job</a> with <em>register</em>, <var>scopeURL</var>, <var>scriptURL</var>, <var>options</var>.<a href="#dom-registrationoptions-type">type</a>, <var>p</var>, and <var>client</var>.</li>
<li>Run the following substep <a>in parallel</a>:
<ol>
<li>Invoke <a href="#schedule-job-algorithm">Schedule Job</a> with <var>job</var>.</li>
</ol>
</li>
<li>Return <var>p</var>.</li>
</ol>
</section>
<section algorithm="navigator-service-worker-getRegistration">
<h4 id="navigator-service-worker-getRegistration">{{ServiceWorkerContainer/getRegistration(clientURL)}}</h4>
<p><dfn method id="service-worker-container-getregistration-method"><code>getRegistration(<var>clientURL</var>)</code></dfn> method <em class="rfc2119" title="MUST">must</em> run these steps or their <a href="#dfn-processing-equivalence">equivalent</a>:</p>
<ol>
<li>Let <var>client</var> be the <a>context object</a>'s <a href="#dfn-service-worker-container-interface-client">service worker client</a>.</li>
<li>If <var>client</var> is not a <a>secure context</a>, return a <a>promise</a> rejected with a "{{SecurityError}}" exception.</li>
<li>Let <var>clientURL</var> be the result of <a for="url">parsing</a> <var>clientURL</var> with <a>entry settings object</a>'s <a>API base URL</a>.</li>
<li>If <var>clientURL</var> is failure, return a <a>promise</a> rejected with a <code>TypeError</code>.</li>
<li>If the <a for="resource">origin</a> of <var>clientURL</var> is not <var>client</var>'s <a for="resource">origin</a>, return a <var>promise</var> rejected with a "{{SecurityError}}" exception.</li>
<li>Let <var>promise</var> be a new <a>promise</a>.</li>
<li>Run the following substeps <a>in parallel</a>:
<ol>
<li>Let <var>registration</var> be the result of running <a href="#scope-match-algorithm">Match Service Worker Registration</a> algorithm, or its equivalent, with <var>clientURL</var> as its argument.</li>
<li>If <var>registration</var> is not null, then:
<ol>
<li>Resolve <var>promise</var> with the {{ServiceWorkerRegistration}} object, setting its <a href="#dfn-service-worker-registration-interface-client">service worker client</a> to <var>client</var>, which represents <var>registration</var>.</li>
</ol>
</li>
<li>Else:
<ol>
<li>Resolve <var>promise</var> with undefined.</li>
</ol>
</li>
</ol>
</li>
<li>Return <var>promise</var>.</li>
</ol>
</section>
<section algorithm="navigator-service-worker-getRegistrations">
<h4 id="navigator-service-worker-getRegistrations">{{ServiceWorkerContainer/getRegistrations()}}</h4>
<p><dfn method id="service-worker-container-getregistrations-method"><code>getRegistrations()</code></dfn> method <em class="rfc2119" title="MUST">must</em> run these steps or their <a href="#dfn-processing-equivalence">equivalent</a>:</p>
<ol>
<li>Let <var>client</var> be the <a>context object</a>'s <a href="#dfn-service-worker-container-interface-client">service worker client</a>.</li>
<li>If <var>client</var> is not a <a>secure context</a>, return a <a>promise</a> rejected with a "{{SecurityError}}" exception.</li>
<li>Let <var>promise</var> be a new <a>promise</a>.</li>
<li>Run the following substeps <a>in parallel</a>:
<ol>
<li>Let <var>array</var> be an empty array.</li>
<li>For each <a>Record</a> {\[[key]], \[[value]]} <var>entry</var> of <a href="#dfn-scope-to-registration-map">scope to registration map</a>:
<ol>
<li>If the <a for="resource">origin</a> of the result of <a for="url">parsing</a> <var>entry</var>.\[[key]] is the <a lt="same origin">same</a> as <var>client</var>'s <a for="resource">origin</a>, add the {{ServiceWorkerRegistration}} object, setting its <a href="#dfn-service-worker-registration-interface-client">service worker client</a> to <var>client</var>, associated with <var>entry</var>.\[[value]] to the <var>array</var>.</li>
</ol>
</li>
<li>Resolve <var>promise</var> with <var>array</var>.</li>
</ol>
</li>
<li>Return <var>promise</var>.</li>
</ol>
</section>
<section>
<h4 id="service-worker-container-event-handlers">Event handlers</h4>
<p>The following are the <a>event handlers</a> (and their corresponding <a>event handler event types</a>) that <em class="rfc2119" title="MUST">must</em> be supported, as <a>event handler IDL attributes</a>, by all objects implementing the <a href="#service-worker-container-interface">ServiceWorkerContainer</a> interface:</p>
<table class="data">
<thead>
<tr>
<th><a>event handler</a></th>
<th><a>event handler event type</a></th>
</tr>
</thead>
<tbody>
<tr>
<td><dfn attribute id="service-worker-container-oncontrollerchange-attribute"><code>oncontrollerchange</code></dfn></td>
<td><code><a href="#service-worker-container-controllerchange-event">controllerchange</a></code></td>
</tr>
<tr>
<td><dfn attribute id="service-worker-container-onerror-attribute"><code>onerror</code></dfn></td>
<td><code><a href="#service-worker-container-error-event">error</a></code></td>
</tr>
<tr>
<td><dfn attribute id="service-worker-container-onmessage-attribute"><code>onmessage</code></dfn></td>
<td><code><a href="#service-worker-container-message-event">message</a></code></td>
</tr>
</tbody>
</table>
</section>
</section>
<section dfn-for="ServiceWorkerMessageEvent">
<h3 id="serviceworkermessage-event-section">{{ServiceWorkerMessageEvent}}</h3>
<pre class="idl">
[Constructor(DOMString type, optional ServiceWorkerMessageEventInit eventInitDict), Exposed=(Window,Worker)]
interface ServiceWorkerMessageEvent : Event {
readonly attribute any data;
readonly attribute DOMString origin;
readonly attribute DOMString lastEventId;
[SameObject] readonly attribute (ServiceWorker or MessagePort)? source;
[SameObject] readonly attribute MessagePort[]? ports;
};
</pre>
<pre class="idl" id="serviceworkermessage-event-init-dictionary">
dictionary ServiceWorkerMessageEventInit : EventInit {
any data;
DOMString origin;
DOMString lastEventId;
(ServiceWorker or MessagePort)? source;
sequence<MessagePort>? ports;
};
</pre>
<p><a href="#dfn-service-worker">Service workers</a> define the <a href="#service-worker-container-message-event">message</a> event that extends the {{Window/message}} event defined in [[!HTML]] to allow setting a {{ServiceWorker}} object as the source of the message. For the <a href="#service-worker-container-message-event">message</a> event, <a href="#dfn-service-worker">service workers</a> use the <code><dfn interface id="serviceworkermessage-event-interface">ServiceWorkerMessageEvent</dfn></code> interface.</p>
<section>
<h4 id="serviceworkermessage-event-data">{{ServiceWorkerMessageEvent/data|event.data}}</h4>
<p>The <dfn attribute id="serviceworkermessage-event-data-attribute">data</dfn> attribute <em class="rfc2119" title="MUST">must</em> return the value it was initialized to. When the object is created, this attribute <em class="rfc2119" title="MUST">must</em> be initialized to null. It represents the message being sent.</p>
</section>
<section>
<h4 id="serviceworkermessage-event-origin">{{ServiceWorkerMessageEvent/origin|event.origin}}</h4>
<p>The <dfn attribute id="serviceworkermessage-event-origin-attribute">origin</dfn> attribute <em class="rfc2119" title="MUST">must</em> return the value it was initialized to. When the object is created, this attribute <em class="rfc2119" title="MUST">must</em> be initialized to the empty string. It represents the <a for="resource">origin</a> of the <a href="#dfn-service-worker">service worker</a>'s <a>environment settings object</a> from which the message is sent.</p>
</section>
<section>
<h4 id="serviceworkermessage-event-lasteventid">{{ServiceWorkerMessageEvent/lastEventId|event.lastEventId}}</h4>
<p>The <dfn attribute id="serviceworkermessage-event-lasteventid-attribute">lastEventId</dfn> attribute <em class="rfc2119" title="MUST">must</em> return the value it was initialized to. When the object is created, this attribute <em class="rfc2119" title="MUST">must</em> be initialized to the empty string.</p>
</section>
<section>
<h4 id="serviceworkermessage-event-source">{{ServiceWorkerMessageEvent/source|event.source}}</h4>
<p>The <dfn attribute id="serviceworkermessage-event-source-attribute">source</dfn> attribute <em class="rfc2119" title="MUST">must</em> return the value it was initialized to. When the object is created, this attribute <em class="rfc2119" title="MUST">must</em> be initialized to null. It represents the <code><a href="#service-worker-interface">ServiceWorker</a></code> object whose associated <a href="#dfn-service-worker">service worker</a> the message is sent from.</p>
</section>
<section>
<h4 id="serviceworkermessage-event-ports">{{ServiceWorkerMessageEvent/ports|event.ports}}</h4>
<p>The <dfn attribute id="serviceworkermessage-event-ports-attribute">ports</dfn> attribute <em class="rfc2119" title="MUST">must</em> return the value it was initialized to. When the object is created, this attribute <em class="rfc2119" title="MUST">must</em> be initialized to null. It represents the {{MessagePort}} array being sent, if any.</p>
</section>
</section>
<section>
<h3 id="document-context-events">Events</h3>
<p>The following event is dispatched on {{ServiceWorker}} object:</p>
<table class="data">
<thead>
<tr>
<th>Event name</th>
<th>Interface</th>
<th>Dispatched when…</th>
</tr>
</thead>