diff --git a/_routes.json b/_routes.json index f37e407..708a79a 100644 --- a/_routes.json +++ b/_routes.json @@ -1,5 +1,5 @@ { "version": 1, - "include": ["/about/*", "/now/*", "/blog/*", "/projects/*", "/api/*"], + "include": ["/about/*", "/now/*", "/blog/*", "/projects", "/projects/*", "/api/*"], "exclude": [] } diff --git a/social/README.md b/social/README.md index 28f5a85..730f46a 100644 --- a/social/README.md +++ b/social/README.md @@ -10,6 +10,7 @@ likes and comments. everything for them lives in this folder, and none of it is | `functions/_middleware.js` | runs before a page is served and fills its `data-social` section from d1 | | `functions/api/like.js` | the like button posts here | | `functions/api/moderate.js` | the approve/delete page linked from new-comment emails | +| `functions/projects/` | not social: `/projects/` is showcase at code.danieldaum.net, redirected or proxied | | `email.js` | email worker: turns mail sent to `reply@replies.danieldaum.net` into comments | | `schema.sql` | the d1 tables: `Likes`, `LikeVoters`, `Comments` | | `wrangler.toml` | pages config (d1 binding) | @@ -44,13 +45,13 @@ put this at the end of the post, inside `
` if it has one. the template ``` -this works on `/about/`, `/now//`, `/blog//` and `/projects//`. take the section out and the page has none. +this works on `/about/`, `/now//` and `/blog//`. `/projects/` is showcase (`functions/projects/`), so it has none. take the section out and the page has none. `/now/` is a copy of the newest edition, so it points at that edition: `data-social="/now//"`. update it with each new now post. comments always go on the edition, never on `/now/`. ### a new kind of page (e.g. `/books//`) -1. add it to `isLikeable()` in `lib.js`: `(blog|now|projects)` → `(blog|now|projects|books)` +1. add it to `isLikeable()` in `lib.js`: `(blog|now)` → `(blog|now|books)` 2. add `"/books/*"` to `/_routes.json`. every path in there runs a function and isn't cached, so keep it narrow 3. add the section to the pages 4. `mise run social-email`: the worker uses `isLikeable()` too, so it rejects comments for the new pages until it's redeployed diff --git a/social/functions/projects/[[path]].js b/social/functions/projects/[[path]].js new file mode 100644 index 0000000..5db842e --- /dev/null +++ b/social/functions/projects/[[path]].js @@ -0,0 +1,24 @@ +// /projects/ is showcase, the code site at code.danieldaum.net, not files in +// this repo (the old pages under projects/ are never served). +// +// MODE "redirect": send visitors to code.danieldaum.net. +// MODE "proxy": serve showcase here at /projects/. Run showcase with +// "base_path": "/projects" first, so its links stay under /projects/. +// Clone, go get and docker pull stay on code.danieldaum.net. +const ORIGIN = "https://code.danieldaum.net"; +const MODE = "redirect"; + +export async function onRequest({ request }) { + const url = new URL(request.url); + if (MODE === "redirect") { + const rest = url.pathname.replace(/^\/projects/, "") || "/"; + return Response.redirect(ORIGIN + rest + url.search, 302); + } + if (request.method !== "GET" && request.method !== "HEAD") { + return new Response("not found", { status: 404 }); + } + // the origin is fixed: only showcase's own pages can come back + const upstream = new Request(ORIGIN + url.pathname + url.search, request); + upstream.headers.delete("Cookie"); + return fetch(upstream, { redirect: "manual" }); +} diff --git a/social/functions/projects/index.js b/social/functions/projects/index.js new file mode 100644 index 0000000..6ff911a --- /dev/null +++ b/social/functions/projects/index.js @@ -0,0 +1,2 @@ +// /projects and /projects/ themselves +export { onRequest } from "./[[path]].js"; diff --git a/social/lib.js b/social/lib.js index 3022e7b..e312360 100644 --- a/social/lib.js +++ b/social/lib.js @@ -3,9 +3,9 @@ export const SITE = "https://danieldaum.net"; export const REPLY_TO = "reply@replies.danieldaum.net"; -// /about/, /now/, /now//, /blog//, /projects// +// /about/, /now/, /now//, /blog// (/projects/ is showcase, see functions/projects/) export function isLikeable(path) { - return /^\/(about|now)\/$|^\/(blog|now|projects)\/[a-z0-9-]+\/$/.test(path); + return /^\/(about|now)\/$|^\/(blog|now)\/[a-z0-9-]+\/$/.test(path); } // ===== hmac ===== diff --git a/theme.js b/theme.js index a4832d7..3ad787a 100644 --- a/theme.js +++ b/theme.js @@ -1,6 +1,11 @@ // one palette per visit: it holds while the tab is open, and the next visit gets the // next one. without js the css stays moss and the theme button stays hidden. +// a `theme` cookie on danieldaum.net carries the palette to code.danieldaum.net +// and back, so both sites show the same one. const themes = ["moss", "ink", "wine", "plum", "teal"]; // "flexoki" +const shared = location.hostname.endsWith("danieldaum.net") + ? "; domain=danieldaum.net; secure" + : ""; function after(theme) { return themes[(themes.indexOf(theme) + 1) % themes.length]; @@ -8,6 +13,7 @@ function after(theme) { function use(theme) { document.documentElement.dataset.theme = theme; + document.cookie = `theme=${theme}; path=/; samesite=lax${shared}`; try { sessionStorage.setItem("theme", theme); localStorage.setItem("theme", theme); @@ -16,10 +22,13 @@ function use(theme) { let current = "moss"; try { + const cookie = /(?:^|;\s*)theme=([a-z]+)/.exec(document.cookie)?.[1]; const visit = sessionStorage.getItem("theme"); - current = themes.includes(visit) - ? visit - : after(localStorage.getItem("theme")); + current = themes.includes(cookie) + ? cookie + : themes.includes(visit) + ? visit + : after(localStorage.getItem("theme")); } catch {} use(current);