fix(web-vitals): polyfill Array.prototype.findLast for the attribution bundles - #4732
Draft
posthog[bot] wants to merge 1 commit into
Draft
fix(web-vitals): polyfill Array.prototype.findLast for the attribution bundles#4732posthog[bot] wants to merge 1 commit into
posthog[bot] wants to merge 1 commit into
Conversation
…n bundles web-vitals 6.x attribution calls Array.prototype.findLast in its LCP attribution path. That method is ES2023, and our browserslist range still admits browsers without it, so the lazily loaded attribution bundles threw `TypeError: ....findLast is not a function` and captured no web vitals. Adds a small side-effecting polyfill next to the existing Array.prototype.at one and imports it first in both attribution entrypoints, so it installs before web-vitals is evaluated. Also replaces the hand-maintained polyfill entries in the browser-common sideEffects list with a glob, so a future polyfill cannot be silently tree-shaken out of the bundles by being left off the list. Generated-By: PostHog Desktop Task-Id: 0a522273-96e7-474b-a615-f0eba20f465d
Contributor
Author
Contributor
Replay incident risk checkThis diff touches code involved in past incidents. This is a heads-up, not a verdict: read the matched sections of INCIDENTS.md and answer their review questions before merging. For a judgment on whether this diff has the same failure mode, run the |
Contributor
Contributor
|
Size Change: +2.54 kB (+0.01%) Total Size: 20.8 MB 📦 View Changed
ℹ️ View Unchanged
|
Contributor
posthog-js Compliance ReportDate: 2026-09-01 20:54:18 UTC ✅ All Tests Passed!26/26 tests passed Capture Tests✅ 26/26 tests passed View Details
|
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Problem
web-vitalsmoved from^5.3.0to^6.2.1. The 6.x attribution build callsArray.prototype.findLast(ES2023) in its LCP attribution path. Our browserslist range (> 0.5%, last 2 versions, Firefox ESR, not dead) still admits browsers without it, and Babel down-levels syntax only — it never polyfills prototype methods.Array.prototype.atneeded the same bespoke fix, and that helper still lives next door.Note
One correction to the report: the call is guarded by
lcpEntry.url, so the throw needs a resource/image LCP, not any LCP. A text LCP short-circuits beforefindLast. The failure is still routine on real pages — it just is not every pageview.Changes
array-find-last-polyfillimported first in both attribution entrypoints. Same shape as theatpolyfill beside it.web-vitals@6.2.1dist:findLastappears solely in the attribution build, so the lighter bundles pay nothing.sideEffectsnow globs*-polyfill.js/.mjsinstead of listing each file. The bare import only survives bundling because the file is on that list, so a third polyfill left off it would silently tree-shake away and reintroduce this exact bug with no failing test.Bespoke rather than
core-js, per the standing rule inrollup.config.mjsabout keeping these bundles small. Cost is ~79 bytes gzipped per attribution bundle;core-jswould add ~6KB to a 6-18KB bundle.Release info Sub-libraries affected
Libraries affected
Checklist
If releasing new changes
pnpm changesetto generate a changeset file🤖 Agent context
Autonomy: Fully autonomous
Claude Code (Opus 4.5), via a PostHog inbox report. Left unassigned — no human drove it.
Verified, not assumed. Rather than trust the report, I pulled
web-vitals@6.2.1and found the real call site, then reproduced and fixed the symptom against the built bundle in a VM withatandfindLastdeleted:Before/after on a simulated pre-97 browser
z.findLastmatches the minified identifier in the reported error.id=3is the last of two matching entries, confirming the semantics web-vitals depends on. Both bundles then evaluated cleanly and registered all four callbacks.Also ran the
sideEffectsglob through a real rebuild to confirm globs are honoured and neither polyfill got tree-shaken. Full browser unit suite green (138 suites, 5983 tests).Rejected:
useBuiltIns+core-js— the rollup plugin factory is shared across ~40 entrypoints, so it is not scopable without threading a per-entry flag, and'usage'cannot see prototype calls in minified third-party code anyway. Also rejected a shared polyfill barrel:browser-commonbuilds withbundle: false, so each file still needs its ownsideEffectsentry and the barrel would over-polyfill the lighter bundles.Worth a follow-up, out of scope here. Nothing in CI can catch the next one of these.
eslint-plugin-compatlints only our source, never a bumped dependency; thees-checkES5 job is a syntax parser, so a valid-ES5 call likeentries.findLast(f)is invisible to it, and it does not cover these bundles at all — the note inpackages/browser/CLAUDE.mdcalling it a polyfill canary is misleading on that point. A postbuild scan ofdist/*.jsfor prototype methods above the browserslist baseline would have caught bothatandfindLastat bump time, alongside the existingcheck-mangled-property-consistency.js. Relatedly, these bundles also referenceglobalThisandqueueMicrotask, both of which post-date the Babel targets inrollup.config.mjs— worth settling whether that claimed range is real.Created with PostHog Desktop from this inbox report.