The best way to control Claude Code, Codex and more from any computer or phone
fix(web): retry a failed theme wallpaper download when back online (#37843)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Wout Stiens committed
Nov 17, 2026 at 02:48 UTC
b75424062af3fdc838cb8d8b411ebaf79024b546
2 files changed
+171
-13
apps/web/src/themeBackground.test.ts
new
+132
@@ -0,0 +1,132 @@
1
+import { EnvironmentId } from "@t3tools/contracts";
2
+import { describe, expect, it, vi } from "vite-plus/test";
3
+
4
+const query = vi.hoisted(() => vi.fn());
5
+vi.mock(
6
+ import("@t3tools/client-runtime/state/runtime"),
7
+ () =>
8
+ ({
9
+ executeAtomQuery: query,
10
+ }) as never,
11
+);
12
+vi.mock(import("./state/primaryEnvironment"), async () => {
13
+ const { Atom } = await import("effect/reactivity");
14
+ return { themeEnvironmentIdAtom: Atom.make(EnvironmentId.make("host")) } as never;
15
+});
16
+vi.mock(import("./state/server"), async () => {
17
+ const { Atom } = await import("effect/reactivity");
18
+ return {
19
+ themeServerEnvironmentThemesAtom: Atom.make([{ id: "nightfall", background: "revision-1" }]),
20
+ } as never;
21
+});
22
+vi.mock(import("./state/session"), async () => {
23
+ const { Atom } = await import("effect/reactivity");
24
+ return {
25
+ environmentSession: {
26
+ preparedConnectionValueAtom: Atom.family(() =>
27
+ Atom.make({ _tag: "Some", value: { httpBaseUrl: "https://example.com/" } }),
28
+ ),
29
+ },
30
+ } as never;
31
+});
32
+vi.mock(
33
+ import("./state/assets"),
34
+ () =>
35
+ ({
36
+ assetEnvironment: { createUrl: vi.fn() },
37
+ }) as never,
38
+);
39
+
40
+import { applyThemeBackground, startThemeBackground } from "./themeBackground";
41
+
42
+const BACKGROUND = "--theme-background-image";
43
+const image = new Blob(["image"], { type: "image/png" });
44
+const offline = () => Promise.reject(new TypeError("Failed to fetch"));
45
+const loaded = async () => ({ ok: true, blob: async () => image });
46
+
47
+function setup() {
48
+ vi.useFakeTimers();
49
+ const properties = new Map<string, string>();
50
+ const browser = new EventTarget();
51
+ vi.stubGlobal("window", browser);
52
+ vi.stubGlobal("document", {
53
+ documentElement: {
54
+ dataset: { themeId: "nightfall" },
55
+ style: {
56
+ setProperty: (name: string, value: string) => properties.set(name, value),
57
+ removeProperty: (name: string) => properties.delete(name),
58
+ },
59
+ },
60
+ });
61
+ query.mockResolvedValue({ _tag: "Success", value: { relativeUrl: "/background.png" } });
62
+ const fetchBackground = vi.fn<() => Promise<unknown>>(offline);
63
+ vi.stubGlobal("fetch", fetchBackground);
64
+ const stop = startThemeBackground();
65
+ return {
66
+ properties,
67
+ browser,
68
+ fetchBackground,
69
+ stop,
70
+ [Symbol.dispose]() {
71
+ stop();
72
+ applyThemeBackground(undefined);
73
+ vi.unstubAllGlobals();
74
+ vi.useRealTimers();
75
+ },
76
+ };
77
+}
78
+
79
+describe("theme backgrounds", () => {
80
+ it("retries a failed download after 1, 2, 4 and 8 seconds", async () => {
81
+ using t = setup();
82
+ t.fetchBackground
83
+ .mockImplementationOnce(async () => new Response(null, { status: 503 }))
84
+ .mockImplementationOnce(offline)
85
+ .mockImplementationOnce(offline)
86
+ .mockImplementationOnce(offline)
87
+ .mockImplementation(loaded);
88
+
89
+ applyThemeBackground("nightfall");
90
+ await vi.advanceTimersByTimeAsync(0);
91
+ for (const [delay, attempts] of [
92
+ [1_000, 1],
93
+ [2_000, 2],
94
+ [4_000, 3],
95
+ [8_000, 4],
96
+ ] as const) {
97
+ await vi.advanceTimersByTimeAsync(delay - 1);
98
+ expect(t.fetchBackground).toHaveBeenCalledTimes(attempts);
99
+ expect(t.properties.has(BACKGROUND)).toBe(false);
100
+ await vi.advanceTimersByTimeAsync(1);
101
+ }
102
+ expect(t.fetchBackground).toHaveBeenCalledTimes(5);
103
+ expect(t.properties.get(BACKGROUND)).toMatch(/^url\("blob:/);
104
+ });
105
+
106
+ it("loads the background once when the browser comes back online after retries stop", async () => {
107
+ using t = setup();
108
+ applyThemeBackground("nightfall");
109
+ await vi.advanceTimersByTimeAsync(30_000);
110
+ expect(t.fetchBackground).toHaveBeenCalledTimes(5);
111
+ expect(t.properties.has(BACKGROUND)).toBe(false);
112
+
113
+ t.fetchBackground.mockImplementation(loaded);
114
+ t.browser.dispatchEvent(new Event("online"));
115
+ await vi.advanceTimersByTimeAsync(0);
116
+ expect(t.properties.get(BACKGROUND)).toMatch(/^url\("blob:/);
117
+
118
+ t.browser.dispatchEvent(new Event("online"));
119
+ await vi.advanceTimersByTimeAsync(30_000);
120
+ expect(t.fetchBackground).toHaveBeenCalledTimes(6);
121
+ });
122
+
123
+ it.each(["theme change", "disposal"])("stops retrying after %s", async (reason) => {
124
+ using t = setup();
125
+ applyThemeBackground("nightfall");
126
+ await vi.advanceTimersByTimeAsync(0);
127
+ if (reason === "theme change") applyThemeBackground(undefined);
128
+ else t.stop();
129
+ await vi.advanceTimersByTimeAsync(30_000);
130
+ expect(t.fetchBackground).toHaveBeenCalledTimes(1);
131
+ });
132
+});
apps/web/src/themeBackground.ts
+39
-13
@@ -9,6 +9,7 @@ import { themeServerEnvironmentThemesAtom } from "./state/server";
9
import { environmentSession } from "./state/session";
10
11
const THEME_BACKGROUND_VARIABLE = "--theme-background-image";
12
+const RETRY_DELAYS_MS = [1_000, 2_000, 4_000, 8_000];
13
14
let shownKey: string | null = null;
15
let loading: AbortController | null = null;
@@ -72,25 +73,50 @@ export function applyThemeBackground(themeId: string | undefined): void {
73
if (environmentId === null || themeId === undefined || key === null) return;
74
const controller = new AbortController();
75
loading = controller;
76
+ let retries = 0;
77
const failed = () => {
78
if (controller.signal.aborted) return;
77
- // Lets the next repaint try again.
78
- shownKey = null;
79
- };
80
- loadBackground(environmentId, themeId, controller.signal).then((url) => {
81
- if (controller.signal.aborted) {
82
- if (url !== null) URL.revokeObjectURL(url);
79
+ const delay = RETRY_DELAYS_MS[retries++];
80
+ if (delay !== undefined) {
81
+ // Transient HTTP failures may never produce a browser online event.
82
+ const timer = setTimeout(load, delay);
83
+ controller.signal.addEventListener("abort", () => clearTimeout(timer), { once: true });
84
return;
85
}
86
loading = null;
86
- if (url === null) failed();
87
- else show(url);
88
- }, failed);
87
+ // Lets the next repaint or `online` event try again.
88
+ shownKey = null;
89
+ };
90
+ const load = () => {
91
+ loadBackground(environmentId, themeId, controller.signal).then((url) => {
92
+ if (controller.signal.aborted) {
93
+ if (url !== null) URL.revokeObjectURL(url);
94
+ return;
95
+ }
96
+ if (url === null) failed();
97
+ else {
98
+ loading = null;
99
+ show(url);
100
+ }
101
+ }, failed);
102
+ };
103
+ load();
104
}
105
91
-/** A new image revision does not change the palette, so it does not repaint the theme. */
106
+/**
107
+ * A new image revision does not change the palette, so it does not repaint the theme.
108
+ * Coming back online retries a download that failed; a shown image is left alone.
109
+ */
110
export function startThemeBackground(): () => void {
93
- return subscribeAtomChanges(themeServerEnvironmentThemesAtom, () =>
94
- applyThemeBackground(document.documentElement.dataset.themeId),
95
- );
111
+ const reapply = () => applyThemeBackground(document.documentElement.dataset.themeId);
112
+ const unsubscribe = subscribeAtomChanges(themeServerEnvironmentThemesAtom, reapply);
113
+ window.addEventListener("online", reapply);
114
+ return () => {
115
+ unsubscribe();
116
+ window.removeEventListener("online", reapply);
117
+ loading?.abort();
118
+ loading = null;
119
+ shownKey = null;
120
+ show(null);
121
+ };
122
}