1
1
"use strict" ;
2
2
3
- var version = 4 ;
3
+ var version = 5 ;
4
4
var isOnline = true ;
5
5
var isLoggedIn = false ;
6
6
var cacheName = `ramblings-${ version } ` ;
@@ -26,6 +26,7 @@ var urlsToCache = {
26
26
self . addEventListener ( "install" , onInstall ) ;
27
27
self . addEventListener ( "activate" , onActivate ) ;
28
28
self . addEventListener ( "message" , onMessage ) ;
29
+ self . addEventListener ( "fetch" , onFetch ) ;
29
30
30
31
main ( ) . catch ( console . error ) ;
31
32
@@ -117,3 +118,119 @@ function onMessage({ data }) {
117
118
console . log ( `Service Worker (v${ version } ) status update... isOnline:${ isOnline } , isLoggedIn:${ isLoggedIn } ` ) ;
118
119
}
119
120
}
121
+
122
+ function onFetch ( evt ) {
123
+ evt . respondWith ( router ( evt . request ) ) ;
124
+ }
125
+
126
+ async function router ( req ) {
127
+ var url = new URL ( req . url ) ;
128
+ var reqURL = url . pathname ;
129
+ var cache = await caches . open ( cacheName ) ;
130
+
131
+ // request for site's own URL?
132
+ if ( url . origin == location . origin ) {
133
+ // are we making an API request?
134
+ if ( / ^ \/ a p i \/ .+ $ / . test ( reqURL ) ) {
135
+ let res ;
136
+
137
+ if ( isOnline ) {
138
+ try {
139
+ let fetchOptions = {
140
+ method : req . method ,
141
+ headers : req . headers ,
142
+ credentials : "same-origin" ,
143
+ cache : "no-store" ,
144
+ } ;
145
+ res = await fetch ( req . url , fetchOptions ) ;
146
+ if ( res && res . ok ) {
147
+ if ( req . method == "GET" ) {
148
+ await cache . put ( reqURL , res . clone ( ) ) ;
149
+ }
150
+ return res ;
151
+ }
152
+ }
153
+ catch ( err ) { }
154
+ }
155
+
156
+ res = await cache . match ( reqURL ) ;
157
+ if ( res ) {
158
+ return res ;
159
+ }
160
+
161
+ return notFoundResponse ( ) ;
162
+ }
163
+ // are we requesting a page?
164
+ else if ( req . headers . get ( "Accept" ) . includes ( "text/html" ) ) {
165
+ // login-aware requests?
166
+ if ( / ^ \/ (?: l o g i n | l o g o u t | a d d - p o s t ) $ / . test ( reqURL ) ) {
167
+ // TODO
168
+ }
169
+ // otherwise, just use "network-and-cache"
170
+ else {
171
+ let res ;
172
+
173
+ if ( isOnline ) {
174
+ try {
175
+ let fetchOptions = {
176
+ method : req . method ,
177
+ headers : req . headers ,
178
+ cache : "no-store" ,
179
+ } ;
180
+ res = await fetch ( req . url , fetchOptions ) ;
181
+ if ( res && res . ok ) {
182
+ if ( ! res . headers . get ( "X-Not-Found" ) ) {
183
+ await cache . put ( reqURL , res . clone ( ) ) ;
184
+ }
185
+ return res ;
186
+ }
187
+ }
188
+ catch ( err ) { }
189
+ }
190
+
191
+ // fetch failed, so try the cache
192
+ res = await cache . match ( reqURL ) ;
193
+ if ( res ) {
194
+ return res ;
195
+ }
196
+
197
+ // otherwise, return an offline-friendly page
198
+ return cache . match ( "/offline" ) ;
199
+ }
200
+ }
201
+ // all other files use "cache-first"
202
+ else {
203
+ let res = await cache . match ( reqURL ) ;
204
+ if ( res ) {
205
+ return res ;
206
+ }
207
+ else {
208
+ if ( isOnline ) {
209
+ try {
210
+ let fetchOptions = {
211
+ method : req . method ,
212
+ headers : req . headers ,
213
+ cache : "no-store" ,
214
+ } ;
215
+ res = await fetch ( req . url , fetchOptions ) ;
216
+ if ( res && res . ok ) {
217
+ await cache . put ( reqURL , res . clone ( ) ) ;
218
+ return res ;
219
+ }
220
+ }
221
+ catch ( err ) { }
222
+ }
223
+
224
+ // otherwise, force a network-level 404 response
225
+ return notFoundResponse ( ) ;
226
+ }
227
+ }
228
+ }
229
+ }
230
+
231
+ function notFoundResponse ( ) {
232
+ return new Response ( "" , {
233
+ status : 404 ,
234
+ statusText : "Not Found"
235
+ } ) ;
236
+ }
0 commit comments