-
Notifications
You must be signed in to change notification settings - Fork 0
/
serviceWorkers.html
273 lines (235 loc) · 31 KB
/
serviceWorkers.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset = "UTF-8">
<meta name = "viewport" content = "width = device-width">
<!-- Use the title from a page's frontmatter if it has one -->
<title>Skeletonxf.github.io</title>
<link rel = "stylesheet" href = "stylesheets/highlighting.css">
<link rel = "stylesheet" href = "stylesheets/styles.css">
</head>
<body>
<div class = "page">
<header class = "back">
<nav>
<ul>
<li>
<p><a href = "index.html">Index</a></p>
</li>
</ul>
</nav>
</header>
<h1 class = "center title">
A blog of sorts
</h1>
<div class = "content">
<article>
<h1>Avoiding corrupted content errors with Service Workers</h1>
<p class = "article-date">2020/03/03</p>
<p>At the time of writing I just finished writing my second service worker for a website and ran into another bunch of Corrupted Content Errors.</p>
<p>There are several ways you can cause this and none of them are particularly easy to Google the solution to because if you just search for “Corrupted Content Error” all you see are these errors in production sites that end users have experienced.</p>
<h2>Service Worker 101</h2>
<p>To understand why these errors can be introduced into service worker code by accident it is helpful to recap how a service worker works.</p>
<p>You will probably register the service worker using code like the following example in a JavaScript file that is executed on every webpage of your site.</p>
<div class="highlight"><pre class="highlight javascript"><code><span class="k">if</span> <span class="p">(</span><span class="dl">'</span><span class="s1">serviceWorker</span><span class="dl">'</span> <span class="k">in</span> <span class="nb">navigator</span><span class="p">)</span> <span class="p">{</span>
<span class="nb">navigator</span><span class="p">.</span><span class="nx">serviceWorker</span><span class="p">.</span><span class="nx">register</span><span class="p">(</span><span class="dl">'</span><span class="s1">/service-worker.js</span><span class="dl">'</span><span class="p">)</span>
<span class="p">}</span>
</code></pre></div>
<p>Then in a JavaScript file which the web server makes public at <code>/service-worker.js</code> (or whatever you’ve named it as) you put the actual service worker (not in the <code><head></code> of any of your webpages).</p>
<p>The service worker will probably contain an <code>install</code> event, and a <code>fetch</code> event which will look a little bit like below:</p>
<div class="highlight"><pre class="highlight javascript"><code><span class="kd">const</span> <span class="nx">CACHE_VERSION</span> <span class="o">=</span> <span class="dl">'</span><span class="s1">v1</span><span class="dl">'</span>
<span class="kd">const</span> <span class="nx">CACHE_NAME</span> <span class="o">=</span> <span class="s2">`cache-name`</span>
<span class="kd">const</span> <span class="nx">CACHE_FILE_URLS</span> <span class="o">=</span> <span class="p">[</span>
<span class="dl">'</span><span class="s1">/</span><span class="dl">'</span><span class="p">,</span>
<span class="dl">'</span><span class="s1">/foo</span><span class="dl">'</span><span class="p">,</span>
<span class="dl">'</span><span class="s1">/foobar</span><span class="dl">'</span>
<span class="p">]</span>
<span class="nb">self</span><span class="p">.</span><span class="nx">addEventListener</span><span class="p">(</span><span class="dl">'</span><span class="s1">install</span><span class="dl">'</span><span class="p">,</span> <span class="p">(</span><span class="nx">event</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span>
<span class="nx">event</span><span class="p">.</span><span class="nx">waitUntil</span><span class="p">(</span>
<span class="nx">caches</span><span class="p">.</span><span class="nx">open</span><span class="p">(</span><span class="nx">CACHE_NAME</span><span class="p">).</span><span class="nx">then</span><span class="p">((</span><span class="nx">cache</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">cache</span><span class="p">.</span><span class="nx">addAll</span><span class="p">(</span><span class="nx">CACHE_FILE_URLS</span><span class="p">)</span>
<span class="p">})</span>
<span class="p">)</span>
<span class="p">})</span>
<span class="nb">self</span><span class="p">.</span><span class="nx">addEventListener</span><span class="p">(</span><span class="dl">'</span><span class="s1">fetch</span><span class="dl">'</span><span class="p">,</span> <span class="p">(</span><span class="nx">event</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span>
<span class="nx">event</span><span class="p">.</span><span class="nx">respondWith</span><span class="p">(...)</span>
<span class="p">})</span>
</code></pre></div>
<p>This rough structure will add all the files listed in <code>CACHE_FILE_URLS</code> to the service worker’s cache when it installs. Once the service worker is installed the service worker will listen to every <code>fetch</code> event the browser makes for the webpage and be able to intercept it.</p>
<p>So far so good.</p>
<p><a href="https://developer.mozilla.org/en-US/docs/Web/Progressive_web_apps/Offline_Service_workers">MDN gives an example</a> for the fetch event which will perform a strategy of cache then network</p>
<div class="highlight"><pre class="highlight javascript"><code><span class="nb">self</span><span class="p">.</span><span class="nx">addEventListener</span><span class="p">(</span><span class="dl">'</span><span class="s1">fetch</span><span class="dl">'</span><span class="p">,</span> <span class="p">(</span><span class="nx">e</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span>
<span class="nx">e</span><span class="p">.</span><span class="nx">respondWith</span><span class="p">(</span>
<span class="nx">caches</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="nx">e</span><span class="p">.</span><span class="nx">request</span><span class="p">).</span><span class="nx">then</span><span class="p">((</span><span class="nx">r</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="dl">'</span><span class="s1">[Service Worker] Fetching resource: </span><span class="dl">'</span><span class="o">+</span><span class="nx">e</span><span class="p">.</span><span class="nx">request</span><span class="p">.</span><span class="nx">url</span><span class="p">)</span>
<span class="k">return</span> <span class="nx">r</span> <span class="o">||</span> <span class="nx">fetch</span><span class="p">(</span><span class="nx">e</span><span class="p">.</span><span class="nx">request</span><span class="p">).</span><span class="nx">then</span><span class="p">((</span><span class="nx">response</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">caches</span><span class="p">.</span><span class="nx">open</span><span class="p">(</span><span class="nx">CACHE_NAME</span><span class="p">).</span><span class="nx">then</span><span class="p">((</span><span class="nx">cache</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="dl">'</span><span class="s1">[Service Worker] Caching new resource: </span><span class="dl">'</span><span class="o">+</span><span class="nx">e</span><span class="p">.</span><span class="nx">request</span><span class="p">.</span><span class="nx">url</span><span class="p">)</span>
<span class="nx">cache</span><span class="p">.</span><span class="nx">put</span><span class="p">(</span><span class="nx">e</span><span class="p">.</span><span class="nx">request</span><span class="p">,</span> <span class="nx">response</span><span class="p">.</span><span class="nx">clone</span><span class="p">())</span>
<span class="k">return</span> <span class="nx">response</span>
<span class="p">})</span>
<span class="p">})</span>
<span class="p">})</span>
<span class="p">)</span>
<span class="p">})</span>
</code></pre></div>
<p>Understanding the asyncronous control flow can be a little tricky using <code>.then()</code>s so presented below is the ES7 version</p>
<div class="highlight"><pre class="highlight javascript"><code><span class="nb">self</span><span class="p">.</span><span class="nx">addEventListener</span><span class="p">(</span><span class="dl">'</span><span class="s1">fetch</span><span class="dl">'</span><span class="p">,</span> <span class="p">(</span><span class="nx">e</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span>
<span class="nx">e</span><span class="p">.</span><span class="nx">respondWith</span><span class="p">(</span><span class="k">async</span> <span class="p">()</span> <span class="o">=></span> <span class="p">{</span>
<span class="kd">const</span> <span class="nx">cacheResponse</span> <span class="o">=</span> <span class="k">await</span> <span class="nx">caches</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="nx">e</span><span class="p">.</span><span class="nx">request</span><span class="p">)</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="dl">'</span><span class="s1">[Service Worker] Fetching resource: </span><span class="dl">'</span><span class="o">+</span><span class="nx">e</span><span class="p">.</span><span class="nx">request</span><span class="p">.</span><span class="nx">url</span><span class="p">)</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">cacheResponse</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">cacheResponse</span>
<span class="p">}</span>
<span class="kd">const</span> <span class="nx">networkResponse</span> <span class="o">=</span> <span class="k">await</span> <span class="nx">fetch</span><span class="p">(</span><span class="nx">e</span><span class="p">.</span><span class="nx">request</span><span class="p">)</span>
<span class="kd">const</span> <span class="nx">cache</span> <span class="o">=</span> <span class="k">await</span> <span class="nx">caches</span><span class="p">.</span><span class="nx">open</span><span class="p">(</span><span class="nx">CACHE_NAME</span><span class="p">)</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="dl">'</span><span class="s1">[Service Worker] Caching new resource: </span><span class="dl">'</span><span class="o">+</span><span class="nx">e</span><span class="p">.</span><span class="nx">request</span><span class="p">.</span><span class="nx">url</span><span class="p">)</span>
<span class="nx">cache</span><span class="p">.</span><span class="nx">put</span><span class="p">(</span><span class="nx">e</span><span class="p">.</span><span class="nx">request</span><span class="p">,</span> <span class="nx">networkResponse</span><span class="p">.</span><span class="nx">clone</span><span class="p">())</span>
<span class="k">return</span> <span class="nx">networkResponse</span>
<span class="p">})</span>
<span class="p">})</span>
</code></pre></div>
<p>Whoops!</p>
<p><img src = "./images/sw-cce-1.png"
alt = "Corrupted Content Error Screenshot"
title = "Corrupted Content Error Screenshot">
</img></p>
<p>I must have made a mistake. Now I can’t load any pages for this site until I go to <code>about:serviceworkers</code> and unregister the service worker.</p>
<h2>The broken Service Worker skeleton</h2>
<p><code>/service-worker.js</code></p>
<div class="highlight"><pre class="highlight javascript"><code><span class="kd">const</span> <span class="nx">CACHE_VERSION</span> <span class="o">=</span> <span class="dl">'</span><span class="s1">v1</span><span class="dl">'</span>
<span class="kd">const</span> <span class="nx">CACHE_NAME</span> <span class="o">=</span> <span class="s2">`cache-name`</span>
<span class="kd">const</span> <span class="nx">CACHE_FILE_URLS</span> <span class="o">=</span> <span class="p">[</span>
<span class="dl">'</span><span class="s1">/index.html</span><span class="dl">'</span><span class="p">,</span>
<span class="p">]</span>
<span class="nb">self</span><span class="p">.</span><span class="nx">addEventListener</span><span class="p">(</span><span class="dl">'</span><span class="s1">install</span><span class="dl">'</span><span class="p">,</span> <span class="p">(</span><span class="nx">event</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span>
<span class="nx">event</span><span class="p">.</span><span class="nx">waitUntil</span><span class="p">(</span>
<span class="nx">caches</span><span class="p">.</span><span class="nx">open</span><span class="p">(</span><span class="nx">CACHE_NAME</span><span class="p">).</span><span class="nx">then</span><span class="p">((</span><span class="nx">cache</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">cache</span><span class="p">.</span><span class="nx">addAll</span><span class="p">(</span><span class="nx">CACHE_FILE_URLS</span><span class="p">)</span>
<span class="p">})</span>
<span class="p">)</span>
<span class="p">})</span>
<span class="nb">self</span><span class="p">.</span><span class="nx">addEventListener</span><span class="p">(</span><span class="dl">'</span><span class="s1">fetch</span><span class="dl">'</span><span class="p">,</span> <span class="p">(</span><span class="nx">e</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span>
<span class="nx">e</span><span class="p">.</span><span class="nx">respondWith</span><span class="p">(</span><span class="k">async</span> <span class="p">()</span> <span class="o">=></span> <span class="p">{</span>
<span class="kd">const</span> <span class="nx">cacheResponse</span> <span class="o">=</span> <span class="k">await</span> <span class="nx">caches</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="nx">e</span><span class="p">.</span><span class="nx">request</span><span class="p">)</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="dl">'</span><span class="s1">[Service Worker] Fetching resource: </span><span class="dl">'</span><span class="o">+</span><span class="nx">e</span><span class="p">.</span><span class="nx">request</span><span class="p">.</span><span class="nx">url</span><span class="p">)</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">cacheResponse</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">cacheResponse</span>
<span class="p">}</span>
<span class="kd">const</span> <span class="nx">networkResponse</span> <span class="o">=</span> <span class="k">await</span> <span class="nx">fetch</span><span class="p">(</span><span class="nx">e</span><span class="p">.</span><span class="nx">request</span><span class="p">)</span>
<span class="kd">const</span> <span class="nx">cache</span> <span class="o">=</span> <span class="k">await</span> <span class="nx">caches</span><span class="p">.</span><span class="nx">open</span><span class="p">(</span><span class="nx">CACHE_NAME</span><span class="p">)</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="dl">'</span><span class="s1">[Service Worker] Caching new resource: </span><span class="dl">'</span><span class="o">+</span><span class="nx">e</span><span class="p">.</span><span class="nx">request</span><span class="p">.</span><span class="nx">url</span><span class="p">)</span>
<span class="nx">cache</span><span class="p">.</span><span class="nx">put</span><span class="p">(</span><span class="nx">e</span><span class="p">.</span><span class="nx">request</span><span class="p">,</span> <span class="nx">networkResponse</span><span class="p">.</span><span class="nx">clone</span><span class="p">())</span>
<span class="k">return</span> <span class="nx">networkResponse</span>
<span class="p">})</span>
<span class="p">})</span>
</code></pre></div>
<p><code>/sw-test.html</code></p>
<div class="highlight"><pre class="highlight html"><code><span class="nt"><html></span>
<span class="nt"><head></span>
<span class="nt"><meta</span> <span class="na">charset=</span><span class="s">"UTF8"</span><span class="nt">></span>
<span class="nt"><script></span>
<span class="k">if</span> <span class="p">(</span><span class="dl">'</span><span class="s1">serviceWorker</span><span class="dl">'</span> <span class="k">in</span> <span class="nb">navigator</span><span class="p">)</span> <span class="p">{</span>
<span class="nb">navigator</span><span class="p">.</span><span class="nx">serviceWorker</span><span class="p">.</span><span class="nx">register</span><span class="p">(</span><span class="dl">'</span><span class="s1">/service-worker.js</span><span class="dl">'</span><span class="p">)</span>
<span class="p">}</span>
<span class="nt"></script></span>
<span class="nt"></head></span>
<span class="nt"><body></span>
<span class="nt"><p></span>The page loaded!<span class="nt"></p></span>
<span class="nt"></body></span>
<span class="nt"></html></span>
</code></pre></div>
<p>Note that to replicate this bug you have to serve these files over HTTPS or localhost. The browser will refuse to install the service worker if you try to use <code>file:///</code></p>
<h2>Issue One</h2>
<p>The mistake I made was that I passed <code>respondWith</code> a function. It needs to receive a <code>Promise</code> that resolves to a <code>Response</code>.</p>
<p>The solution is to immediately call the asyncronous function I defined, as async functions always return promises.</p>
<div class="highlight"><pre class="highlight javascript"><code><span class="nb">self</span><span class="p">.</span><span class="nx">addEventListener</span><span class="p">(</span><span class="dl">'</span><span class="s1">fetch</span><span class="dl">'</span><span class="p">,</span> <span class="p">(</span><span class="nx">e</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span>
<span class="nx">e</span><span class="p">.</span><span class="nx">respondWith</span><span class="p">((</span><span class="k">async</span> <span class="p">()</span> <span class="o">=></span> <span class="p">{</span>
<span class="kd">const</span> <span class="nx">cacheResponse</span> <span class="o">=</span> <span class="k">await</span> <span class="nx">caches</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="nx">e</span><span class="p">.</span><span class="nx">request</span><span class="p">)</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="dl">'</span><span class="s1">[Service Worker] Fetching resource: </span><span class="dl">'</span><span class="o">+</span><span class="nx">e</span><span class="p">.</span><span class="nx">request</span><span class="p">.</span><span class="nx">url</span><span class="p">)</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">cacheResponse</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">cacheResponse</span>
<span class="p">}</span>
<span class="kd">const</span> <span class="nx">networkResponse</span> <span class="o">=</span> <span class="k">await</span> <span class="nx">fetch</span><span class="p">(</span><span class="nx">e</span><span class="p">.</span><span class="nx">request</span><span class="p">)</span>
<span class="kd">const</span> <span class="nx">cache</span> <span class="o">=</span> <span class="k">await</span> <span class="nx">caches</span><span class="p">.</span><span class="nx">open</span><span class="p">(</span><span class="nx">CACHE_NAME</span><span class="p">)</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="dl">'</span><span class="s1">[Service Worker] Caching new resource: </span><span class="dl">'</span><span class="o">+</span><span class="nx">e</span><span class="p">.</span><span class="nx">request</span><span class="p">.</span><span class="nx">url</span><span class="p">)</span>
<span class="nx">cache</span><span class="p">.</span><span class="nx">put</span><span class="p">(</span><span class="nx">e</span><span class="p">.</span><span class="nx">request</span><span class="p">,</span> <span class="nx">networkResponse</span><span class="p">.</span><span class="nx">clone</span><span class="p">())</span>
<span class="k">return</span> <span class="nx">networkResponse</span>
<span class="p">})())</span>
<span class="p">})</span>
</code></pre></div>
<p>It’s easy to miss this step even when following a tutorial that does it because the <code>})()</code> at the end is after the function body.</p>
<p>Now I can stop the server and continue to load the cached webpage and the service worker will fetch it from the cache for me.</p>
<h2>Pages not in cache or network</h2>
<p>What happens if I try a different URL?</p>
<p>Let’s try <code>/sw-test</code> instead of <code>/sw-test.html</code></p>
<p><img src = "./images/sw-cce-2.png"
alt = "Corrupted Content Error Screenshot"
title = "Corrupted Content Error Screenshot">
</img></p>
<p>Yet another Corrupted Content Error</p>
<p>Aside from the issue that the service worker code doesn’t realise the resource at <code>/sw-test</code> and <code>/sw-test.html</code> is the same, and it has <code>/sw-test.html</code> in the cache, the main issue is that if you pass a promise that fails to resolve to a response you still get a Corrupted Content Error!</p>
<p><code>fetch()</code> rejects if there’s a network error, which must be caught so we can still resolve to a response.</p>
<div class="highlight"><pre class="highlight javascript"><code><span class="nb">self</span><span class="p">.</span><span class="nx">addEventListener</span><span class="p">(</span><span class="dl">'</span><span class="s1">fetch</span><span class="dl">'</span><span class="p">,</span> <span class="p">(</span><span class="nx">e</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span>
<span class="nx">e</span><span class="p">.</span><span class="nx">respondWith</span><span class="p">((</span><span class="k">async</span> <span class="p">()</span> <span class="o">=></span> <span class="p">{</span>
<span class="kd">const</span> <span class="nx">cacheResponse</span> <span class="o">=</span> <span class="k">await</span> <span class="nx">caches</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="nx">e</span><span class="p">.</span><span class="nx">request</span><span class="p">)</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="dl">'</span><span class="s1">[Service Worker] Fetching resource: </span><span class="dl">'</span><span class="o">+</span><span class="nx">e</span><span class="p">.</span><span class="nx">request</span><span class="p">.</span><span class="nx">url</span><span class="p">)</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">cacheResponse</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">cacheResponse</span>
<span class="p">}</span>
<span class="k">try</span> <span class="p">{</span>
<span class="kd">const</span> <span class="nx">networkResponse</span> <span class="o">=</span> <span class="k">await</span> <span class="nx">fetch</span><span class="p">(</span><span class="nx">e</span><span class="p">.</span><span class="nx">request</span><span class="p">)</span>
<span class="kd">const</span> <span class="nx">cache</span> <span class="o">=</span> <span class="k">await</span> <span class="nx">caches</span><span class="p">.</span><span class="nx">open</span><span class="p">(</span><span class="nx">CACHE_NAME</span><span class="p">)</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="dl">'</span><span class="s1">[Service Worker] Caching new resource: </span><span class="dl">'</span><span class="o">+</span><span class="nx">e</span><span class="p">.</span><span class="nx">request</span><span class="p">.</span><span class="nx">url</span><span class="p">)</span>
<span class="nx">cache</span><span class="p">.</span><span class="nx">put</span><span class="p">(</span><span class="nx">e</span><span class="p">.</span><span class="nx">request</span><span class="p">,</span> <span class="nx">networkResponse</span><span class="p">.</span><span class="nx">clone</span><span class="p">())</span>
<span class="k">return</span> <span class="nx">networkResponse</span>
<span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="nx">error</span><span class="p">)</span> <span class="p">{</span>
<span class="c1">// network error</span>
<span class="p">}</span>
<span class="k">return</span> <span class="k">new</span> <span class="nx">Response</span><span class="p">(</span><span class="dl">'</span><span class="s1"><p>Not in cache 😢</p></span><span class="dl">'</span><span class="p">)</span>
<span class="p">})())</span>
<span class="p">})</span>
</code></pre></div>
<p>What about <code>match()</code> or <code>open()</code>? <a href="https://developer.mozilla.org/en-US/docs/Web/API/Cache/match"><code>match()</code></a> always resolves to either the response or <code>undefined</code> and <a href="https://developer.mozilla.org/en-US/docs/Web/API/CacheStorage/open"><code>open()</code></a> always resolves to the cache object, and will make one if it didn’t exist.</p>
<h2>Working versions</h2>
<p>Finally working offline</p>
<p><img src = "./images/sw-cce-3.png"
alt = "Fallback resource when offline screenshot"
title = "Fallback resource when offline screenshot">
</img></p>
<p>Immediately after adding the above image I tried refreshing my browser to see the updated article. Because the service worker is still set to cache then network and it has a cache of the article, it’s going to prevent my browser fetching the new one from my server.</p>
<p>Hence below is the network then cache version for completeness.</p>
<div class="highlight"><pre class="highlight javascript"><code><span class="nb">self</span><span class="p">.</span><span class="nx">addEventListener</span><span class="p">(</span><span class="dl">'</span><span class="s1">fetch</span><span class="dl">'</span><span class="p">,</span> <span class="p">(</span><span class="nx">e</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span>
<span class="nx">e</span><span class="p">.</span><span class="nx">respondWith</span><span class="p">((</span><span class="k">async</span> <span class="p">()</span> <span class="o">=></span> <span class="p">{</span>
<span class="k">try</span> <span class="p">{</span>
<span class="kd">const</span> <span class="nx">networkResponse</span> <span class="o">=</span> <span class="k">await</span> <span class="nx">fetch</span><span class="p">(</span><span class="nx">e</span><span class="p">.</span><span class="nx">request</span><span class="p">)</span>
<span class="kd">const</span> <span class="nx">cache</span> <span class="o">=</span> <span class="k">await</span> <span class="nx">caches</span><span class="p">.</span><span class="nx">open</span><span class="p">(</span><span class="nx">CACHE_NAME</span><span class="p">)</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="dl">'</span><span class="s1">[Service Worker] Caching updated resource: </span><span class="dl">'</span><span class="o">+</span><span class="nx">e</span><span class="p">.</span><span class="nx">request</span><span class="p">.</span><span class="nx">url</span><span class="p">)</span>
<span class="nx">cache</span><span class="p">.</span><span class="nx">put</span><span class="p">(</span><span class="nx">e</span><span class="p">.</span><span class="nx">request</span><span class="p">,</span> <span class="nx">networkResponse</span><span class="p">.</span><span class="nx">clone</span><span class="p">())</span>
<span class="k">return</span> <span class="nx">networkResponse</span>
<span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="nx">error</span><span class="p">)</span> <span class="p">{</span>
<span class="c1">// network error</span>
<span class="p">}</span>
<span class="kd">const</span> <span class="nx">cacheResponse</span> <span class="o">=</span> <span class="k">await</span> <span class="nx">caches</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="nx">e</span><span class="p">.</span><span class="nx">request</span><span class="p">)</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="dl">'</span><span class="s1">[Service Worker] Fetching resource: </span><span class="dl">'</span><span class="o">+</span><span class="nx">e</span><span class="p">.</span><span class="nx">request</span><span class="p">.</span><span class="nx">url</span><span class="p">)</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">cacheResponse</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">cacheResponse</span>
<span class="p">}</span>
<span class="k">return</span> <span class="k">new</span> <span class="nx">Response</span><span class="p">(</span><span class="dl">'</span><span class="s1"><p>Not in cache 😢</p></span><span class="dl">'</span><span class="p">)</span>
<span class="p">})())</span>
<span class="p">})</span>
</code></pre></div>
<p>On a real website you probably don’t want to indiscriminately cache everything and probably want a proper webpage to return if the cache and network fail, but you should now be able to avoid two easy mistakes that cause Corrupted Content Errors.</p>
<hr>
<p>All of the code examples in this article are MIT licensed.</p>
</article>
</div>
</div>
<footer>
<div class = "footer">
<p>My content on this site is licensed under Creative Commons By Attribution <a href="https://creativecommons.org/licenses/by/4.0/">https://creativecommons.org/licenses/by/4.0/</a></p>
<p>This site's source code is licensed under the MIT license <a href="https://github.com/Skeletonxf/Skeletonxf.github.io/tree/code">https://github.com/Skeletonxf/Skeletonxf.github.io/tree/code</a></p>
</div>
</footer>
</body>
</html>