1
1
"use strict" ;
2
2
3
- var version = 5 ;
3
+ var version = 6 ;
4
4
var isOnline = true ;
5
5
var isLoggedIn = false ;
6
6
var cacheName = `ramblings-${ version } ` ;
@@ -132,29 +132,15 @@ async function router(req) {
132
132
if ( url . origin == location . origin ) {
133
133
// are we making an API request?
134
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 ) ;
135
+ let fetchOptions = {
136
+ credentials : "same-origin" ,
137
+ cache : "no-store"
138
+ } ;
139
+ let res = await safeRequest ( reqURL , req , fetchOptions , /*cacheResponse=*/ false , /*checkCacheFirst=*/ false , /*checkCacheLast=*/ true , /*useRequestDirectly=*/ true ) ;
157
140
if ( res ) {
141
+ if ( req . method == "GET" ) {
142
+ await cache . put ( reqURL , res . clone ( ) ) ;
143
+ }
158
144
return res ;
159
145
}
160
146
@@ -164,33 +150,125 @@ async function router(req) {
164
150
else if ( req . headers . get ( "Accept" ) . includes ( "text/html" ) ) {
165
151
// login-aware requests?
166
152
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
153
let res ;
172
154
173
- if ( isOnline ) {
174
- try {
155
+ if ( reqURL == "/login" ) {
156
+ if ( isOnline ) {
175
157
let fetchOptions = {
176
158
method : req . method ,
177
159
headers : req . headers ,
160
+ credentials : "same-origin" ,
178
161
cache : "no-store" ,
162
+ redirect : "manual"
179
163
} ;
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 ( ) ) ;
164
+ res = await safeRequest ( reqURL , req , fetchOptions ) ;
165
+ if ( res ) {
166
+ if ( res . type == "opaqueredirect" ) {
167
+ return Response . redirect ( "/add-post" , 307 ) ;
184
168
}
185
169
return res ;
186
170
}
171
+ if ( isLoggedIn ) {
172
+ return Response . redirect ( "/add-post" , 307 ) ;
173
+ }
174
+ res = await cache . match ( "/login" ) ;
175
+ if ( res ) {
176
+ return res ;
177
+ }
178
+ return Response . redirect ( "/" , 307 ) ;
179
+ }
180
+ else if ( isLoggedIn ) {
181
+ return Response . redirect ( "/add-post" , 307 ) ;
182
+ }
183
+ else {
184
+ res = await cache . match ( "/login" ) ;
185
+ if ( res ) {
186
+ return res ;
187
+ }
188
+ return cache . match ( "/offline" ) ;
187
189
}
188
- catch ( err ) { }
189
190
}
190
-
191
- // fetch failed, so try the cache
192
- res = await cache . match ( reqURL ) ;
191
+ else if ( reqURL == "/logout" ) {
192
+ if ( isOnline ) {
193
+ let fetchOptions = {
194
+ method : req . method ,
195
+ headers : req . headers ,
196
+ credentials : "same-origin" ,
197
+ cache : "no-store" ,
198
+ redirect : "manual"
199
+ } ;
200
+ res = await safeRequest ( reqURL , req , fetchOptions ) ;
201
+ if ( res ) {
202
+ if ( res . type == "opaqueredirect" ) {
203
+ return Response . redirect ( "/" , 307 ) ;
204
+ }
205
+ return res ;
206
+ }
207
+ if ( isLoggedIn ) {
208
+ isLoggedIn = false ;
209
+ await sendMessage ( "force-logout" ) ;
210
+ await delay ( 100 ) ;
211
+ }
212
+ return Response . redirect ( "/" , 307 ) ;
213
+ }
214
+ else if ( isLoggedIn ) {
215
+ isLoggedIn = false ;
216
+ await sendMessage ( "force-logout" ) ;
217
+ await delay ( 100 ) ;
218
+ return Response . redirect ( "/" , 307 ) ;
219
+ }
220
+ else {
221
+ return Response . redirect ( "/" , 307 ) ;
222
+ }
223
+ }
224
+ else if ( reqURL == "/add-post" ) {
225
+ if ( isOnline ) {
226
+ let fetchOptions = {
227
+ method : req . method ,
228
+ headers : req . headers ,
229
+ credentials : "same-origin" ,
230
+ cache : "no-store"
231
+ } ;
232
+ res = await safeRequest ( reqURL , req , fetchOptions , /*cacheResponse=*/ true ) ;
233
+ if ( res ) {
234
+ return res ;
235
+ }
236
+ res = await cache . match (
237
+ isLoggedIn ? "/add-post" : "/login"
238
+ ) ;
239
+ if ( res ) {
240
+ return res ;
241
+ }
242
+ return Response . redirect ( "/" , 307 ) ;
243
+ }
244
+ else if ( isLoggedIn ) {
245
+ res = await cache . match ( "/add-post" ) ;
246
+ if ( res ) {
247
+ return res ;
248
+ }
249
+ return cache . match ( "/offline" ) ;
250
+ }
251
+ else {
252
+ res = await cache . match ( "/login" ) ;
253
+ if ( res ) {
254
+ return res ;
255
+ }
256
+ return cache . match ( "/offline" ) ;
257
+ }
258
+ }
259
+ }
260
+ // otherwise, just use "network-and-cache"
261
+ else {
262
+ let fetchOptions = {
263
+ method : req . method ,
264
+ headers : req . headers ,
265
+ cache : "no-store"
266
+ } ;
267
+ let res = await safeRequest ( reqURL , req , fetchOptions , /*cacheResponse=*/ false , /*checkCacheFirst=*/ false , /*checkCacheLast=*/ true ) ;
193
268
if ( res ) {
269
+ if ( ! res . headers . get ( "X-Not-Found" ) ) {
270
+ await cache . put ( reqURL , res . clone ( ) ) ;
271
+ }
194
272
return res ;
195
273
}
196
274
@@ -200,31 +278,57 @@ async function router(req) {
200
278
}
201
279
// all other files use "cache-first"
202
280
else {
203
- let res = await cache . match ( reqURL ) ;
281
+ let fetchOptions = {
282
+ method : req . method ,
283
+ headers : req . headers ,
284
+ cache : "no-store"
285
+ } ;
286
+ let res = await safeRequest ( reqURL , req , fetchOptions , /*cacheResponse=*/ true , /*checkCacheFirst=*/ true ) ;
204
287
if ( res ) {
205
288
return res ;
206
289
}
290
+
291
+ // otherwise, force a network-level 404 response
292
+ return notFoundResponse ( ) ;
293
+ }
294
+ }
295
+ }
296
+
297
+ async function safeRequest ( reqURL , req , options , cacheResponse = false , checkCacheFirst = false , checkCacheLast = false , useRequestDirectly = false ) {
298
+ var cache = await caches . open ( cacheName ) ;
299
+ var res ;
300
+
301
+ if ( checkCacheFirst ) {
302
+ res = await cache . match ( reqURL ) ;
303
+ if ( res ) {
304
+ return res ;
305
+ }
306
+ }
307
+
308
+ if ( isOnline ) {
309
+ try {
310
+ if ( useRequestDirectly ) {
311
+ res = await fetch ( req , options ) ;
312
+ }
207
313
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
- }
314
+ res = await fetch ( req . url , options ) ;
315
+ }
223
316
224
- // otherwise, force a network-level 404 response
225
- return notFoundResponse ( ) ;
317
+ if ( res && ( res . ok || res . type == "opaqueredirect" ) ) {
318
+ if ( cacheResponse ) {
319
+ await cache . put ( reqURL , res . clone ( ) ) ;
320
+ }
321
+ return res ;
226
322
}
227
323
}
324
+ catch ( err ) { }
325
+ }
326
+
327
+ if ( checkCacheLast ) {
328
+ res = await cache . match ( reqURL ) ;
329
+ if ( res ) {
330
+ return res ;
331
+ }
228
332
}
229
333
}
230
334
@@ -234,3 +338,9 @@ function notFoundResponse() {
234
338
statusText : "Not Found"
235
339
} ) ;
236
340
}
341
+
342
+ function delay ( ms ) {
343
+ return new Promise ( function c ( res ) {
344
+ setTimeout ( res , ms ) ;
345
+ } ) ;
346
+ }
0 commit comments