A lightweight design agent built with libfx. Describe a page and an agent writes it as one HTML file.
TypeScript
Stop stalling the page after sign-out by dropping Clear-Site-Data
Esteban Suareza458d646b3cc
diff --git a/app/api/artifact-frame/route.ts b/app/api/artifact-frame/route.tsindex 575990c..0e123b1 100644--- a/app/api/artifact-frame/route.ts+++ b/app/api/artifact-frame/route.ts@@ -12,10 +12,15 @@ export const GET = withSessionRoute(async (request, { id: sessionId, user }) =>const past = Number.isSafeInteger(revision) && revision > 0const live = past ? null : await readDesignArtifactAt(sessionId)const artifact = past ? await readGeneration(user.id, sessionId, revision) : live!.html- // ?v= names the revision the page expects. When it is the one live, this response- // is that revision for good, so the browser keeps it; sign-out clears the cache.+ // ?v= names the revision the page expects. When it is the one live, the browser may+ // keep this response but must revalidate it: this route checks the session first,+ // so a reload costs a 304 and a signed-out browser gets nothing from its cache.const version = Number(params.get('v'))- const immutable = live?.html != null && Number.isSafeInteger(version) && version > 0 && version === live.revision+ const cacheable = live?.html != null && Number.isSafeInteger(version) && version > 0 && version === live.revision+ const etag = cacheable ? `"${sessionId}:${live.revision}"` : null+ if (etag && request.headers.get('if-none-match') === etag) {+ return new Response(null, { status: 304, headers: { 'cache-control': 'private, no-cache', etag } })+ }const readyToken = crypto.randomUUID()const preparedArtifact = artifact == null? `<!doctype html><html><head><meta charset="utf-8"><title>No design artifact</title></head><body><p>${@@ -91,7 +96,8 @@ document.querySelector('#frame-root').append(frame);return new Response(html, {status: artifact == null ? 404 : 200,headers: {- 'cache-control': immutable ? 'private, max-age=31536000, immutable' : 'private, no-store',+ 'cache-control': etag ? 'private, no-cache' : 'private, no-store',+ ...(etag ? { etag } : {}),'content-security-policy': csp,'content-type': 'text/html; charset=utf-8','permissions-policy':diff --git a/app/api/auth/signout/route.ts b/app/api/auth/signout/route.tsindex 9906800..f7a3871 100644--- a/app/api/auth/signout/route.ts+++ b/app/api/auth/signout/route.ts@@ -32,8 +32,9 @@ export async function POST(request: NextRequest) {]))const loginId = request.cookies.get(SESSION_COOKIE_NAME)?.valueconst cleared = isLoginId(loginId) ? await sealSessionCookie(null, loginId) : null- // Previews are kept in the browser's cache by revision; none outlive the sign-out.- const response = NextResponse.json({ success: true }, { headers: { 'Cache-Control': 'no-store', 'Clear-Site-Data': '"cache"' } })+ // No Clear-Site-Data: Chrome scans its whole disk cache for it and stalls the page+ // for seconds. Cached previews revalidate against the session instead.+ const response = NextResponse.json({ success: true }, { headers: { 'Cache-Control': 'no-store' } })response.cookies.set(FLOW_COOKIE_NAME, '', { ...FLOW_COOKIE_OPTIONS, maxAge: 0 })if (cleared) response.headers.append('set-cookie', cleared.setCookie)response.headers.append('set-cookie', selectorCookie(null).setCookie)diff --git a/tests/auth.test.mjs b/tests/auth.test.mjsindex cc1fa99..b4eba5f 100644--- a/tests/auth.test.mjs+++ b/tests/auth.test.mjs@@ -697,7 +697,7 @@ test('logout rejects cross-origin or missing Origin; same-origin clears both cooconst request = await requestWithSession('/api/auth/signout', session(), { origin: 'https://fx0.example' })const response = await signout(request)assert.equal(response.status, 200)- assert.equal(response.headers.get('clear-site-data'), '"cache"', 'cached previews do not outlive the sign-out')+ assert.equal(response.headers.get('clear-site-data'), null, 'cached previews revalidate against the session instead')await globalThis.__signoutAfterassert.equal(calls, 1)const cookies = response.headers.getSetCookie()diff --git a/tests/route-security.test.mjs b/tests/route-security.test.mjsindex 9bcae4c..ed3df66 100644--- a/tests/route-security.test.mjs+++ b/tests/route-security.test.mjs@@ -470,7 +470,14 @@ test('the browser keeps a preview only when its URL names the revision that is lglobalThis.securityTest.liveRevision = 2const current = await frame('&v=2')assert.equal(current.status, 200)- assert.equal(current.headers.get('cache-control'), 'private, max-age=31536000, immutable')+ assert.equal(current.headers.get('cache-control'), 'private, no-cache')+ const etag = current.headers.get('etag')+ assert.ok(etag, 'the live revision carries a validator')+ // A kept copy revalidates through the session check and costs an empty 304.+ const revalidate = request('artifact-frame', 'GET', own, undefined, 'null')+ const unchanged = await routes['artifact-frame'].GET(new Request(`${revalidate.url}&v=2`, { headers: { ...Object.fromEntries(revalidate.headers), 'if-none-match': etag } }))+ assert.equal(unchanged.status, 304)+ assert.equal(await unchanged.text(), '')// The live design moved on, or the page names none: the bytes may not be v, so nothing is kept.for (const query of ['&v=1', '&v=3', '', '&v=abc']) {assert.equal((await frame(query)).headers.get('cache-control'), 'private, no-store', query || 'no v')diff --git a/tests/signout.test.mjs b/tests/signout.test.mjsindex a0ded8c..e3793a1 100644--- a/tests/signout.test.mjs+++ b/tests/signout.test.mjs@@ -129,7 +129,7 @@ test('logout waits for one durable revocation write, then returns before cleanupassert.equal((await credentials.readExecutionCredential('phone-chat', 1)).loginId, phone.loginId)assert.equal(fetch.mock.callCount(), 0, 'provider revocation does not block success')assert.equal(state.cancellations.length, 0)- assert.equal(response.headers.get('clear-site-data'), '"cache"')+ assert.equal(response.headers.get('clear-site-data'), null, 'Clear-Site-Data stalls Chrome for seconds; previews revalidate instead')assert.ok(response.headers.getSetCookie().some((cookie) => cookie.startsWith(`${secure.grantCookieName(laptop.loginId)}=`) && cookie.includes('Max-Age=0')))await state.after[0]()assert.deepEqual(state.cancellations, [[laptop.userId, laptop.loginId]])