Skip to content

Commit

Permalink
immutable
Browse files Browse the repository at this point in the history
  • Loading branch information
hazae41 committed Nov 1, 2024
1 parent 2c45bce commit 3b16b39
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 38 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"@hazae41/future": "^1.0.3",
"@hazae41/gardien": "^0.1.1",
"@hazae41/glacier": "^2.0.20",
"@hazae41/immutable": "^1.0.11",
"@hazae41/immutable": "^1.0.12",
"@hazae41/jsonrpc": "^1.1.6",
"@hazae41/kcp": "^1.1.3",
"@hazae41/keccak256": "^1.0.10",
Expand Down
71 changes: 40 additions & 31 deletions public/start.html
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>
1 change: 1 addition & 0 deletions scripts/build.android.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { walkSync } from "./libs/walkSync.mjs";
{
fs.rmSync("./dist/android/action.html")
fs.rmSync("./dist/android/popup.html")
fs.rmSync(`./dist/android/tabbed.html`)

fs.rmSync(`./dist/android/content_script.js`)
fs.rmSync(`./dist/android/injected_script.js`)
Expand Down
1 change: 1 addition & 0 deletions scripts/build.apple.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { walkSync } from "./libs/walkSync.mjs";
{
fs.rmSync("./dist/apple/action.html")
fs.rmSync("./dist/apple/popup.html")
fs.rmSync(`./dist/apple/tabbed.html`)

fs.rmSync(`./dist/apple/content_script.js`)
fs.rmSync(`./dist/apple/injected_script.js`)
Expand Down
12 changes: 10 additions & 2 deletions scripts/build.website.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { walkSync } from "./libs/walkSync.mjs";
{
fs.rmSync(`./dist/website/action.html`)
fs.rmSync(`./dist/website/popup.html`)
fs.rmSync(`./dist/website/tabbed.html`)

fs.rmSync(`./dist/website/content_script.js`)
fs.rmSync(`./dist/website/injected_script.js`)
Expand Down Expand Up @@ -63,8 +64,15 @@ import { walkSync } from "./libs/walkSync.mjs";
if (!filename.endsWith(".html"))
continue

const canonical = path.relative(`./dist/website`, `./${dirname}/_${filename}`)

fs.copyFileSync(pathname, `./${dirname}/_${filename}`)
fs.copyFileSync(`./dist/website/start.html`, pathname)

const original = fs.readFileSync(pathname, "utf8")
const replaced = original.replaceAll(`<link rel="canonical" href="/">`, `<link rel="canonical" href="${canonical}">`)

fs.writeFileSync(pathname, replaced, "utf8")
}

fs.rmSync(`./dist/website/start.html`)
Expand All @@ -85,11 +93,11 @@ import { walkSync } from "./libs/walkSync.mjs";
if (!filename.endsWith(`.html`) && fs.existsSync(`./${dirname}/_${filename}/index`))
continue

const relative = path.relative(`./dist/website`, pathname)

const text = fs.readFileSync(pathname)
const hash = crypto.createHash("sha256").update(text).digest("hex")

const relative = path.relative(`./dist/website`, pathname)

files.push([`/${relative}`, hash])
}

Expand Down

0 comments on commit 3b16b39

Please sign in to comment.