Files
siftlode/frontend/src/lib/lazyWithPreload.dom.test.tsx
T
peter 5346efcd10 fix(boot): a new tab knows its account before anything renders
With Settings -> Appearance -> Startup turned off, every NEW tab or window still
opened on Home; only a same-tab reload honoured it. The provider tree mounted
before /api/me answered, so NavigationProvider's initializer could not read the
per-account opt-out, landed on Home and mirrored ?page=home into the address bar.
When App pinned the account a moment later, the re-hydrate re-read that mirror
and stayed on Home. The same mount-then-patch shape made a new tab start on
default filters, the default theme and the browser language, and correct each
after the first paint.

BootGate (main.tsx, above the providers) now owns the setup-status and /api/me
queries and mounts the tree only once the identity is settled: it pins the tab's
account and adopts the account's language in a layout effect, so the first frame
of the app is already right. The tree is keyed on the account id, which makes
"per-account state is read once, for this account" a structural guarantee.

With that, the whole re-hydrate machinery is gone: useAccountRehydrate, the
reactive active-account store, the markDirty guards, the pin-deferred saved-view
seed and the present-dimension bookkeeping of the shared Channels/Plex links.
PrefsProvider, FeedFiltersProvider, usePref and App's panel layouts seed from the
me cache instead of adopting it in an effect; the theme is applied in layout
effects. The app chunk is fetched in parallel with the probe, and the boot screen
only fades its label in after 400 ms, on one clock across the gate and the
Suspense fallback.

Module switches no longer flash a placeholder either. Measured frame by frame
in Chromium and WebKit, every first visit to a module showed "Loading..." for
17-70 ms: the lazy chunk's Suspense fallback, then often the first fetch's
LoadingState. Pages are now lazyWithPreload components, and App fetches the
chunks of every module the account can reach (plus the channel page) once the
first page is idle; a preloaded page renders synchronously and never suspends.
LoadingState, the one shared loading visual, fades in only after 300 ms, so a
fast load shows nothing in between and a slow one still gets its spinner.
2026-09-14 22:29:33 +02:00

70 lines
2.7 KiB
TypeScript

// @vitest-environment jsdom
//
// lazyWithPreload: once preloaded, a page renders synchronously — no Suspense fallback, which on a module
// switch is a "Loading…" flash in place of the page. And a page that mounted through the lazy path is
// not remounted (its state kept) once the module is loaded.
import { describe, it, expect, afterEach } from "vitest";
import { Suspense, useState, type ComponentType } from "react";
import { render, screen, act, fireEvent, cleanup } from "@testing-library/react";
import { lazyWithPreload } from "./lazyWithPreload";
function Counter() {
const [n, setN] = useState(0);
return <button onClick={() => setN(n + 1)}>count {n}</button>;
}
const module = (): Promise<{ default: ComponentType<object> }> =>
Promise.resolve({ default: Counter });
const inSuspense = (C: ComponentType) => (
<Suspense fallback={<span>fallback</span>}>
<C />
</Suspense>
);
afterEach(cleanup);
describe("lazyWithPreload", () => {
it("renders a preloaded page on its first render, without the fallback", async () => {
const Page = lazyWithPreload(module);
await Page.preload();
render(inSuspense(Page));
// Synchronously after render — no act/await, so a suspension would still be showing its fallback.
expect(screen.queryByText("fallback")).toBeNull();
expect(screen.getByText("count 0")).toBeTruthy();
});
it("without a preload it suspends once, then renders", async () => {
const Page = lazyWithPreload(module);
render(inSuspense(Page));
expect(screen.getByText("fallback")).toBeTruthy();
expect(await screen.findByText("count 0")).toBeTruthy();
});
it("a page mounted through the lazy path keeps its state across later renders", async () => {
// Mounted into a boundary that is ALREADY showing content — how a module switch mounts a page.
const Page = lazyWithPreload(module);
const shell = (show: boolean, tick: number) => (
<Suspense fallback={<span>fallback</span>}>
<i>shell {tick}</i>
{show && <Page />}
</Suspense>
);
const { rerender } = render(shell(false, 0));
rerender(shell(true, 0));
fireEvent.click(await screen.findByText("count 0")); // the module has loaded by now
rerender(shell(true, 1)); // a later render of the same mount
expect(screen.getByText("count 1")).toBeTruthy(); // not remounted as the loaded component
});
it("retries a preload that failed", async () => {
let calls = 0;
const Page = lazyWithPreload(() =>
++calls === 1
? Promise.reject<{ default: ComponentType<object> }>(new Error("offline"))
: module(),
);
await expect(Page.preload()).rejects.toThrow("offline");
await act(() => Page.preload());
expect(calls).toBe(2);
});
});