fix: retain safe dashboard cache data

This commit is contained in:
ajnart committed 2026-08-27 11:32:29 +02:00
1 parent a845d57d57
commit e8ca3d3ad4
2 files changed
+25 -5

No files matched your search

@@ -37,7 +37,7 @@ afterEach(() => {
});
describe("dashboard query persistence", () => {
test("keeps successful dashboard data and skips volatile or unrelated queries", () => {
test("keeps usable dashboard data and skips volatile or unrelated queries", () => {
const widget = createSuccessfulQuery([["widget", "weather", "atLocation"], { type: "query" }], { temp: 21 });
const supporting = createSuccessfulQuery([["app", "byIds"], { type: "query" }], [{ id: "app-1" }]);
const ping = createSuccessfulQuery([["widget", "app", "ping"], { type: "query" }], { status: "online" });
@@ -57,7 +57,7 @@ describe("dashboard query persistence", () => {
expect(shouldPersistDashboardQuery(beszel.query)).toBe(false);
expect(shouldPersistDashboardQuery(unrelated.query)).toBe(false);
expect(shouldPersistDashboardQuery(pending)).toBe(false);
expect(shouldPersistDashboardQuery(failed.query)).toBe(false);
expect(shouldPersistDashboardQuery(failed.query)).toBe(true);
});
test("round-trips dashboard data with SuperJSON in an isolated session scope", async () => {
@@ -68,14 +68,31 @@ describe("dashboard query persistence", () => {
const { queryClient } = createSuccessfulQuery([["widget", "calendar", "findAllEvents"], { type: "query" }], {
startsAt: new Date("2026-08-22T12:00:00.000Z"),
});
queryClient.getMutationCache().build(
queryClient,
{ mutationKey: ["integration", "create"], mutationFn: async () => undefined },
{
context: undefined,
data: undefined,
error: null,
failureCount: 0,
failureReason: null,
isPaused: true,
status: "pending",
submittedAt: Date.now(),
variables: { apiKey: "secret" },
},
);
const persistedClient: PersistedClient = {
timestamp: Date.now(),
buster: queryPersistenceBuster,
clientState: dehydrate(queryClient, userA.dehydrateOptions),
};
expect(persistedClient.clientState.mutations).toHaveLength(0);
await userA.persister.persistClient(persistedClient);
await vi.runAllTimersAsync();
await vi.advanceTimersByTimeAsync(1_000);
const restored = await userA.persister.restoreClient();
expect(restored?.clientState.queries[0]?.state.data).toEqual({
@@ -19,7 +19,7 @@ interface PersistableQuery {
}
export const shouldPersistDashboardQuery = (query: PersistableQuery) =>
query.state.status === "success" && query.state.data !== undefined && isWidgetDataQueryKey(query.queryKey);
query.state.data !== undefined && isWidgetDataQueryKey(query.queryKey);
const getSessionStorage = () => {
if (typeof window === "undefined") return undefined;
@@ -46,5 +46,8 @@ export const createSessionQueryPersistence = (
}),
maxAge: queryCacheDefaultGcTimeMs,
buster: queryPersistenceBuster,
dehydrateOptions: { shouldDehydrateQuery: shouldPersistDashboardQuery },
dehydrateOptions: {
shouldDehydrateMutation: () => false,
shouldDehydrateQuery: shouldPersistDashboardQuery,
},
});