-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: load out of viewport sections after TTI (#2604)
## What's the purpose of this pull request? This PR aims to load the `out of viewport` sections after the TTI, enhancing interactivity and performance. The key changes include the addition of a new hook for Time To Interactive (TTI) measurement, updates to the `LazyLoadingSection` and `ViewportObserver` components to know when the browser isInteractive and load the other sections. This PR also improves debug. ## How to test it? You can see the sections loaded after the TTI in the Network tab of DevTools using the [preview link](https://starter-261g6dokj-vtex.vercel.app/). ### Starters Deploy Preview https://starter-261g6dokj-vtex.vercel.app/ - vtex-sites/starter.store#641 ### References https://web.dev/articles/tti?hl=pt-br https://web.dev/articles/tbt?hl=pt-br#how_does_tbt_relate_to_tti https://developer.mozilla.org/en-US/docs/Web/API/Window/requestIdleCallback https://caniuse.com/?search=requestIdleCallback https://caniuse.com/mdn-api_performanceobserver
- Loading branch information
1 parent
b692b17
commit 98f8109
Showing
3 changed files
with
93 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { useEffect, useState } from 'react' | ||
|
||
const TTI_TIMEOUT = 5000 // 5 seconds without long tasks as a criterion for Time To Interactive - https://web.dev/articles/tti | ||
export default function useTTI() { | ||
const [isInteractive, setIsInteractive] = useState(false) | ||
|
||
useEffect(() => { | ||
if ('PerformanceObserver' in window) { | ||
let lastTaskEnd = 0 | ||
|
||
const observer = new PerformanceObserver((list) => { | ||
for (const entry of list.getEntries()) { | ||
lastTaskEnd = entry.startTime + entry.duration | ||
} | ||
}) | ||
|
||
observer.observe({ type: 'longtask', buffered: true }) | ||
|
||
// Monitoring when TTI might have been reached | ||
const checkTTI = () => { | ||
const now = performance.now() | ||
if (now - lastTaskEnd >= TTI_TIMEOUT) { | ||
observer.disconnect() | ||
setIsInteractive(true) // Sets the state to true when TTI is estimated | ||
} else { | ||
requestIdleCallback(checkTTI) // Keeps checking while the browser is idle | ||
} | ||
} | ||
|
||
requestIdleCallback(checkTTI) | ||
} | ||
}, []) | ||
|
||
return { isInteractive } | ||
} |