Skip to content

Commit

Permalink
Fix demo cache
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanaobrien committed Jan 8, 2025
1 parent 0dde5d6 commit e1c8d4d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ <h1>Settings</h1>
});
}
input.addEventListener("change", async () => {
const url = URL.createObjectURL(new Blob([input.files[0]]));
const url = input.files[0];
const parts = input.files[0].name.split(".");

const core = await (async (ext) => {
Expand Down
15 changes: 9 additions & 6 deletions sw.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const CACHE_NAME = "offline-demo";
const STABLE_EJS_VER = "4.2.1";
const OFFLINE_FILES = [
"index.html",
"404.html",
Expand Down Expand Up @@ -43,7 +44,7 @@ function getCacheUrl(url) {
} else if (url.startsWith("/")) {
return url.slice(1);
}
return url;
return url.replace(STABLE_EJS_VER, "stable");
}

self.addEventListener("fetch", (event) => {
Expand All @@ -52,23 +53,25 @@ self.addEventListener("fetch", (event) => {
const requestURL = new URL(event.request.url);
let url = (requestURL.hostname === "cdn.emulatorjs.org") ? event.request.url : requestURL.pathname;
const cache = await caches.open(CACHE_NAME);
if (requestURL.hostname === "cdn.emulatorjs.org" && !OFFLINE_FILES.includes(event.request.url)) {
if (requestURL.hostname === "cdn.emulatorjs.org" && !OFFLINE_FILES.includes(event.request.url.replace(STABLE_EJS_VER, "stable")) && !event.request.url.includes("reports/")) {
return await fetch(event.request);
}
try {
const req = (url === "/versions") ? "https://cdn.emulatorjs.org/versions.json" : event.request;
const res = await fetch(req);
if (!res.status.toString().startsWith('2')) {
if (!res.ok && res.status !== 0) {
throw new Error('status code not ok');
}
cache.put(getCacheUrl(url), res.clone());
return res;
} catch(e) {
console.log("error:", e);
url = getCacheUrl(url);
if (!OFFLINE_FILES.includes(url)) {
url = "404.html";
let rv = await cache.match(url);
if (rv === undefined) {
rv = await cache.match("404.html");
}
return await cache.match(url);
return rv;
}
})()
);
Expand Down

0 comments on commit e1c8d4d

Please sign in to comment.