mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 23:52:24 +00:00
perf(insights): mount below-the-fold overview sections near the viewport
Entering the Insights overview fired every section's reads at once, about two dozen requests on a seeded account, and the server worked through them in a queue: the hero and the first sections waited behind trends, the period review, cycle, signals, rhythm events, health status, breathing, labs and ECG, all far below the fold. The first three sections in the account's own order still mount with the page. Every later one renders a 1 px sentinel until it comes within 400 px of the visible area of the shell's scroll container, then mounts and fetches; once mounted it stays. Measured on a seeded account under a 4x CPU slowdown: 24 -> 13 requests on entry, the entry network span 1.3-1.8 s -> 0.5-0.9 s, LCP about 1.35 s -> 0.9 s, blocking time about 550 ms -> 200 ms. Scrolling to the end still loads all 25 and renders all ten sections. Specs that assert on, measure or scan the lower sections scroll them in first through a shared helper, so their coverage is unchanged.
This commit is contained in:
1 parent
fa48110875
commit
4916a29bbe
7 files changed
+177
-2
No files matched your search
@@ -0,0 +1,23 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { renderToStaticMarkup } from "react-dom/server";
|
||||
|
||||
import { DeferUntilNear } from "../defer-until-near";
|
||||
|
||||
/**
|
||||
* The first paint of a deferred slot is the sentinel alone: the children,
|
||||
* and so every read they own, wait until the slot nears the visible area.
|
||||
* The mounted state is exercised in a real browser by the e2e specs that
|
||||
* reveal the Insights overview (`e2e/utils/deferred-sections.ts`).
|
||||
*/
|
||||
describe("<DeferUntilNear>", () => {
|
||||
it("paints only a 1 px sentinel, never the children, before it is near", () => {
|
||||
const html = renderToStaticMarkup(
|
||||
<DeferUntilNear id="labs-changes">
|
||||
<section data-slot="labs-changes-section">reads on mount</section>
|
||||
</DeferUntilNear>,
|
||||
);
|
||||
expect(html).toBe(
|
||||
'<div data-deferred-section="labs-changes" aria-hidden="true" class="h-px"></div>',
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,73 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useRef, useState, type ReactNode } from "react";
|
||||
|
||||
/**
|
||||
* How far ahead of the visible area a deferred section mounts. Roughly half a
|
||||
* phone screen: far enough that a section is usually painted before it
|
||||
* scrolls in, close enough that a visit that never scrolls never asks for it.
|
||||
*/
|
||||
export const DEFER_UNTIL_NEAR_MARGIN = "400px 0px";
|
||||
|
||||
/** The nearest ancestor that scrolls vertically, or `null` for the viewport. */
|
||||
function scrollRoot(el: HTMLElement): Element | null {
|
||||
for (let node = el.parentElement; node; node = node.parentElement) {
|
||||
const overflowY = getComputedStyle(node).overflowY;
|
||||
if (overflowY === "auto" || overflowY === "scroll") return node;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mount `children` only once their slot comes near the visible area.
|
||||
*
|
||||
* Until then a 1 px sentinel holds the slot, so a section that owns its own
|
||||
* reads does not send them on page entry. Observed against the nearest
|
||||
* scroll container (the app shell scrolls inside `<main>`, not the window),
|
||||
* since a root margin only widens the root and an observer on the viewport
|
||||
* would not see a section until it was already on screen.
|
||||
*
|
||||
* Once mounted the children stay mounted, and the sentinel leaves no wrapper
|
||||
* behind: the rendered tree is the same as an eager mount. Without
|
||||
* `IntersectionObserver` the children mount on the next task.
|
||||
*/
|
||||
export function DeferUntilNear({
|
||||
id,
|
||||
children,
|
||||
}: {
|
||||
/** Names the slot for tests: `data-deferred-section`. */
|
||||
id: string;
|
||||
children: ReactNode;
|
||||
}) {
|
||||
const [near, setNear] = useState(false);
|
||||
const sentinelRef = useRef<HTMLDivElement | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (near) return;
|
||||
const el = sentinelRef.current;
|
||||
if (!el) return;
|
||||
if (typeof IntersectionObserver === "undefined") {
|
||||
// No observer to wait on: mount on the next task instead of never.
|
||||
const timer = setTimeout(() => setNear(true));
|
||||
return () => clearTimeout(timer);
|
||||
}
|
||||
const observer = new IntersectionObserver(
|
||||
(entries) => {
|
||||
if (entries.some((entry) => entry.isIntersecting)) setNear(true);
|
||||
},
|
||||
{ root: scrollRoot(el), rootMargin: DEFER_UNTIL_NEAR_MARGIN },
|
||||
);
|
||||
observer.observe(el);
|
||||
return () => observer.disconnect();
|
||||
}, [near]);
|
||||
|
||||
if (near) return <>{children}</>;
|
||||
return (
|
||||
<div
|
||||
ref={sentinelRef}
|
||||
data-deferred-section={id}
|
||||
aria-hidden="true"
|
||||
className="h-px"
|
||||
/>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user