Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve page load performance #2155

Merged
merged 14 commits into from
Aug 5, 2024
Merged
5 changes: 5 additions & 0 deletions .changeset/bright-planets-accept.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/starlight": patch
---

Improves page load performance on slower devices
15 changes: 10 additions & 5 deletions packages/starlight/components/TableOfContents/starlight-toc.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PAGE_TITLE_ID } from '../../constants';

export class StarlightTOC extends HTMLElement {
private _current = this.querySelector('a[aria-current="true"]') as HTMLAnchorElement | null;
private _current = this.querySelector<HTMLAnchorElement>('a[aria-current="true"]');
private minH = parseInt(this.dataset.minH || '2', 10);
private maxH = parseInt(this.dataset.maxH || '3', 10);

Expand All @@ -12,9 +12,15 @@ export class StarlightTOC extends HTMLElement {
this._current = link;
}

private onIdle = (cb: IdleRequestCallback) =>
(window.requestIdleCallback || ((cb) => setTimeout(cb, 1)))(cb);

constructor() {
super();
this.onIdle(() => this.init());
}

private init = (): void => {
/** All the links in the table of contents. */
const links = [...this.querySelectorAll('a')];

Expand Down Expand Up @@ -73,21 +79,20 @@ export class StarlightTOC extends HTMLElement {

let observer: IntersectionObserver | undefined;
const observe = () => {
if (observer) observer.disconnect();
if (observer) return;
observer = new IntersectionObserver(setCurrent, { rootMargin: this.getRootMargin() });
toObserve.forEach((h) => observer!.observe(h));
};
observe();

const onIdle = window.requestIdleCallback || ((cb) => setTimeout(cb, 1));
let timeout: NodeJS.Timeout;
window.addEventListener('resize', () => {
// Disable intersection observer while window is resizing.
if (observer) observer.disconnect();
clearTimeout(timeout);
timeout = setTimeout(() => onIdle(observe), 200);
timeout = setTimeout(() => this.onIdle(observe), 200);
});
}
};

private getRootMargin(): `-${number}px 0% ${number}px` {
const navBarHeight = document.querySelector('header')?.getBoundingClientRect().height || 0;
Expand Down
2 changes: 1 addition & 1 deletion packages/starlight/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@
"@pagefind/default-ui": "^1.0.3",
"@types/hast": "^3.0.4",
"@types/mdast": "^4.0.4",
"astro-expressive-code": "^0.35.3",
"astro-expressive-code": "^0.35.4",
"bcp-47": "^2.1.0",
"hast-util-from-html": "^2.0.1",
"hast-util-select": "^6.0.2",
Expand Down
50 changes: 25 additions & 25 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading