fix: read table and sidebar geometry after layout

Two reads forced a layout of the whole page. The sidebar measured the
current link as the script ran (114 ms on gaps, mobile, 4x CPU
throttling), and every table's width was read in an idle callback that
still found the layout invalidated (103 ms), then again after enhance()
wrote captions (131 ms). The sidebar now reads an IntersectionObserver
entry and the tables a ResizeObserver, whose callbacks run once the page
is laid out. The observer also covers a window resize and a block that
opens.
This commit is contained in:
Abdessamad Derraz committed 2026-11-19 18:42:55 +01:00
1 parent 3eb5d1fce1
commit cb23f7f8a4
2 files changed
+54 -23

No files matched your search

+22 -1
View File
@@ -143,6 +143,22 @@ class ScrollingTablesHaveTheirOwnName(unittest.TestCase):
self.assertIn("`${heading}, table ${count}`", source)
class TablesMeasuredWithoutAForcedLayout(unittest.TestCase):
"""Measured in an idle callback after load, every table's scrollWidth
forced a layout of the gaps page (103 ms, mobile, 4x CPU throttling)."""
def test_widths_read_from_a_resize_observer(self):
source = _read(SITE_TS)
block = source[source.index("const enhanceTables = () =>"):source.index("// Paths")]
self.assertIn("new ResizeObserver(", block)
self.assertIn("sized.observe(table);", block)
self.assertNotIn("requestIdleCallback", block)
self.assertNotIn("addEventListener('resize'", block)
# Read after enhance() wrote captions and labels, the width forced
# the layout again (131 ms).
self.assertNotIn("requestAnimationFrame(() => markScrollable", block)
class CommandBlocksHaveNoWindowDots(unittest.TestCase):
def test_the_dots_are_off_in_the_theme(self):
self.assertIn("terminalTitlebarDotsOpacity: '0'", _read(CONFIG))
@@ -905,7 +921,12 @@ class TheSidebarShowsTheCurrentPage(unittest.TestCase):
source = _read(SITE_TS)
block = source[source.index("const showCurrentPage = () =>"):source.index("// A glossary term")]
self.assertIn("pane.scrollTop +=", block)
self.assertIn("pane.clientHeight / 3", block)
self.assertIn("box.height / 3", block)
# Read from an observer once laid out: a geometry read as the script
# ran forced a layout of the whole page (114 ms on gaps, mobile).
self.assertIn("new IntersectionObserver(", block)
self.assertNotIn("getBoundingClientRect", block)
self.assertNotIn("clientHeight", block)
self.assertIn("addEventListener('toggle'", block)
self.assertNotIn("scrollIntoView", source)
self.assertIn("showCurrentPage();", source)
+32 -22
View File
@@ -182,7 +182,7 @@ const enhanceTables = () => {
}
filterTable(table);
};
// A table is set up and measured when it nears the screen. Doing all of
// A table is set up when it nears the screen. Doing all of
// them at load relaid out every row of a 3,800-row page after its first
// paint. One that appears later (a block opened, a tab chosen) is handled
// then.
@@ -194,32 +194,32 @@ const enhanceTables = () => {
if (!near.has(table)) enhance(table);
near.add(table);
}
if (shown.length) requestAnimationFrame(() => markScrollable(shown));
},
{ rootMargin: '400px 0px' },
);
// Every table is measured once the page is drawn and idle, before anything
// is written, so the read finds the layout done instead of forcing it: a
// keyboard reaches a scrolling table anywhere on the page, not only near
// the screen.
const idle = window.requestIdleCallback ?? ((callback: () => void) => setTimeout(callback, 1));
// Every table is measured from a ResizeObserver: its callback runs once
// the browser has laid the page out, so the read never forces a layout,
// and a keyboard reaches a scrolling table anywhere on the page, not only
// near the screen. The same callback measures again when the window
// changes width or a closed block opens. Measured in an idle callback
// after load, the read still forced a layout of the gaps page (103 ms,
// mobile, 4x CPU throttling). A table in a closed block is not laid out,
// and measuring it would lay it out: it is skipped until its block opens.
const sized = new ResizeObserver((entries) =>
markScrollable(
entries.map((entry) => entry.target as HTMLTableElement).filter((table) => table.checkVisibility?.() ?? true),
),
);
const start = () =>
idle(() => {
// A table in a closed block is not laid out, and measuring it would
// lay it out: it is measured when its block opens.
markScrollable(tables.filter((table) => table.checkVisibility?.() ?? true));
tables.forEach((table) => observer.observe(table));
tables.forEach((table) => {
sized.observe(table);
observer.observe(table);
});
if (document.readyState === 'complete') start();
else addEventListener('load', start, { once: true });
// A box above a closed block is typed in before its table is ever near
// the screen: it is wired now.
for (const table of tables) if (table.closest('details:not([open])') && filterControls(table)) filterTable(table);
let timer = 0;
addEventListener('resize', () => {
clearTimeout(timer);
timer = window.setTimeout(() => markScrollable([...near]), 150);
});
};
// Paths
@@ -928,17 +928,27 @@ const osTabs = () => {
// The sidebar lists 91 manufacturers: the current page's link sat below the
// pane, out of sight. On load, and when the phone menu opens, the pane
// scrolls so the link sits a third of the way down. Only the pane scrolls,
// never the page.
// never the page. The positions come from an IntersectionObserver, read
// once the page is laid out: reading them as the script ran laid out the
// whole page early, a forced reflow of 114 ms on the gaps page (mobile,
// 4x CPU throttling).
const showCurrentPage = () => {
const pane = document.getElementById('starlight__sidebar');
if (!pane) return;
const reveal = () => {
const current = pane.querySelector<HTMLElement>('[aria-current="page"]');
if (!current) return;
const box = pane.getBoundingClientRect();
const link = current.getBoundingClientRect();
if (link.top >= box.top && link.bottom <= box.bottom) return;
pane.scrollTop += link.top - box.top - pane.clientHeight / 3;
const observer = new IntersectionObserver(
([entry]) => {
observer.disconnect();
const box = entry.rootBounds;
const link = entry.boundingClientRect;
if (!box || !box.height || (link.top >= box.top && link.bottom <= box.bottom)) return;
pane.scrollTop += link.top - box.top - box.height / 3;
},
{ root: pane },
);
observer.observe(current);
};
reveal();
pane.addEventListener('toggle', (event) => {