Skip to content

A one-of-a-kind resume builder that keeps your privacy in mind. Completely secure, customizable, portable, open-source and free forever. Try it out today!

fix: warm scene drawings after theme changes

Amruth Pillaib760da5cf54c
diff --git a/apps/web/src/features/homepage/scroll.test.tsx b/apps/web/src/features/homepage/scroll.test.tsx
index 6b51053af..4d2f78076 100644
--- a/apps/web/src/features/homepage/scroll.test.tsx
+++ b/apps/web/src/features/homepage/scroll.test.tsx
@@ -36,6 +36,47 @@ describe("scroll engine", () => {
useLanding.setState({ designExiting: false });
});
+ it.each(["light", "dark"])("warms newly visible images in an already nearby scene after %s mode", async (initial) => {
+ let theme = initial;
+ const decoded: (string | null)[] = [];
+ vi.stubGlobal("innerHeight", 900);
+ vi.stubGlobal("matchMedia", () => ({ matches: false, addEventListener: vi.fn(), removeEventListener: vi.fn() }));
+ vi.spyOn(HTMLElement.prototype, "getBoundingClientRect").mockImplementation(function (this: HTMLElement) {
+ const top = this.dataset.scene === String(SCENE.footer) ? 1700 : 9000;
+ return { top, bottom: top + 800, height: 800, width: 0 } as DOMRect;
+ });
+ vi.spyOn(HTMLElement.prototype, "checkVisibility").mockImplementation(function (this: HTMLElement) {
+ return this.dataset.theme === theme;
+ });
+ vi.spyOn(HTMLImageElement.prototype, "decode").mockImplementation(function (this: HTMLImageElement) {
+ decoded.push(this.getAttribute("src"));
+ return Promise.resolve();
+ });
+ function Scenes() {
+ const root = useRef<HTMLDivElement>(null);
+ useScrollScenes(root);
+ return (
+ <div ref={root}>
+ <section data-scene={SCENE.write} data-pin>
+ <img alt="" src="/far.webp" data-theme="light" />
+ </section>
+ <section data-scene={SCENE.footer}>
+ <img alt="" src="/desk-light.webp" data-theme="light" />
+ <img alt="" src="/desk-dark.webp" data-theme="dark" />
+ </section>
+ </div>
+ );
+ }
+ render(<Scenes />);
+ await waitFor(() => expect(decoded).toEqual([`/desk-${initial}.webp`]));
+ theme = initial === "light" ? "dark" : "light";
+ fireEvent.scroll(window);
+ await waitFor(() => expect(decoded).toEqual([`/desk-${initial}.webp`, `/desk-${theme}.webp`]));
+ fireEvent.scroll(window);
+ await new Promise<void>((resolve) => requestAnimationFrame(() => requestAnimationFrame(() => resolve())));
+ expect(decoded).toHaveLength(2);
+ });
+
it("keeps header progress and ink local while Share keeps its own night", async () => {
let shareTop = -450;
const motion = { matches: false, addEventListener: vi.fn(), removeEventListener: vi.fn() };
diff --git a/apps/web/src/features/homepage/scroll.ts b/apps/web/src/features/homepage/scroll.ts
index e40df132c..1a8dd6c12 100644
--- a/apps/web/src/features/homepage/scroll.ts
+++ b/apps/web/src/features/homepage/scroll.ts
@@ -167,16 +167,17 @@ export function goToScene(index: number, at: number) {
* Renders a pinned scene and decodes its images while it's still a screen or two away, so its first frame on screen
* isn't the one that pays for them. Scenes further off go back to skipping their rendering (landing.css).
*/
-function warmScenes(sections: HTMLElement[], nearby: Set<number>, decoded: WeakSet<HTMLElement>) {
+function warmScenes(sections: HTMLElement[], nearby: Set<number>, decoded: WeakSet<HTMLImageElement>) {
for (const section of sections) {
const near = nearby.has(Number(section.dataset.scene));
if (section.dataset.pin !== undefined && near !== (section.dataset.warm !== undefined))
section.toggleAttribute("data-warm", near);
- if (!near || decoded.has(section)) continue;
- decoded.add(section);
+ if (!near) continue;
for (const image of section.querySelectorAll("img")) {
+ if (decoded.has(image)) continue;
// Only the drawing for the current theme: the other one is display: none.
if (typeof image.checkVisibility === "function" && !image.checkVisibility()) continue;
+ decoded.add(image);
image.loading = "eager";
void image.decode().catch(() => undefined);
}
@@ -236,7 +237,7 @@ export function useScrollScenes(root: RefObject<HTMLElement | null>) {
// Where Check's lens comes to rest, as a share of its page: over the date line it flags.
let lensPark = { x: 60, y: 50 };
- const decodedScenes = new WeakSet<HTMLElement>();
+ const decodedImages = new WeakSet<HTMLImageElement>();
// Values that only change with the viewport, not with scrolling.
const measuredScenes = new Set<number>();
@@ -310,7 +311,7 @@ export function useScrollScenes(root: RefObject<HTMLElement | null>) {
}
if (rect.top <= viewportHeight / 2 && rect.bottom > viewportHeight / 2) activeScene = index;
}
- warmScenes(sections, nearby, decodedScenes);
+ warmScenes(sections, nearby, decodedImages);
// Descendant geometry would force content-visibility to render an offscreen scene.
layout(nearby);
// The footer's desk paints itself in as it scrolls up, finishing exactly when the page ends.
diff --git a/tests/e2e/specs/motion-feedback.spec.ts b/tests/e2e/specs/motion-feedback.spec.ts
index f21671d08..85b94bec4 100644
--- a/tests/e2e/specs/motion-feedback.spec.ts
+++ b/tests/e2e/specs/motion-feedback.spec.ts
@@ -201,6 +201,60 @@ test("the builder shows its own frame while a resume loads", async ({ authPage:
});
test.describe("homepage", () => {
+ for (const width of [1440, 390])
+ for (const initial of ["light", "dark"] as const)
+ test(`warms the other theme before the footer enters at ${width}px from ${initial}`, async ({ page }) => {
+ await page.setViewportSize({ width, height: 900 });
+ await page.emulateMedia({ colorScheme: initial });
+ await page.addInitScript(() => {
+ const decoded: string[] = [];
+ (window as Window & { homepageDecoded: string[] }).homepageDecoded = decoded;
+ const original = HTMLImageElement.prototype.decode;
+ HTMLImageElement.prototype.decode = function () {
+ const src = this.currentSrc || this.src;
+ return original.call(this).then(() => {
+ decoded.push(src);
+ });
+ };
+ });
+ await page.goto("/");
+ await page.waitForLoadState("networkidle");
+ await expect
+ .poll(() => page.locator("html").evaluate((el) => el.classList.contains("dark")))
+ .toBe(initial === "dark");
+ const footer = page.locator('[data-scene="12"]');
+ await footer.evaluate((el) =>
+ scrollTo({ top: el.getBoundingClientRect().top + scrollY - 1700, behavior: "instant" }),
+ );
+ const decoded = () =>
+ page.evaluate(() =>
+ (window as Window & { homepageDecoded: string[] }).homepageDecoded.filter((src) =>
+ src.includes("footer-desk-"),
+ ),
+ );
+ await expect.poll(decoded).toHaveLength(1);
+ expect((await decoded())[0]).toContain(`footer-desk-${initial}`);
+ await page
+ .getByRole("button", {
+ name: initial === "light" ? "Switch to dark mode" : "Switch to light mode",
+ exact: true,
+ })
+ .first()
+ .click();
+ await expect
+ .poll(() => page.locator("html").evaluate((el) => el.classList.contains("dark")))
+ .toBe(initial === "light");
+ await page.mouse.move(width / 2, 450);
+ const before = await page.evaluate(() => scrollY);
+ await page.mouse.wheel(0, 80);
+ await expect.poll(() => page.evaluate(() => scrollY)).toBeGreaterThan(before);
+ await expect.poll(decoded).toHaveLength(2);
+ expect((await decoded())[1]).toContain(`footer-desk-${initial === "light" ? "dark" : "light"}`);
+ const top = await footer.evaluate((el) => el.getBoundingClientRect().top);
+ expect(top).toBeGreaterThan(900);
+ expect(top).toBeLessThan(1800);
+ });
+
test("warms only nearby scenes and keeps scrolling when checkVisibility is unavailable", async ({ page }) => {
await page.setViewportSize({ width: 1440, height: 900 });
await page.addInitScript(() => {