Skip to content

Commit 46aeead

Browse files
committed
sw: caching all posts in the background
1 parent c906c97 commit 46aeead

File tree

1 file changed

+102
-1
lines changed

1 file changed

+102
-1
lines changed

web/js/sw.js

+102-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
"use strict";
22

3-
var version = 6;
3+
var version = 7;
44
var isOnline = true;
55
var isLoggedIn = false;
66
var cacheName = `ramblings-${version}`;
7+
var allPostsCaching = false;
78

89
var urlsToCache = {
910
loggedOut: [
@@ -36,6 +37,7 @@ main().catch(console.error);
3637
async function main() {
3738
await sendMessage({ statusUpdateRequest: true });
3839
await cacheLoggedOutFiles();
40+
return cacheAllPosts();
3941
}
4042

4143
function onInstall(evt) {
@@ -52,6 +54,9 @@ async function handleActivation() {
5254
await cacheLoggedOutFiles(/*forceReload=*/true);
5355
await clients.claim();
5456
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);
5560
}
5661

5762
async function clearCaches() {
@@ -101,6 +106,102 @@ async function cacheLoggedOutFiles(forceReload = false) {
101106
);
102107
}
103108

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+
104205
async function sendMessage(msg) {
105206
var allClients = await clients.matchAll({ includeUncontrolled: true, });
106207
return Promise.all(

0 commit comments

Comments
 (0)