diff --git a/README.md b/README.md index c808d73b..98e60f45 100644 --- a/README.md +++ b/README.md @@ -17,9 +17,12 @@ deno task deploy:statichost diffuse The statichost deploy builds and zips the site with the `zip` task, then uploads `diffuse.zip` via the [direct upload API](https://www.statichost.eu/docs/direct-upload/). It needs a `STATICHOST_APIKEY` (create one at https://builder.statichost.eu/account) and your site name (as an argument or `STATICHOST_SITE`). +Diffuse records the version slug it is running as in `localStorage` (`diffuse/last-version-used`). The generated root `index.html` reads it and redirects there when that version is still deployed, falling back to `/latest/` otherwise. This is what makes `/` return to the version you used last. + ## To do - [x] Generate rules for symlinks - [x] Index page listing all versions - [x] Support version ranges (eg. "4.0.x" for latest patch release) +- [x] Redirect the homepage to the last version used diff --git a/_config.ts b/_config.ts index 6f15b4ad..c991da15 100644 --- a/_config.ts +++ b/_config.ts @@ -26,7 +26,6 @@ site.add("images/background/7.jpg"); site.add("images/maxx-gong-yER6L9sRPXM-unsplash.jpg"); site.add("images/diffuse-current.svg"); -site.add("default-layout.js"); site.add("site.webmanifest"); // DIFFUSE DEPENDENCIES @@ -67,11 +66,6 @@ site.remote( ["**/*"], ); -site.remote( - "default-layout.js", - import.meta.resolve("./diffuse/src/default-layout.js"), -); - site.remote( "site.webmanifest", import.meta.resolve("./diffuse/src/site.webmanifest"), diff --git a/diffuse b/diffuse index 6a04fdfa..cb092621 160000 --- a/diffuse +++ b/diffuse @@ -1 +1 @@ -Subproject commit 6a04fdfae2202fdcdf50e014921a9d600195db36 +Subproject commit cb0926216be88aa5991d93b8c509c827e39eb651 diff --git a/tasks/generate-redirects.ts b/tasks/generate-redirects.ts index 91ce51ed..83bd5771 100644 --- a/tasks/generate-redirects.ts +++ b/tasks/generate-redirects.ts @@ -379,14 +379,14 @@ const oauthRedirectBare: Rule = { status: 302, }; -const root: Rule = { - from: "/", - to: `/${latestRelease.version}/`, - status: 302, -}; +// ROOT +// +// `/` is intentionally not a `_redirects` rule. Hosts serve the generated +// `index.html` (written below) for `/`, and that page has to run before +// choosing a destination: it redirects to the version slug the visitor used +// last, falling back to the `latest` alias. A 302 here would bypass that page. rules = [ - root, oauthMetadata("/"), oauthMetadata("/components/output/raw/atproto/", "/latest"), oauthMetadata("/components/output/raw/atproto-passkey/", "/latest"), @@ -416,14 +416,38 @@ rules.forEach((rule) => { Deno.writeTextFileSync("artifacts/_redirects", redirects); //////////////////////////////////////////// -// `index.html` fallback redirect +// `index.html` root redirect // -// Gateways that honour Netlify-style `_redirects` will serve the root -// rule above. This page is a fallback for gateways that don't: a -// client-side redirect to the latest release. +// This page is served for `/` (the root rule is deliberately absent from +// `_redirects`). It remembers the version slug the visitor last used (recorded +// by Diffuse's `versionUpgrade()`), but only follows it when that slug is still +// registered. Otherwise it falls back to `latest`. //////////////////////////////////////////// -const version = latestRelease.version; +// Every first path segment that can legitimately be visited: versions, +// artifacts, and the semver ranges generated above. `latest` is deliberately +// absent: it is an alias, and the page falls back to it directly. This also +// keeps a stale or tampered localStorage value from becoming an arbitrary +// redirect. +const knownTargets = new Set([ + ...Object.keys(versions), + ...Object.keys(artifacts), +]); + +for (const v of latestByMinor.values()) { + knownTargets.add(`${v.semver.major}.${v.semver.minor}.x`); +} + +for (const v of latestByMajor.values()) { + knownTargets.add(`${v.semver.major}.x`); +} + +const knownTargetsJson = JSON.stringify([...knownTargets].sort()) + .replaceAll("<", "\\u003c"); + +// Prefer the `latest` alias over a concrete version so a cached copy of this +// page still points at the current release after a deploy. +const fallback = "latest"; const indexHtml = ` @@ -431,12 +455,25 @@ const indexHtml = ` Diffuse - - + + -Redirecting to the latest release of Diffuse. - +Redirecting to Diffuse. + `;