Archilyzer · Source

archilyzer

Archilyzer
git clone https://archilyzer.pages.dev/source/archilyzer.git
Log | Files | Refs | README | LICENSE

commit 2fd6762573eea7516966391f6d087b85a8851d98
parent fb81da5ce4551ffa4e441069823b7cd5878b6929
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date:   Thu, 15 Oct 2026 09:38:17 -0400

editor: the active-site cookie has two writers, by what follows the write — the server action again where the page stays (a pick on Dashboard or Channels, a site page's visit, the migration: its answer re-renders the page; a route write and a router.refresh after it was, now and then under load, never applied), the route before a navigation (the picker on a site's page or a ?site= link's: the push/replace fix); both chained, the last pick last. The retried refresh is gone; another tab's pick still shows at once

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

Diffstat:
Meditor/app/api/active-site/route.ts | 23+++++++++++------------
Meditor/app/components/SiteScopeProvider.tsx | 91+++++++++++++++++++++++++++++++-------------------------------------------------
Meditor/app/components/SiteScopeSelect.tsx | 6+++---
Meditor/app/lib/activeSite.ts | 5+++--
Aeditor/app/lib/activeSiteActions.ts | 34++++++++++++++++++++++++++++++++++
Meditor/app/lib/activeSiteServer.ts | 2+-
6 files changed, 87 insertions(+), 74 deletions(-)

diff --git a/editor/app/api/active-site/route.ts b/editor/app/api/active-site/route.ts @@ -4,20 +4,19 @@ import { activeSiteCookieForRequest } from "../../lib/activeSiteServer"; export const dynamic = "force-dynamic"; -// THE ONE WRITER of the active-site cookie (see app/lib/activeSite.ts): POST -// `{ value }` from SiteScopeProvider — the picker's choice, a site's own page -// recording itself, the localStorage migration — answers `{ ok }`. +// THE COOKIE'S WRITER BEFORE A NAVIGATION (see app/lib/activeSite.ts): POST +// `{ value }` from SiteScopeProvider when the picker moves the URL after the +// choice (a site's own page, a `?site=` link's) answers `{ ok }`. A page that +// stays writes through setActiveSiteAction, whose answer re-renders it. // -// A ROUTE, NOT A SERVER ACTION (release 29 D1). A cookie set in a server action +// NOT A SERVER ACTION HERE (release 29 D1). A cookie set in a server action // makes Next re-render the page in the action's answer and apply it through -// the router — answered BEFORE that applies, so a navigation the caller made -// next (the picker on a site's page pushes the other site's) discarded the -// pending action and replayed it as a refresh after the push, and when React -// committed the two together the push's history entry was written as a -// replace (Back skipped the page the choice was made on); and under load the -// re-render waited on in-flight prefetches, so a re-scope came seconds late -// (site-scope.spec). Here the write is only a write: the provider refreshes, -// or the picker navigates, itself. +// the router — answered BEFORE that applies, so the navigation the picker +// made next discarded the pending action and replayed it as a refresh after +// the push, and when React committed the two together the push's history +// entry was written as a replace (Back skipped the page the choice was made +// on: site-scope.spec). The route only writes; the picker refreshes once the +// new URL has committed (the client cache is not cleared by a route). // // Same-origin only (a cross-site page could otherwise flip an operator's // scope): JSON, and an Origin, when sent, that is this host. The value is only diff --git a/editor/app/components/SiteScopeProvider.tsx b/editor/app/components/SiteScopeProvider.tsx @@ -18,7 +18,10 @@ import { siteIdFromPathname, } from "../lib/activeSite"; -// The cookie's one writer (app/api/active-site/route.ts): true when it is set. +import { setActiveSiteAction } from "../lib/activeSiteActions"; + +// The cookie's writer before a navigation (app/api/active-site/route.ts; see +// `write` below for the other): true when it is set. async function writeActiveSite(value: string): Promise<boolean> { try { const res = await fetch("/api/active-site", { @@ -60,8 +63,8 @@ async function writeActiveSite(value: string): Promise<boolean> { // cookie gets it copied into the cookie, then the key is removed. That is the // only localStorage read, and the one paint it may cost is on the first visit // after the update. -// Both only WRITE the cookie (`record`), then refresh: the value comes back -// through the server's re-render. They run as the page hydrates, and a state change there +// Both only WRITE the cookie (`record`, the server action): the value comes +// back through the server's re-render. They run as the page hydrates, and a state change there // would re-render the picker before React replays a choice made on the // server-rendered select before hydration: the re-render resets the select to // its old value, and the replayed change reads that instead of the choice. @@ -70,11 +73,11 @@ type SiteScope = { // The stored selection: a site id or "__all__". The picker resolves it // against the path and a `?site=` param; it is not the effective scope alone. stored: string; - // Store a choice: shown at once, written through /api/active-site. Resolves - // once the cookie is set to true — the page then refreshes to re-render with - // it, unless `navigating` (the caller moves the URL itself, and the next page - // renders with the cookie) — or to false with the previous selection put - // back when the write failed. + // Store a choice: shown at once, written (see `write`). Resolves once the + // cookie is set to true — the page re-rendered with it, unless `navigating` + // (the caller moves the URL itself, and refreshes once it has: the route's + // write leaves the client cache alone) — or to false with the previous + // selection put back when the write failed. choose: (value: string, o?: { navigating?: boolean }) => Promise<boolean>; }; @@ -113,42 +116,25 @@ export function SiteScopeProvider({ // The other tabs (see above): subscribe, and keep the one instance to post on. const router = useRouter(); - // A REFRESH UNTIL THE LAYOUT SAYS SO (release 29 D1). A write is followed by - // a router refresh, the re-render that carries the new scope; under load a - // refresh dispatched just after a page loaded was, now and then, never - // applied (site-scope.spec: the list stayed on the old site for 15 s). So - // the value the layout should come back with is remembered, and the refresh - // is asked again every 2 s (five times at most) until `activeSite` is it. - const awaiting = useRef<{ value: string; tries: number } | null>(null); - const activeRef = useRef(activeSite); - activeRef.current = activeSite; - const refreshUntil = useCallback( - (value: string) => { - awaiting.current = activeRef.current === value ? null : { value, tries: 0 }; - router.refresh(); - }, - [router], - ); - useEffect(() => { - const t = setInterval(() => { - const w = awaiting.current; - if (w === null) return; - if (activeRef.current === w.value || w.tries >= 5) { - awaiting.current = null; - return; - } - w.tries += 1; - router.refresh(); - }, 2000); - return () => clearInterval(t); - }, [router]); + // TWO WRITERS OF ONE COOKIE, by what follows the write (release 29 D1): + // - a write the page stays on (a pick on Dashboard or Channels, a site + // page's visit, the migration) is the SERVER ACTION, whose answer carries + // the page re-rendered with the new cookie — the re-scope, with nothing of + // our own (a route's write and a router.refresh after it was, now and + // then under load, never applied: site-scope.spec); + // - a write the caller navigates after (a pick on a site's page or a + // `?site=` link's) is the ROUTE, POST /api/active-site: a navigation made + // while a server action's re-render is still pending discards it and + // replays it as a refresh after the navigation, committing the push as a + // replace (Back skipped the page); the route's answer carries no render. // THE WRITES GO ONE AT A TIME, in the order they were asked for (review fix, - // release 29): two fetches race, and an earlier pick's Set-Cookie landing - // last would leave the cookie, and the next refresh, on it. Chained, the - // last pick is the last write. + // release 29): an earlier pick's write landing last would leave the cookie + // on it. Chained, the last pick is the last write. const writes = useRef<Promise<unknown>>(Promise.resolve()); - const write = useCallback((value: string): Promise<boolean> => { - const next = writes.current.then(() => writeActiveSite(value)); + const write = useCallback((value: string, navigating: boolean): Promise<boolean> => { + const next = writes.current.then(() => + navigating ? writeActiveSite(value) : setActiveSiteAction(value).catch(() => false), + ); writes.current = next; return next; }, []); @@ -166,13 +152,13 @@ export function SiteScopeProvider({ storedRef.current = e.data; setStored(e.data); } - refreshUntil(e.data); + router.refresh(); }; return () => { ch.close(); if (channel.current === ch) channel.current = null; }; - }, [router, refreshUntil]); + }, [router]); const announce = useCallback((value: string) => { try { channel.current?.postMessage(value); @@ -186,30 +172,23 @@ export function SiteScopeProvider({ storedRef.current = value; setStored(value); setWriting((n) => n + 1); - const ok = await write(value); + const ok = await write(value, o.navigating === true); setWriting((n) => n - 1); if (ok) { announce(value); - // The route only writes (no server action's implicit re-render): the - // page and the layout re-render with the cookie now — unless the caller - // navigates, whose next page does. - if (!o.navigating) refreshUntil(value); } else { storedRef.current = previous; setStored(previous); } return ok; - }, [announce, refreshUntil, write]); + }, [announce, write]); // Write a value the page did not choose — no optimistic state (see above). const record = useCallback(async (value: string): Promise<boolean> => { - const ok = await write(value); - if (ok) { - announce(value); - refreshUntil(value); - } + const ok = await write(value, false); + if (ok) announce(value); return ok; - }, [announce, refreshUntil, write]); + }, [announce, write]); // Visiting a site's own page records it: once per arrival at that site's // pages (a re-run of the effect — StrictMode, Fast Refresh — does not write diff --git a/editor/app/components/SiteScopeSelect.tsx b/editor/app/components/SiteScopeSelect.tsx @@ -80,9 +80,9 @@ export function SiteScopeSelect({ sites }: { sites: SiteScopeOption[] }) { return; } if (param === null) { - // The common case: the stored value is what shows, and the provider - // refreshes this page (Dashboard, Channels) in the new scope once the - // cookie is written. + // The common case: the stored value is what shows, and the cookie write + // (the server action) re-renders this page (Dashboard, Channels) in the + // new scope. await choose(value); return; } diff --git a/editor/app/lib/activeSite.ts b/editor/app/lib/activeSite.ts @@ -7,8 +7,9 @@ // `SiteScopeProvider`; Dashboard and Channels read it through the same helper. // So the server renders the picker, and the scoped pages, with the stored // selection on the first paint — nothing is reconciled after it. It is written -// only by `POST /api/active-site` (app/api/active-site/route.ts; a server action -// until release 29), which SiteScopeProvider calls. +// by `setActiveSiteAction` (app/lib/activeSiteActions.ts) where the page stays, +// and by `POST /api/active-site` (app/api/active-site/route.ts) before a +// navigation (release 29) — SiteScopeProvider chooses. // // A `?site=<id>` LINK still works: a valid param governs that one request (the // server pages read it from `searchParams`, the picker from the URL), and is not diff --git a/editor/app/lib/activeSiteActions.ts b/editor/app/lib/activeSiteActions.ts @@ -0,0 +1,34 @@ +"use server"; + +import { cookies } from "next/headers"; +import { + ACTIVE_SITE_COOKIE_MAX_AGE, + isStorableActiveSite, +} from "./activeSite"; +import { activeSiteCookieForRequest } from "./activeSiteServer"; + +// ONE OF THE TWO WRITERS of the active-site cookie (see ./activeSite.ts): the +// one a page that STAYS calls — the picker on Dashboard or Channels, +// SiteScopeProvider when a site's own page is visited, and once its +// localStorage migration. Setting a cookie in a server action makes Next +// re-render the current page and its layouts with the new value in the +// action's answer, so the root layout hands the provider the new selection and +// Dashboard or Channels re-scope, with no refresh of our own. A write followed +// by a navigation goes through POST /api/active-site instead (release 29 D1: +// SiteScopeProvider's `write` says why). +// +// The value is only SHAPE-checked (a site id or "__all__"): whether it names a +// configured site is decided at every read, so a site deleted later resolves as +// no selection. Returns false, writing nothing, for anything else. +export async function setActiveSiteAction(value: string): Promise<boolean> { + if (!isStorableActiveSite(value)) return false; + const name = await activeSiteCookieForRequest(); + (await cookies()).set(name, value, { + path: "/", + sameSite: "lax", + maxAge: ACTIVE_SITE_COOKIE_MAX_AGE, + // Only the server reads it: the page gets it through the layout. + httpOnly: true, + }); + return true; +} diff --git a/editor/app/lib/activeSiteServer.ts b/editor/app/lib/activeSiteServer.ts @@ -13,7 +13,7 @@ import { // result to SiteScopeProvider; Dashboard and Channels call it with their // `searchParams.site`, which wins for that request when it names a site or // "__all__". Nothing is written here: a server component cannot set a cookie, -// and only POST /api/active-site does. +// and only setActiveSiteAction and POST /api/active-site do. export type ActiveSiteRead = ResolvedActiveSite & { // The cookie's raw value, or null when the request carried none — which is // what tells the provider a visitor may still have the old localStorage key.