@@ -22,18 +22,18 @@ on your own server.
22
22
The easiest way to start using this module is via the CDN. You just need to
23
23
add the following to your service worker:
24
24
25
- ``` javascript
25
+ < pre class = " prettyprint js " >
26
26
importScripts('{% include "web/tools/workbox/_shared/workbox-sw-cdn-url.html" %}');
27
- ```
27
+ </ pre >
28
28
29
29
With this you’ll have the ` workbox ` namespace in your service worker that will
30
30
provide access to all of the Workbox modules.
31
31
32
- ``` javascript
32
+ < pre class = " prettyprint js " >
33
33
workbox.precaching.*
34
34
workbox.routing.*
35
35
etc
36
- ```
36
+ </ pre >
37
37
38
38
There is some magic that happens as you start to use the additional modules.
39
39
@@ -58,13 +58,13 @@ Release](https://github.com/GoogleChrome/workbox/releases), and then tell
58
58
59
59
If you put the files under ` /third_party/workbox/ ` , you would use them like so:
60
60
61
- ``` javascript
61
+ < pre class = " prettyprint js " >
62
62
importScripts('/third_party/workbox/workbox-sw.js');
63
63
64
64
workbox.setConfig({
65
65
modulePathPrefix: '/third_party/workbox/'
66
66
});
67
- ```
67
+ </ pre >
68
68
69
69
With this, you’ll use only the local Workbox files.
70
70
@@ -82,20 +82,20 @@ In order to avoid violating this restriction, a best practice is to reference th
82
82
83
83
For example, the following top-level service worker code is fine:
84
84
85
- ``` javascript
85
+ < pre class = " prettyprint js " >
86
86
importScripts('{% include "web/tools/workbox/_shared/workbox-sw-cdn-url.html" %}');
87
87
88
88
// This will work!
89
89
workbox.routing.registerRoute(
90
90
new RegExp('\.png$'),
91
91
workbox.strategies.cacheFirst()
92
92
);
93
- ```
93
+ </ pre >
94
94
95
95
But this code could be a problem if you have not referenced ` workbox.strategies ` elsewhere in your
96
96
service worker:
97
97
98
- ``` javascript
98
+ < pre class = " prettyprint js " >
99
99
importScripts('{% include "web/tools/workbox/_shared/workbox-sw-cdn-url.html" %}');
100
100
101
101
self.addEventListener('fetch', (event) => {
@@ -106,13 +106,13 @@ self.addEventListener('fetch', (event) => {
106
106
event.respondWith(cacheFirst.makeRequest({request: event.request}));
107
107
}
108
108
});
109
- ```
109
+ </ pre >
110
110
111
111
If you need to write code that would otherwise run afoul of this restriction, you can explicitly
112
112
trigger the ` importScripts() ` call outside of the event handler by using the
113
113
[ ` workbox.loadModule() ` ] ( /web/tools/workbox/reference-docs/latest/workbox#.loadModule ) method:
114
114
115
- ``` javascript
115
+ < pre class = " prettyprint js " >
116
116
importScripts('{% include "web/tools/workbox/_shared/workbox-sw-cdn-url.html" %}');
117
117
118
118
// This will trigger the importScripts() for workbox.strategies and its dependencies:
@@ -125,12 +125,12 @@ self.addEventListener('fetch', (event) => {
125
125
event.respondWith(cacheFirst.makeRequest({request: event.request}));
126
126
}
127
127
});
128
- ```
128
+ </ pre >
129
129
130
130
Alternatively, you can create a reference to the relevant namespaces outside of your event handlers,
131
131
and then use that reference later on:
132
132
133
- ``` javascript
133
+ < pre class = " prettyprint js " >
134
134
importScripts('{% include "web/tools/workbox/_shared/workbox-sw-cdn-url.html" %}');
135
135
136
136
// This will trigger the importScripts() for workbox.strategies and its dependencies:
@@ -143,7 +143,7 @@ self.addEventListener('fetch', (event) => {
143
143
event.respondWith(cacheFirst.makeRequest({request: event.request}));
144
144
}
145
145
});
146
- ```
146
+ </ pre >
147
147
148
148
Note: Some versions of Chrome do not honor this restriction, and asynchronous calls to
149
149
` importScripts() ` don't trigger the expected failure. Developers are advised * not* to rely on this
@@ -162,11 +162,11 @@ but for any other origin it’ll use the production build.
162
162
If you want to force debug or production builds you set the ` debug ` config
163
163
option.
164
164
165
- ``` javascript
165
+ < pre class = " prettyprint js " >
166
166
workbox.setConfig({
167
167
debug: <true or false >
168
168
});
169
- ```
169
+ </ pre >
170
170
171
171
## Skip Waiting and Clients Claim
172
172
@@ -177,10 +177,10 @@ update and control a web page as soon as possible, skipping the default
177
177
If you find yourself wanting this behavior, ` workbox-sw ` provides some helper
178
178
methods to make this easy:
179
179
180
- ``` javascript
180
+ < pre class = " prettyprint js " >
181
181
workbox.skipWaiting();
182
182
workbox.clientsClaim();
183
- ```
183
+ </ pre >
184
184
185
185
Note: If your web app lazy-loads resources that are uniquely versioned with, e.g., hashes in their
186
186
URLs, it's recommended that you avoid using skip waiting. Enabling it could
0 commit comments