Merged in fix/pageloadtime-fix (pull request #1248)

tracking: wait on document.readystate

* tracking: wait on document.readystate
This commit is contained in:
Linus Flood
2025-02-04 10:06:44 +00:00
parent 7db6c54beb
commit 683646a490

View File

@@ -49,9 +49,8 @@ export default function RouterTransition({
useEffect(() => {
if (!hasRun && !hasRunInitial.current) {
if (performance) {
const handleLoad = () => {
const entry = performance.getEntriesByType("navigation")[0]
if (entry) {
const trackingData = {
...pageData,
@@ -69,7 +68,14 @@ export default function RouterTransition({
})
}
}
// If the page is already loaded, execute immediately
if (document.readyState === "complete") {
handleLoad()
} else {
// Otherwise wait for the load event
window.addEventListener("load", handleLoad)
return () => window.removeEventListener("load", handleLoad)
}
hasRunInitial.current = true
setHasRun()
}