Skip to content

Commit 125bf64

Browse files
committed
use the more supported 'scroll' event instead of 'scrollend'
assume 100% progress beyound 95%
1 parent c26bb44 commit 125bf64

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/components/track-reader-depth.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ export function ReaderDepthTracker() {
2929
// calculate the progress based on the scroll position
3030
const scrollPosition = window.scrollY;
3131
const totalHeight = document.documentElement.scrollHeight - window.innerHeight;
32-
const progress = Math.floor((scrollPosition / totalHeight) * 100);
32+
let progress = Math.floor((scrollPosition / totalHeight) * 100);
33+
// it's hard to trigger the 100% milestone, so we'll just assume beyond 95%
34+
if (progress > 95) {
35+
progress = 100;
36+
}
3337

3438
// find the biggest milestone that has not been reached yet
3539
const milestone = milestones.findLast(
@@ -51,11 +55,11 @@ export function ReaderDepthTracker() {
5155
sendProgressToPlausible(100);
5256
return () => {};
5357
}
54-
const debouncedTrackProgress = debounce(trackProgress, 50);
58+
const debouncedTrackProgress = debounce(trackProgress, 20);
5559

56-
window.addEventListener('scrollend', debouncedTrackProgress);
60+
window.addEventListener('scroll', debouncedTrackProgress);
5761
return () => {
58-
window.removeEventListener('scrollend', debouncedTrackProgress);
62+
window.removeEventListener('scroll', debouncedTrackProgress);
5963
};
6064
});
6165
// do not render anything

0 commit comments

Comments
 (0)