1
1
"use strict" ;
2
2
3
- var version = 6 ;
3
+ var version = 7 ;
4
4
var isOnline = true ;
5
5
var isLoggedIn = false ;
6
6
var cacheName = `ramblings-${ version } ` ;
7
+ var allPostsCaching = false ;
7
8
8
9
var urlsToCache = {
9
10
loggedOut : [
@@ -36,6 +37,7 @@ main().catch(console.error);
36
37
async function main ( ) {
37
38
await sendMessage ( { statusUpdateRequest : true } ) ;
38
39
await cacheLoggedOutFiles ( ) ;
40
+ return cacheAllPosts ( ) ;
39
41
}
40
42
41
43
function onInstall ( evt ) {
@@ -52,6 +54,9 @@ async function handleActivation() {
52
54
await cacheLoggedOutFiles ( /*forceReload=*/ true ) ;
53
55
await clients . claim ( ) ;
54
56
console . log ( `Service Worker (v${ version } ) activated` ) ;
57
+
58
+ // spin off background caching of all past posts (over time)
59
+ cacheAllPosts ( /*forceReload=*/ true ) . catch ( console . error ) ;
55
60
}
56
61
57
62
async function clearCaches ( ) {
@@ -101,6 +106,102 @@ async function cacheLoggedOutFiles(forceReload = false) {
101
106
) ;
102
107
}
103
108
109
+ async function cacheAllPosts ( forceReload = false ) {
110
+ // already caching the posts?
111
+ if ( allPostsCaching ) {
112
+ return ;
113
+ }
114
+ allPostsCaching = true ;
115
+ await delay ( 5000 ) ;
116
+
117
+ var cache = await caches . open ( cacheName ) ;
118
+ var postIDs ;
119
+
120
+ try {
121
+ if ( isOnline ) {
122
+ let fetchOptions = {
123
+ method : "GET" ,
124
+ cache : "no-store" ,
125
+ credentials : "omit"
126
+ } ;
127
+ let res = await fetch ( "/api/get-posts" , fetchOptions ) ;
128
+ if ( res && res . ok ) {
129
+ await cache . put ( "/api/get-posts" , res . clone ( ) ) ;
130
+ postIDs = await res . json ( ) ;
131
+ }
132
+ }
133
+ else {
134
+ let res = await cache . match ( "/api/get-posts" ) ;
135
+ if ( res ) {
136
+ let resCopy = res . clone ( ) ;
137
+ postIDs = await res . json ( ) ;
138
+ }
139
+ // caching not started, try to start again (later)
140
+ else {
141
+ allPostsCaching = false ;
142
+ return cacheAllPosts ( forceReload ) ;
143
+ }
144
+ }
145
+ }
146
+ catch ( err ) {
147
+ console . error ( err ) ;
148
+ }
149
+
150
+ if ( postIDs && postIDs . length > 0 ) {
151
+ return cachePost ( postIDs . shift ( ) ) ;
152
+ }
153
+ else {
154
+ allPostsCaching = false ;
155
+ }
156
+
157
+
158
+ // *************************
159
+
160
+ async function cachePost ( postID ) {
161
+ var postURL = `/post/${ postID } ` ;
162
+ var needCaching = true ;
163
+
164
+ if ( ! forceReload ) {
165
+ let res = await cache . match ( postURL ) ;
166
+ if ( res ) {
167
+ needCaching = false ;
168
+ }
169
+ }
170
+
171
+ if ( needCaching ) {
172
+ await delay ( 10000 ) ;
173
+ if ( isOnline ) {
174
+ try {
175
+ let fetchOptions = {
176
+ method : "GET" ,
177
+ cache : "no-store" ,
178
+ credentials : "omit"
179
+ } ;
180
+ let res = await fetch ( postURL , fetchOptions ) ;
181
+ if ( res && res . ok ) {
182
+ await cache . put ( postURL , res . clone ( ) ) ;
183
+ needCaching = false ;
184
+ }
185
+ }
186
+ catch ( err ) { }
187
+ }
188
+
189
+ // failed, try caching this post again?
190
+ if ( needCaching ) {
191
+ return cachePost ( postID ) ;
192
+ }
193
+ }
194
+
195
+ // any more posts to cache?
196
+ if ( postIDs . length > 0 ) {
197
+ return cachePost ( postIDs . shift ( ) ) ;
198
+ }
199
+ else {
200
+ allPostsCaching = false ;
201
+ }
202
+ }
203
+ }
204
+
104
205
async function sendMessage ( msg ) {
105
206
var allClients = await clients . matchAll ( { includeUncontrolled : true , } ) ;
106
207
return Promise . all (
0 commit comments