commit fb81da5ce4551ffa4e441069823b7cd5878b6929
parent 0cc3214c84892c361adb4cb1ecedce772bd19f73
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date: Thu, 15 Oct 2026 09:19:47 -0400
editor: review fixes — a site choice that navigates refreshes once the new URL has committed (the route leaves the client cache alone; the push stays a push); the active-site writes go one at a time (the last pick wins); /api/active-site's origin check reads X-Forwarded-Host first; a tab shows another tab's pick at once; a write's refresh is asked again every 2 s (five times at most) until the layout comes back with the value (a refresh just after a load was now and then never applied). site-scope.spec: the picks wait for hydration, a re-scope gets 15 s, and a new case — a choice on a site's page reaches a page cached under the old site
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Diffstat:
4 files changed, 141 insertions(+), 33 deletions(-)
diff --git a/editor/app/api/active-site/route.ts b/editor/app/api/active-site/route.ts
@@ -25,7 +25,9 @@ export const dynamic = "force-dynamic";
// decided at every read, so a site deleted later resolves as no selection.
export async function POST(request: Request): Promise<Response> {
const origin = request.headers.get("origin");
- const host = request.headers.get("host");
+ // The host the browser addressed: a proxy's X-Forwarded-Host first, as
+ // Next's own server-action origin check reads it.
+ const host = request.headers.get("x-forwarded-host")?.split(",")[0]?.trim() || request.headers.get("host");
if (origin !== null && (() => {
try {
return new URL(origin).host !== host;
diff --git a/editor/app/components/SiteScopeProvider.tsx b/editor/app/components/SiteScopeProvider.tsx
@@ -113,17 +113,66 @@ 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]);
+ // 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.
+ const writes = useRef<Promise<unknown>>(Promise.resolve());
+ const write = useCallback((value: string): Promise<boolean> => {
+ const next = writes.current.then(() => writeActiveSite(value));
+ writes.current = next;
+ return next;
+ }, []);
const channel = useRef<BroadcastChannel | null>(null);
useEffect(() => {
if (typeof BroadcastChannel === "undefined") return;
const ch = new BroadcastChannel(ACTIVE_SITE_CHANNEL);
channel.current = ch;
- ch.onmessage = () => router.refresh();
+ ch.onmessage = (e: MessageEvent) => {
+ // The value the other tab wrote is shown at once (review fix, release
+ // 29): the refresh brings the layout's own read of the cookie after it,
+ // and a refresh that lands late (a busy tab) no longer leaves the picker
+ // on the old site meanwhile.
+ if (isStorableActiveSite(e.data) && storedRef.current !== e.data) {
+ storedRef.current = e.data;
+ setStored(e.data);
+ }
+ refreshUntil(e.data);
+ };
return () => {
ch.close();
if (channel.current === ch) channel.current = null;
};
- }, [router]);
+ }, [router, refreshUntil]);
const announce = useCallback((value: string) => {
try {
channel.current?.postMessage(value);
@@ -137,30 +186,30 @@ export function SiteScopeProvider({
storedRef.current = value;
setStored(value);
setWriting((n) => n + 1);
- const ok = await writeActiveSite(value);
+ const ok = await write(value);
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) router.refresh();
+ if (!o.navigating) refreshUntil(value);
} else {
storedRef.current = previous;
setStored(previous);
}
return ok;
- }, [announce, router]);
+ }, [announce, refreshUntil, 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 writeActiveSite(value);
+ const ok = await write(value);
if (ok) {
announce(value);
- router.refresh();
+ refreshUntil(value);
}
return ok;
- }, [announce, router]);
+ }, [announce, refreshUntil, 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
@@ -1,6 +1,6 @@
"use client";
-import { useEffect, useState } from "react";
+import { useEffect, useRef, useState } from "react";
import { usePathname, useRouter, useSearchParams } from "next/navigation";
import {
ALL_SITES,
@@ -42,6 +42,18 @@ export function SiteScopeSelect({ sites }: { sites: SiteScopeOption[] }) {
null,
);
if (pending !== null && pending.urlKey !== urlKey) setPending(null);
+ // A choice that navigates (a site's page, a `?site=` link's) refreshes once
+ // the new URL has COMMITTED (review fix, release 29): the cookie is written
+ // by a route, which — unlike a server action — leaves the client router's
+ // cache as it was, so a page visited within its stale time would otherwise
+ // show the scope it was rendered under. After the commit, so the push stays
+ // a push (the history fix the route was made for).
+ const refreshFrom = useRef<string | null>(null);
+ useEffect(() => {
+ if (refreshFrom.current === null || refreshFrom.current === urlKey) return;
+ refreshFrom.current = null;
+ router.refresh();
+ }, [urlKey, router]);
const held = pending?.urlKey === urlKey ? pending.value : null;
const resolved = resolveActiveSiteFrom(
[held, onSite?.siteId, param, stored],
@@ -59,6 +71,7 @@ export function SiteScopeSelect({ sites }: { sites: SiteScopeOption[] }) {
setPending(null);
return;
}
+ refreshFrom.current = urlKey;
router.push(
value === ALL_SITES
? "/sites"
@@ -80,6 +93,7 @@ export function SiteScopeSelect({ sites }: { sites: SiteScopeOption[] }) {
setPending(null);
return;
}
+ refreshFrom.current = urlKey;
router.replace(withoutSiteParam(pathname, search), { scroll: false });
}
diff --git a/editor/e2e/site-scope.spec.ts b/editor/e2e/site-scope.spec.ts
@@ -6,6 +6,11 @@ import { readJson, resetData, writeSettings, writeSite, testPaths } from "./help
type SiteFile = { channels: { slug: string }[] };
// Two sites over the shared two-channel pool: alpha exposes slow-a, beta slow-b.
+// A pick re-scopes the page by a router refresh — a server render of the
+// whole page — which on a loaded machine (other worktrees' builds and suites
+// beside this one) can take longer than the default 5 s (release 29 D1).
+const RESCOPED = { timeout: 15_000 };
+
async function twoSites() {
await resetData("two-slow-channels");
await writeSite("alpha", {
@@ -22,8 +27,8 @@ test("defaults to all sites and shows the full pool", async ({ page }) => {
await twoSites();
await page.goto("/channels");
await expect(page.getByLabel("Active site")).toHaveValue("__all__");
- await expect(page.getByRole("link", { name: "slow-a" })).toBeVisible();
- await expect(page.getByRole("link", { name: "slow-b" })).toBeVisible();
+ await expect(page.getByRole("link", { name: "slow-a" })).toBeVisible(RESCOPED);
+ await expect(page.getByRole("link", { name: "slow-b" })).toBeVisible(RESCOPED);
});
test("selecting a site scopes the channels list and persists", async ({
@@ -32,25 +37,26 @@ test("selecting a site scopes the channels list and persists", async ({
await twoSites();
await page.goto("/channels");
+ await hydrated(page); // a pick before React attaches is lost (release 29 D1)
await page.getByLabel("Active site").selectOption("alpha");
// The choice is a cookie (release 15 SS): the URL is left alone, where it
// used to gain ?site=alpha.
await expect(page).toHaveURL(/\/channels$/);
- await expect(page.getByRole("link", { name: "slow-a" })).toBeVisible();
- await expect(page.getByRole("link", { name: "slow-b" })).toHaveCount(0);
+ await expect(page.getByRole("link", { name: "slow-a" })).toBeVisible(RESCOPED);
+ await expect(page.getByRole("link", { name: "slow-b" })).toHaveCount(0, RESCOPED);
await expectNoSiteParam(page);
// The cookie persists the choice: revisiting with no param re-applies it.
await page.goto("/channels");
await expect(page.getByLabel("Active site")).toHaveValue("alpha");
await expect(page).toHaveURL(/\/channels$/);
- await expect(page.getByRole("link", { name: "slow-b" })).toHaveCount(0);
+ await expect(page.getByRole("link", { name: "slow-b" })).toHaveCount(0, RESCOPED);
await expectNoSiteParam(page);
// Switching to beta flips the scope.
await page.getByLabel("Active site").selectOption("beta");
- await expect(page.getByRole("link", { name: "slow-b" })).toBeVisible();
- await expect(page.getByRole("link", { name: "slow-a" })).toHaveCount(0);
+ await expect(page.getByRole("link", { name: "slow-b" })).toBeVisible(RESCOPED);
+ await expect(page.getByRole("link", { name: "slow-a" })).toHaveCount(0, RESCOPED);
});
test("dashboard stats and table scope to the active site", async ({ page }) => {
@@ -60,8 +66,8 @@ test("dashboard stats and table scope to the active site", async ({ page }) => {
// (c63f5cb), which has no "channels" group; the scoped count is now observable
// as the number of rows in its channels table.
await expect(page.getByLabel(/^channel row /)).toHaveCount(1);
- await expect(page.getByRole("link", { name: "slow-a" })).toBeVisible();
- await expect(page.getByRole("link", { name: "slow-b" })).toHaveCount(0);
+ await expect(page.getByRole("link", { name: "slow-a" })).toBeVisible(RESCOPED);
+ await expect(page.getByRole("link", { name: "slow-b" })).toHaveCount(0, RESCOPED);
});
test("charts is a site's tab, and the picker follows the path", async ({
@@ -108,6 +114,7 @@ test("publish targets the site in the path, and says when it cannot deploy", asy
).toBeVisible();
await expect(page.getByText(/select a specific site/i)).toHaveCount(0);
await expect(page.getByLabel("Active site")).toHaveValue("alpha");
+ await hydrated(page); // a pick before React attaches is lost (release 29 D1)
await page.getByLabel("Active site").selectOption("beta");
await expect(page).toHaveURL(/\/sites\/beta\/publish$/);
});
@@ -256,8 +263,9 @@ test("a stored site is the picker's first paint on every page, never All sites",
// Store alpha the way a person does, and wait for it to land: the re-render
// that drops slow-b is the one the cookie write triggers.
await page.goto("/channels");
+ await hydrated(page); // a pick before React attaches is lost (release 29 D1)
await picker.selectOption("alpha");
- await expect(page.getByRole("link", { name: "slow-b" })).toHaveCount(0);
+ await expect(page.getByRole("link", { name: "slow-b" })).toHaveCount(0, RESCOPED);
expect(await storedCookie(page)).toBe("alpha");
// Full loads: the server renders the stored site, and nothing moves it.
@@ -308,18 +316,19 @@ test("a ?site= link scopes its own page and is not stored; a choice there drops
await twoSites();
const picker = page.getByLabel("Active site");
await page.goto("/channels");
+ await hydrated(page); // a pick before React attaches is lost (release 29 D1)
await picker.selectOption("alpha");
- await expect(page.getByRole("link", { name: "slow-b" })).toHaveCount(0);
+ await expect(page.getByRole("link", { name: "slow-b" })).toHaveCount(0, RESCOPED);
// The link's scope, on the first paint, for this page only.
expect(await firstPaint(page, "/channels?site=beta")).toBe("beta");
- await expect(page.getByRole("link", { name: "slow-b" })).toBeVisible();
- await expect(page.getByRole("link", { name: "slow-a" })).toHaveCount(0);
+ await expect(page.getByRole("link", { name: "slow-b" })).toBeVisible(RESCOPED);
+ await expect(page.getByRole("link", { name: "slow-a" })).toHaveCount(0, RESCOPED);
expect(await storedCookie(page)).toBe("alpha");
// Not written: the next plain visit is the stored site's.
expect(await firstPaint(page, "/channels")).toBe("alpha");
- await expect(page.getByRole("link", { name: "slow-b" })).toHaveCount(0);
+ await expect(page.getByRole("link", { name: "slow-b" })).toHaveCount(0, RESCOPED);
// Choosing on a link's page replaces the link's scope: stored, and the param
// goes, so the page and the picker agree.
@@ -328,8 +337,8 @@ test("a ?site= link scopes its own page and is not stored; a choice there drops
await picker.selectOption("__all__");
await expect(page).toHaveURL(/\/channels$/);
await expect(picker).toHaveValue("__all__");
- await expect(page.getByRole("link", { name: "slow-a" })).toBeVisible();
- await expect(page.getByRole("link", { name: "slow-b" })).toBeVisible();
+ await expect(page.getByRole("link", { name: "slow-a" })).toBeVisible(RESCOPED);
+ await expect(page.getByRole("link", { name: "slow-b" })).toBeVisible(RESCOPED);
expect(await storedCookie(page)).toBe("__all__");
expect(await firstPaint(page, "/channels")).toBe("__all__");
});
@@ -349,7 +358,7 @@ test("a choice the old picker kept in localStorage moves to the cookie once", as
// The one visit that may flash: the server had no cookie to render from.
await page.goto("/channels");
await expect(picker).toHaveValue("beta");
- await expect(page.getByRole("link", { name: "slow-a" })).toHaveCount(0);
+ await expect(page.getByRole("link", { name: "slow-a" })).toHaveCount(0, RESCOPED);
await expect.poll(() => storedCookie(page), { timeout: 15_000 }).toBe("beta");
await expect.poll(legacy, { timeout: 15_000 }).toBeNull();
@@ -379,6 +388,36 @@ test("Back to a site's page shows that site, not the choice made there", async (
await expect(picker).toHaveValue("alpha");
});
+// A choice that navigates refreshes the client cache once the new page is in
+// (review fix, release 29): the cookie is written by a route, which leaves
+// the router's cache alone, so Channels — visited, and so cached, under alpha
+// a moment before — would otherwise come back scoped to alpha.
+test("a choice made on a site's page reaches a page cached under the old site", async ({
+ page,
+}) => {
+ await twoSites();
+ const picker = page.getByLabel("Active site");
+ await page.goto("/channels");
+ await hydrated(page);
+ await picker.selectOption("alpha");
+ await expect(page.getByRole("link", { name: "slow-b" })).toHaveCount(0, RESCOPED);
+ await expect(page.getByRole("link", { name: "slow-a" })).toBeVisible(RESCOPED);
+ // A client-side visit to alpha's page, then the choice there.
+ await page.locator("aside nav").getByRole("link", { name: "Sites", exact: true }).click();
+ await expect(page).toHaveURL(/\/sites$/);
+ await page.locator('main a[href="/sites/alpha"]').click();
+ await expect(page).toHaveURL(/\/sites\/alpha$/);
+ await expect(picker).toHaveAttribute("data-hydrated", "true");
+ await picker.selectOption("beta");
+ await expect(page).toHaveURL(/\/sites\/beta$/);
+ // Back to Channels by the sidebar, well inside its stale time.
+ await page.locator("aside nav").getByRole("link", { name: "Channels", exact: true }).click();
+ await expect(page).toHaveURL(/\/channels$/);
+ await expect(picker).toHaveValue("beta");
+ await expect(page.getByRole("link", { name: "slow-b" })).toBeVisible(RESCOPED);
+ await expect(page.getByRole("link", { name: "slow-a" })).toHaveCount(0, RESCOPED);
+});
+
// A site picked in one tab reaches the others. The cookie is shared, but a
// tab's picker reads its root layout, which a client-side navigation does not
// re-render; without the broadcast, tab A kept showing alpha over beta's pages
@@ -391,15 +430,17 @@ test("a site picked in another tab reaches this one", async ({ context }) => {
const a = await context.newPage();
const pickerA = a.getByLabel("Active site");
await a.goto("/channels");
+ await hydrated(a); // a pick before React attaches is lost (release 29 D1)
await pickerA.selectOption("alpha");
- await expect(a.getByRole("link", { name: "slow-b" })).toHaveCount(0);
+ await expect(a.getByRole("link", { name: "slow-b" })).toHaveCount(0, RESCOPED);
await a.goto("/settings");
await hydrated(a);
const b = await context.newPage();
await b.goto("/channels");
+ await hydrated(b); // a pick before React attaches is lost (release 29 D1)
await b.getByLabel("Active site").selectOption("beta");
- await expect(b.getByRole("link", { name: "slow-a" })).toHaveCount(0);
+ await expect(b.getByRole("link", { name: "slow-a" })).toHaveCount(0, RESCOPED);
expect(await storedCookie(b)).toBe("beta");
// Tab A follows without a reload, and its next page agrees with its picker.
@@ -407,8 +448,8 @@ test("a site picked in another tab reaches this one", async ({ context }) => {
await a.locator("aside nav").getByRole("link", { name: "Channels", exact: true }).click();
await expect(a).toHaveURL(/\/channels$/);
await expect(pickerA).toHaveValue("beta");
- await expect(a.getByRole("link", { name: "slow-b" })).toBeVisible();
- await expect(a.getByRole("link", { name: "slow-a" })).toHaveCount(0);
+ await expect(a.getByRole("link", { name: "slow-b" })).toBeVisible(RESCOPED);
+ await expect(a.getByRole("link", { name: "slow-a" })).toHaveCount(0, RESCOPED);
// And a client-side visit to alpha's page is recorded again: tab A no longer
// believes alpha is stored.
@@ -433,8 +474,9 @@ test("a new channel starts checked on the stored site, or on a ?site= link's", a
await writeSettings({ autoRefreshIntervalSeconds: 0 });
const a = await context.newPage();
await a.goto("/channels");
+ await hydrated(a); // a pick before React attaches is lost (release 29 D1)
await a.getByLabel("Active site").selectOption("alpha");
- await expect(a.getByRole("link", { name: "slow-b" })).toHaveCount(0);
+ await expect(a.getByRole("link", { name: "slow-b" })).toHaveCount(0, RESCOPED);
const alpha = a.getByLabel("Include on Alpha");
const beta = a.getByLabel("Include on Beta");
@@ -461,8 +503,9 @@ test("a new channel starts checked on the stored site, or on a ?site= link's", a
await expect(a.getByLabel("Group for Alpha")).toHaveCount(0);
const b = await context.newPage();
await b.goto("/channels");
+ await hydrated(b); // a pick before React attaches is lost (release 29 D1)
await b.getByLabel("Active site").selectOption("beta");
- await expect(b.getByRole("link", { name: "slow-a" })).toHaveCount(0);
+ await expect(b.getByRole("link", { name: "slow-a" })).toHaveCount(0, RESCOPED);
await expect(a.getByLabel("Active site")).toHaveValue("beta");
await a.waitForTimeout(SETTLE_MS);
expect(await alpha.isChecked()).toBe(false);