⚡ bolt: cache document dimensions to avoid layout thrashing#20
⚡ bolt: cache document dimensions to avoid layout thrashing#20
Conversation
…ectionnav caching these dimensions avoids forced synchronous layouts (layout thrashing) during scroll events and intersection observer callbacks, improving scrolling performance and smoothness. invalidate the cache via resizeobserver and window resize events.
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
Deploying personal-website with
|
| Latest commit: |
baeca0d
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://1d469e3c.personal-website-5ns.pages.dev |
| Branch Preview URL: | https://bolt-cache-dimensions-969281.personal-website-5ns.pages.dev |
There was a problem hiding this comment.
Pull request overview
This PR aims to improve scroll-related performance by caching layout-dependent document dimensions for the scroll progress bar and section navigation logic, reducing repeated layout reads during scroll/observer callbacks.
Changes:
- Add
cachedDocHeight/cachedWindowHeightcaches toScrollProgressandSectionNav, and switch computations to use cached values. - Recompute cached dimensions on debounced
window.resize. - Add a global
ResizeObserverintended to refresh cached dimensions when layout/content changes.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| script.js | Introduces cached dimension reads for scroll progress + section nav and adds resize/observer-based invalidation. |
| .jules/bolt.md | Documents the performance learning/action for caching dimensions with ResizeObserver. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
|
|
||
| ## 2024-05-20 - [Frontend Performance: Caching Dimensions with ResizeObserver] | ||
| **Learning:** Reading `document.documentElement.scrollHeight` and `window.innerHeight` frequently inside scroll event handlers (even with `requestAnimationFrame`) causes unnecessary synchronous layout calculations, harming performance and scrolling smoothness. | ||
| **Action:** Cache these dimensions in properties. Set up a global `ResizeObserver` on `document.documentElement` to automatically recalculate and update the cached values only when the layout height actually changes (e.g. from expanding content or lazy-loaded images). No newline at end of file |
| if (typeof ScrollProgress !== 'undefined') ScrollProgress.cacheDimensions(); | ||
| if (typeof SectionNav !== 'undefined') SectionNav.cacheDimensions(); | ||
| }); | ||
| layoutObserver.observe(document.documentElement); |
| const layoutObserver = new ResizeObserver(() => { | ||
| if (typeof ScrollProgress !== 'undefined') ScrollProgress.cacheDimensions(); | ||
| if (typeof SectionNav !== 'undefined') SectionNav.cacheDimensions(); | ||
| }); | ||
| layoutObserver.observe(document.documentElement); |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: baeca0d51e
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| const layoutObserver = new ResizeObserver(() => { | ||
| if (typeof ScrollProgress !== 'undefined') ScrollProgress.cacheDimensions(); | ||
| if (typeof SectionNav !== 'undefined') SectionNav.cacheDimensions(); |
There was a problem hiding this comment.
Refresh scroll progress when cached heights change
This observer handles the exact non-scroll cases mentioned in the comment, but it only updates cachedDocHeight/cachedWindowHeight. On this page, index.html lazy-loads several gallery images and PageLoad.hidePreloader() also removes DOM after load, so the document height can change while the user is stationary; because ScrollProgress.update() is never called here, the progress bar keeps the old percentage until the next scroll or resize and becomes visibly incorrect.
Useful? React with 👍 / 👎.
💡 what: cached
document.documentElement.scrollHeightandwindow.innerHeightin thescrollprogressandsectionnavcomponents to avoid querying the dom directly during scroll events and intersection observer callbacks. the cache is invalidated via aresizeobserveron the document element, and during window resize events.🎯 why: querying layout dimensions like
scrollHeightandinnerHeightinside scroll handlers (even within requestanimationframe) causes synchronous layout thrashing, which is a significant performance bottleneck. this change eliminates those reflows, ensuring smoother scrolling.📊 impact: eliminates synchronous layout thrashing related to scroll progress and section tracking, improving scroll smoothness and reducing cpu overhead during scrolling.
🔬 measurement: visually verify the scrolling experience and ensure the scroll progress bar and section tracking work properly. profile performance before and after to observe fewer layout recalculations.
PR created automatically by Jules for task 9692812995039132109 started by @dttdrv