-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
57 additions
and
38 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,53 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<html style="height: 100%; width: 100%;"> | ||
<head> | ||
<link rel="canonical" href="/"> | ||
<script type="module"> | ||
try { | ||
const latestScriptUrl = new URL(`/service_worker.latest.js`, location.href) | ||
const latestScriptRes = await fetch(latestScriptUrl, { cache: "reload" }) | ||
|
||
<head> | ||
<script type="module"> | ||
const message = document.createElement("div") | ||
message.textContent = "Loading..." | ||
document.body.appendChild(message) | ||
if (!latestScriptRes.ok) | ||
throw new Error(`Failed to fetch latest service-worker`) | ||
|
||
try { | ||
const latestScriptUrl = new URL(`/service_worker.latest.js`, location.href) | ||
const latestScriptRes = await fetch(latestScriptUrl, { cache: "reload" }) | ||
const cache = latestScriptRes.headers.get("cache-control") | ||
|
||
if (!cache?.includes("immutable")) | ||
alert("This webapp is not distributed as immutable. Use it at your own risk.") | ||
|
||
if (!latestScriptRes.ok) | ||
throw new Error(`Failed to fetch latest service-worker`) | ||
if (latestScriptRes.headers.get("cache-control") !== "public, max-age=31536000, immutable") | ||
throw new Error(`Wrong Cache-Control header for latest service-worker`) | ||
const ttl = cache?.split(",").map(s => s.trim()).find(s => s.startsWith("max-age="))?.split("=").at(-1) | ||
|
||
const { pathname } = latestScriptUrl | ||
if (ttl !== "31536000") | ||
alert("This webapp is distributed with a time-to-live of less than 1 year. Use it at your own risk.") | ||
|
||
const filename = pathname.split("/").at(-1) | ||
const basename = filename.split(".").at(0) | ||
const { pathname } = latestScriptUrl | ||
|
||
const latestHashBytes = new Uint8Array(await crypto.subtle.digest("SHA-256", await latestScriptRes.arrayBuffer())) | ||
const latestHashRawHex = Array.from(latestHashBytes).map(b => b.toString(16).padStart(2, "0")).join("") | ||
const latestVersion = latestHashRawHex.slice(0, 6) | ||
const filename = pathname.split("/").at(-1) | ||
const basename = filename.split(".").at(0) | ||
|
||
const latestVersionScriptPath = `${basename}.${latestVersion}.js` | ||
const latestVersionScriptUrl = new URL(latestVersionScriptPath, latestScriptUrl) | ||
const latestHashBytes = new Uint8Array(await crypto.subtle.digest("SHA-256", await latestScriptRes.arrayBuffer())) | ||
const latestHashRawHex = Array.from(latestHashBytes).map(b => b.toString(16).padStart(2, "0")).join("") | ||
const latestVersion = latestHashRawHex.slice(0, 6) | ||
|
||
localStorage.setItem("service_worker.current.version", JSON.stringify(latestVersion)) | ||
const latestVersionScriptPath = `${basename}.${latestVersion}.js` | ||
const latestVersionScriptUrl = new URL(latestVersionScriptPath, latestScriptUrl) | ||
|
||
await navigator.serviceWorker.register(latestVersionScriptUrl, { updateViaCache: "all" }) | ||
await navigator.serviceWorker.ready | ||
localStorage.setItem("service_worker.current.version", JSON.stringify(latestVersion)) | ||
|
||
location.reload() | ||
} catch (error) { | ||
message.textContent = "Failed to load." | ||
console.error(error) | ||
} | ||
</script> | ||
</head> | ||
await navigator.serviceWorker.register(latestVersionScriptUrl, { updateViaCache: "all" }) | ||
await navigator.serviceWorker.ready | ||
|
||
location.reload() | ||
} catch (error) { | ||
console.error(error) | ||
|
||
alert(`An error occurred when loading this website. Please try again later.`) | ||
} | ||
</script> | ||
</head> | ||
<body style="height: 100%; width: 100%; display: flex; justify-content: center; align-items: center; overflow: hidden;"> | ||
<img style="width: 100px; height: 100px;" | ||
src="/favicon.png" | ||
alt="favicon" /> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters