Skip to content

[Tracker] Use ResizeObserver instead of polling for document height#6174

Open
ukutaht wants to merge 5 commits intomasterfrom
use-resizeobserver
Open

[Tracker] Use ResizeObserver instead of polling for document height#6174
ukutaht wants to merge 5 commits intomasterfrom
use-resizeobserver

Conversation

@ukutaht
Copy link
Contributor

@ukutaht ukutaht commented Mar 18, 2026

Changes

Replaces document height polling with ResizeObserver to avoid forced reflows.

Fixes #5727

ResizeObserver is a fairly modern API. Should be OK for the main script since overall it doesn't have worse support than fetch-with-keepalive. The only gap I was able to find is Edge versions less than 79 do not support ResizeObserver while fetch support goes back longer on Edge. So we will lose pre-2020 Edge versions but they have negligible usage worldwide. It would be good to have some kind of feature matrix for tracker script and check against caniuse data when we update it.

Instead of keeping the old code as fallback for compat code, I've disabled engagement tracking completely in compat mode. Without fetch-with-keepalive engagement data isn't reliable anyways. But it should probably be checked with sites that actually use the compat mode.

Disabling engagements in compat mode also allowed me to remove some fallbacks in getCurrentScrollDepthPx function which are not relevant in modern browsers (window.scrollY and window.innerHeight are way better supported than fetch and ResizeObserver, the fallbacks were only relevant for IE).

Verification

On a local page with plausible installed, I profiled a scripted journey where page is loaded and scrolled after waiting a half a second. The old code produced 27 document reflows, with this PR there were 3 reflows. None of the remaining 3 were triggered by our script, they were all internal chrome events.

ukutaht added 3 commits March 18, 2026 13:59
window.scrollY and window.innerHeight are compositor-cached values
that never trigger layout recalculation, unlike el.scrollTop and
el.clientHeight. The fallbacks only existed for IE which is already
unsupported.
}).observe(document.documentElement)

document.addEventListener('scroll', function () {
currentDocumentHeight = getDocumentHeight()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to recompute document height on every scroll event. If the document height changes at any point, ResizeObserver will keep it updated.

// next 3 seconds. This makes sure dynamically loaded content is
// also accounted for.
var count = 0
var interval = setInterval(function () {
Copy link
Contributor Author

@ukutaht ukutaht Mar 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to poll the document for height changes during initial load. Any time DOM is loaded and updated, ResizeObserver will keep currentDocumentHeight variable in sync.

function getCurrentScrollDepthPx() {
var body = document.body || {}
var el = document.documentElement || {}
var viewportHeight = window.innerHeight || el.clientHeight || 0
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By removing IE support for engagement tracking, these fallbacks are not necessary since window.scrollY and window.innerHeight are well-supported:

https://caniuse.com/mdn-api_window_innerheight
https://caniuse.com/mdn-api_window_scrolly

@ukutaht ukutaht requested a review from RobertJoonas March 18, 2026 13:31
@github-actions
Copy link

Analyzed 1026 tracker script variants for size changes.
The following tables summarize the results, with comparison with the baseline version in parentheses.

Main variants:

Brotli Gzip Uncompressed
npm_package/plausible.js (new variant) 2357B 2693B 6444B
plausible-web.js 2178B (-37B / -1.7%) 2496B (-41B / -1.6%) 5936B (-154B / -2.5%)

Important legacy variants:

Brotli Gzip Uncompressed
plausible.js 1122B (-24B / -2.1%) 1300B (-40B / -3%) 2851B (-154B / -5.1%)
plausible.hash.js 1080B (-35B / -3.1%) 1264B (-41B / -3.1%) 2730B (-154B / -5.3%)
plausible.pageview-props.tagged-events.js 1658B (-29B / -1.7%) 1930B (-35B / -1.8%) 4382B (-154B / -3.4%)
plausible.file-downloads.hash.pageview-props.revenue.js 1509B (-33B / -2.1%) 1784B (-41B / -2.2%) 3778B (-154B / -3.9%)
plausible.compat.exclusions.file-downloads.outbound-links.pageview-props.revenue.tagged-events.js 2192B (-91B / -4%) 2602B (-99B / -3.7%) 5819B (-355B / -5.7%)

Summary:

Brotli Gzip Uncompressed
Largest variant (plausible.compat.exclusions.file-downloads.outbound-links.pageview-props.revenue.tagged-events.js) 2192B (-91B / -4%) 2602B (-99B / -3.7%) 5819B (-355B / -5.7%)
Max change (plausible.file-downloads.hash.outbound-links.pageview-props.revenue.tagged-events.js) 1932B (-19B / -1%) 2259B (-39B / -1.7%) 5062B (-154B / -3%)
Min change (plausible.compat.hash.local.manual.revenue.js) 853B (-93B / -9.8%) 1062B (-104B / -8.9%) 2004B (-355B / -15%)
Median change 1591B (-49B / -3.7%) 1887B (-48B / -3.7%) 4041B (-154B / -6.1%)

In total, 0 variants brotli size increased and 1025 variants brotli size decreased.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tracking script causes forced reflow of the page

1 participant