fix: refetch persisted resources when the cache identity becomes known (#12176)

A response is tagged with the cache keys of its request. When the user or
the session they hold is confirmed after a persisted resource's first
request (the first load of a workspace), the keys shown change and the
untagged response is hidden, with nothing refetching: Home's recent runs,
folder names, usernames, pipeline folders and owner counts stayed empty
until their arguments changed. The identity is now part of the resource's
source, and a session confirmation bumps it the way a user change does.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ruben Fiszel
2026-11-03 13:12:21 +01:00
committed by GitHub
co-authored by Claude Opus 5.5
parent c05febcefa
commit df2a631e77
3 changed files with 31 additions and 7 deletions
+15 -3
View File
@@ -27,6 +27,18 @@ export function readSessionHint(cookies?: string): string | undefined {
// it renders as. Another tab logging in swaps the session under this one, whose user stays
// the old one until its next `whoami`: until then, nothing may be read or written as them.
let confirmedHint: string | undefined
const confirmationListeners = new Set<() => void>()
function setConfirmedHint(hint: string | undefined) {
if (hint === confirmedHint) return
confirmedHint = hint
for (const cb of confirmationListeners) cb()
}
/** Called whenever the session this tab's user is confirmed under changes. */
export function onSessionConfirmationChange(cb: () => void): void {
confirmationListeners.add(cb)
}
/** Whether the session the browser holds is still the one this tab's user was confirmed under. */
export function sessionStillConfirmed(): boolean {
@@ -50,7 +62,7 @@ function currentLastUser(): UserExt | undefined {
export function optimisticUser(workspace: string): UserExt | undefined {
const user = currentLastUser()
if (user?.workspace_id !== workspace) return undefined
confirmedHint = readSessionHint()
setConfirmedHint(readSessionHint())
return user
}
@@ -68,7 +80,7 @@ export function rememberUser(user: UserExt, sentWith: string | undefined): void
try {
const hint = readSessionHint()
// Who answered is only known for the session the request went out with.
confirmedHint = hint === sentWith ? hint : undefined
setConfirmedHint(hint === sentWith ? hint : undefined)
if (hint && hint === sentWith)
localStorage.setItem(KEY, JSON.stringify({ hint, user } satisfies LastSession))
else localStorage.removeItem(KEY)
@@ -76,7 +88,7 @@ export function rememberUser(user: UserExt, sentWith: string | undefined): void
}
export function forgetLastSession(): void {
confirmedHint = undefined
setConfirmedHint(undefined)
try {
localStorage.removeItem(KEY)
} catch {}
+11 -3
View File
@@ -6,7 +6,11 @@ import { OpenAPI } from '$lib/gen'
import { userStore } from '$lib/stores'
import { userScopedDb } from '$lib/userScopedDb'
import { getCurrentUserEmail, onUserChange } from '$lib/userScopedStorage'
import { rememberedEmail, sessionStillConfirmed } from '$lib/lastSession'
import {
onSessionConfirmationChange,
rememberedEmail,
sessionStillConfirmed
} from '$lib/lastSession'
// Last-known API responses, kept so a page can paint them before its requests return. Every
// row in them was filtered by row-level security for one user, so an entry is only ever served
@@ -284,7 +288,9 @@ if (BROWSER) {
readSnapshot()
// Loads the whole (bounded) store into memory once the user is known, and keeps only what
// belongs to them: the snapshot's entries for the same identity survive.
onUserChange(() => {
// The identity is known once both the user and the session they hold are: either can come
// second, and a key derived before must follow.
const onIdentityChange = () => {
userTick++
// No user yet (the call at registration, as the app boots): nothing can be read without
// an identity, and the snapshot just loaded is waiting for its user. Logout clears memory
@@ -294,5 +300,7 @@ if (BROWSER) {
const mine = `[${JSON.stringify(identity)},`
for (const key of [...memory.keys()]) if (!key.startsWith(mine)) memory.delete(key)
void hydrate()
})
}
onUserChange(onIdentityChange)
onSessionConfirmationChange(onIdentityChange)
}
+5 -1
View File
@@ -67,7 +67,11 @@ export function persistedResource<A, T>(
let latest = 0
let inFlight = $state(false)
let error = $state<unknown>(undefined)
const res = resource<A, Tagged<T>>(source, async (args) => {
// The identity is a source too: a response is tagged with the keys of its request, and once
// the identity changes no response tagged before matches the keys shown, so without a new
// request nothing would be shown until the arguments next change.
const sourceWithIdentity = () => ({ args: source(), identity: cacheIdentity() })
const res = resource(sourceWithIdentity, async ({ args }): Promise<Tagged<T>> => {
const id = ++latest
inFlight = true
try {