showcase
- v0.2.4
- 37 commits
- 1 branch
- 6 tags
- updated
- CI 2/2
saturn.patch
5.8 KB · 134 lines
1diff --git a/_routes.json b/_routes.json2index f37e407..708a79a 1006443--- a/_routes.json4+++ b/_routes.json5@@ -1,5 +1,5 @@6 {7 "version": 1,8- "include": ["/about/*", "/now/*", "/blog/*", "/projects/*", "/api/*"],9+ "include": ["/about/*", "/now/*", "/blog/*", "/projects", "/projects/*", "/api/*"],10 "exclude": []11 }12diff --git a/social/README.md b/social/README.md13index 28f5a85..730f46a 10064414--- a/social/README.md15+++ b/social/README.md16@@ -10,6 +10,7 @@ likes and comments. everything for them lives in this folder, and none of it is17 | `functions/_middleware.js` | runs before a page is served and fills its `data-social` section from d1 |18 | `functions/api/like.js` | the like button posts here |19 | `functions/api/moderate.js` | the approve/delete page linked from new-comment emails |20+| `functions/projects/` | not social: `/projects/` is showcase at code.danieldaum.net, redirected or proxied |21 | `email.js` | email worker: turns mail sent to `reply@replies.danieldaum.net` into comments |22 | `schema.sql` | the d1 tables: `Likes`, `LikeVoters`, `Comments` |23 | `wrangler.toml` | pages config (d1 binding) |24@@ -44,13 +45,13 @@ put this at the end of the post, inside `<article>` if it has one. the template25 </section>26 ```27 28-this works on `/about/`, `/now/<slug>/`, `/blog/<slug>/` and `/projects/<slug>/`. take the section out and the page has none.29+this works on `/about/`, `/now/<slug>/` and `/blog/<slug>/`. `/projects/` is showcase (`functions/projects/`), so it has none. take the section out and the page has none.30 31 `/now/` is a copy of the newest edition, so it points at that edition: `data-social="/now/<slug>/"`. update it with each new now post. comments always go on the edition, never on `/now/`.32 33 ### a new kind of page (e.g. `/books/<slug>/`)34 35-1. add it to `isLikeable()` in `lib.js`: `(blog|now|projects)` → `(blog|now|projects|books)`36+1. add it to `isLikeable()` in `lib.js`: `(blog|now)` → `(blog|now|books)`37 2. add `"/books/*"` to `/_routes.json`. every path in there runs a function and isn't cached, so keep it narrow38 3. add the section to the pages39 4. `mise run social-email`: the worker uses `isLikeable()` too, so it rejects comments for the new pages until it's redeployed40diff --git a/social/functions/projects/[[path]].js b/social/functions/projects/[[path]].js41new file mode 10064442index 0000000..5db842e43--- /dev/null44+++ b/social/functions/projects/[[path]].js45@@ -0,0 +1,24 @@46+// /projects/ is showcase, the code site at code.danieldaum.net, not files in47+// this repo (the old pages under projects/ are never served).48+//49+// MODE "redirect": send visitors to code.danieldaum.net.50+// MODE "proxy": serve showcase here at /projects/. Run showcase with51+// "base_path": "/projects" first, so its links stay under /projects/.52+// Clone, go get and docker pull stay on code.danieldaum.net.53+const ORIGIN = "https://code.danieldaum.net";54+const MODE = "redirect";55+56+export async function onRequest({ request }) {57+ const url = new URL(request.url);58+ if (MODE === "redirect") {59+ const rest = url.pathname.replace(/^\/projects/, "") || "/";60+ return Response.redirect(ORIGIN + rest + url.search, 302);61+ }62+ if (request.method !== "GET" && request.method !== "HEAD") {63+ return new Response("not found", { status: 404 });64+ }65+ // the origin is fixed: only showcase's own pages can come back66+ const upstream = new Request(ORIGIN + url.pathname + url.search, request);67+ upstream.headers.delete("Cookie");68+ return fetch(upstream, { redirect: "manual" });69+}70diff --git a/social/functions/projects/index.js b/social/functions/projects/index.js71new file mode 10064472index 0000000..6ff911a73--- /dev/null74+++ b/social/functions/projects/index.js75@@ -0,0 +1,2 @@76+// /projects and /projects/ themselves77+export { onRequest } from "./[[path]].js";78diff --git a/social/lib.js b/social/lib.js79index 3022e7b..e312360 10064480--- a/social/lib.js81+++ b/social/lib.js82@@ -3,9 +3,9 @@83 export const SITE = "https://danieldaum.net";84 export const REPLY_TO = "reply@replies.danieldaum.net";85 86-// /about/, /now/, /now/<slug>/, /blog/<slug>/, /projects/<slug>/87+// /about/, /now/, /now/<slug>/, /blog/<slug>/ (/projects/ is showcase, see functions/projects/)88 export function isLikeable(path) {89- return /^\/(about|now)\/$|^\/(blog|now|projects)\/[a-z0-9-]+\/$/.test(path);90+ return /^\/(about|now)\/$|^\/(blog|now)\/[a-z0-9-]+\/$/.test(path);91 }92 93 // ===== hmac =====94diff --git a/theme.js b/theme.js95index a4832d7..3ad787a 10064496--- a/theme.js97+++ b/theme.js98@@ -1,6 +1,11 @@99 // one palette per visit: it holds while the tab is open, and the next visit gets the100 // next one. without js the css stays moss and the theme button stays hidden.101+// a `theme` cookie on danieldaum.net carries the palette to code.danieldaum.net102+// and back, so both sites show the same one.103 const themes = ["moss", "ink", "wine", "plum", "teal"]; // "flexoki"104+const shared = location.hostname.endsWith("danieldaum.net")105+ ? "; domain=danieldaum.net; secure"106+ : "";107 108 function after(theme) {109 return themes[(themes.indexOf(theme) + 1) % themes.length];110@@ -8,6 +13,7 @@ function after(theme) {111 112 function use(theme) {113 document.documentElement.dataset.theme = theme;114+ document.cookie = `theme=${theme}; path=/; samesite=lax${shared}`;115 try {116 sessionStorage.setItem("theme", theme);117 localStorage.setItem("theme", theme);118@@ -16,10 +22,13 @@ function use(theme) {119 120 let current = "moss";121 try {122+ const cookie = /(?:^|;\s*)theme=([a-z]+)/.exec(document.cookie)?.[1];123 const visit = sessionStorage.getItem("theme");124- current = themes.includes(visit)125- ? visit126- : after(localStorage.getItem("theme"));127+ current = themes.includes(cookie)128+ ? cookie129+ : themes.includes(visit)130+ ? visit131+ : after(localStorage.getItem("theme"));132 } catch {}133 use(current);134