From fd0c2337c5f85cf2c2850a41feeb150a7ef27fe6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Tue, 10 Nov 2026 20:33:53 +0000 Subject: [PATCH] perf(docs): load showcase film tiles only when they are near the screen (#7507) --- docs/snippets/docs-video.jsx | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/docs/snippets/docs-video.jsx b/docs/snippets/docs-video.jsx index ce87454b88..a71a6855c4 100644 --- a/docs/snippets/docs-video.jsx +++ b/docs/snippets/docs-video.jsx @@ -455,6 +455,35 @@ export const ShowcaseWall = () => { } }, [reduced]); + // A tile fetches and plays its card only once it is near the viewport (and pauses when it leaves), so films far down + // the page do not compete with what the visitor is looking at; until then its poster shows, the same first frame. + const [near, setNear] = useState({}); + useEffect(() => { + const wall = wallRef.current; + if (reduced || !wall) return; + const tiles = wall.querySelectorAll("video[data-film]"); + if (typeof IntersectionObserver === "undefined") { + setNear(Object.fromEntries([...tiles].map((video) => [video.dataset.film, true]))); + return; + } + const observer = new IntersectionObserver( + (entries) => { + for (const entry of entries) { + const video = entry.target; + if (entry.isIntersecting) { + setNear((current) => (current[video.dataset.film] ? current : { ...current, [video.dataset.film]: true })); + if (video.getAttribute("src")) video.play().catch(() => {}); + } else { + video.pause(); + } + } + }, + { rootMargin: "200px 0px" }, + ); + for (const video of tiles) observer.observe(video); + return () => observer.disconnect(); + }, [reduced, openId]); + const open = films.find((film) => film.id === openId) || null; if (open) { @@ -537,9 +566,10 @@ export const ShowcaseWall = () => { }} >