perf(watch): a shared link loads the player, not the app
A /watch link is opened by whoever it was sent to — signed out, often on a phone on mobile data — and it booted through main.tsx: every app provider, the app's i18n (every locale of every area), the flag sprites, ~392 KB gzip before the player could start. It now has its own HTML entry, watch.html → watchMain.tsx, which imports WatchPage and what it renders; the server hands every /watch/ path that page (the OG block injected as before), and vite dev does the same. Measured on the build: /watch JS 290 → 87 KB gzip, CSS 104 → 19 KB; the app's own bytes unchanged. Two leaks had to go for that: lib/format imported the app's i18n for one "just now" string (the relative-time formatters now take `t`, like the file's other t-formatters), and the boundary's crash copy came from the same i18n (CrashBoundary takes its copy; ErrorBoundary is the app's wrapper, the watch entry gives it the page's own strings). The page's popover tree went too: it renders no popover. Held by: a contract test walking the entry's runtime imports, naming the path to anything only the app may load (red on a planted i18n import in lib/format), and holding watch.html's head equal to index.html's; a DB-lane test that every /watch/ path gets the player's page with the link's OG tags and app paths get the app (red on the old routing). Screenshots at 375 px, EN and HU, video / audio / password / gone / wrong password / unlocked: identical to the old entry's, bar the video's buffered bar.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import type { TFunction } from "i18next";
|
||||
import {
|
||||
RELATIVE_UNITS,
|
||||
formatBytes,
|
||||
@@ -18,12 +19,10 @@ import {
|
||||
import en from "../i18n/locales/en/time.json";
|
||||
import hu from "../i18n/locales/hu/time.json";
|
||||
|
||||
// i18n boots the whole locale glob (and touches localStorage/document) on import, so stub it.
|
||||
// `language` is mutable because relativeFromMs reads it per call to pick the Intl locale — these
|
||||
// `language` is mutable because relativeFromMs takes it per call to pick the Intl locale — these
|
||||
// cases assert both languages by flipping it. `t` echoes the key: the only string still coming
|
||||
// from the locale files is justNow.
|
||||
const i18nStub = vi.hoisted(() => ({ language: "en", t: (key: string) => key }));
|
||||
vi.mock("../i18n", () => ({ default: i18nStub }));
|
||||
const i18nStub = { language: "en", t: ((key: string) => key) as unknown as TFunction };
|
||||
|
||||
const MIN = 60;
|
||||
const HOUR = 3600;
|
||||
@@ -74,29 +73,31 @@ describe("relativeFromMs", () => {
|
||||
|
||||
for (const [secs, tier, expectedEn, expectedHu] of CASES) {
|
||||
it(`${secs}s is ${tier} — "${expectedEn}" / "${expectedHu}"`, () => {
|
||||
expect(relativeFromMs(NOW - secs * 1000, i18nStub.language)).toBe(expectedEn);
|
||||
expect(relativeFromMs(NOW - secs * 1000, i18nStub.language, i18nStub.t)).toBe(expectedEn);
|
||||
i18nStub.language = "hu";
|
||||
expect(relativeFromMs(NOW - secs * 1000, i18nStub.language)).toBe(expectedHu);
|
||||
expect(relativeFromMs(NOW - secs * 1000, i18nStub.language, i18nStub.t)).toBe(expectedHu);
|
||||
});
|
||||
}
|
||||
|
||||
it("follows a language switch at runtime", () => {
|
||||
const twoHoursAgo = NOW - 2 * HOUR * 1000;
|
||||
expect(relativeFromMs(twoHoursAgo, i18nStub.language)).toBe("2h ago");
|
||||
expect(relativeFromMs(twoHoursAgo, i18nStub.language, i18nStub.t)).toBe("2h ago");
|
||||
i18nStub.language = "hu";
|
||||
expect(relativeFromMs(twoHoursAgo, i18nStub.language)).toBe("2 órája");
|
||||
expect(relativeFromMs(twoHoursAgo, i18nStub.language, i18nStub.t)).toBe("2 órája");
|
||||
i18nStub.language = "en";
|
||||
expect(relativeFromMs(twoHoursAgo, i18nStub.language)).toBe("2h ago");
|
||||
expect(relativeFromMs(twoHoursAgo, i18nStub.language, i18nStub.t)).toBe("2h ago");
|
||||
});
|
||||
|
||||
it("clamps a future timestamp instead of counting up", () => {
|
||||
expect(relativeFromMs(NOW + 5 * DAY * 1000, i18nStub.language)).toBe("time.justNow");
|
||||
expect(relativeFromMs(NOW + 5 * DAY * 1000, i18nStub.language, i18nStub.t)).toBe(
|
||||
"time.justNow",
|
||||
);
|
||||
});
|
||||
|
||||
it("renders nothing for a timestamp it cannot read", () => {
|
||||
expect(relativeTime("not-a-date", i18nStub.language)).toBe("");
|
||||
expect(relativeTime(null, i18nStub.language)).toBe("");
|
||||
expect(relativeFromMs(NaN, i18nStub.language)).toBe("");
|
||||
expect(relativeTime("not-a-date", i18nStub.language, i18nStub.t)).toBe("");
|
||||
expect(relativeTime(null, i18nStub.language, i18nStub.t)).toBe("");
|
||||
expect(relativeFromMs(NaN, i18nStub.language, i18nStub.t)).toBe("");
|
||||
});
|
||||
|
||||
// Derived from RELATIVE_UNITS, not from CASES, so adding a tier to the table without covering
|
||||
|
||||
Reference in New Issue
Block a user