Skip to content

Wait for prerendered pages to be activated before running Optimization Detective - #2708

Draft
sarthak-19 wants to merge 2 commits into
WordPress:trunkfrom
sarthak-19:fix/optimization-detective-prerendered-state
Draft

sarthak-19 wants to merge 2 commits into
WordPress:trunkfrom
sarthak-19:fix/optimization-detective-prerendered-state

Conversation

@sarthak-19

Copy link
Copy Markdown
Contributor

Summary

Fixes #1915.

detect.js did not explicitly account for the document being in a prerendered state (e.g. via Speculative Loading). In practice, detection mostly happened to wait anyway, since the intersection-observer callback and onLCP both tend not to fire until the page is shown, and extension loading/initialization currently runs after those waits. But that guarantee was incidental, not an intentional one, and depended on detect.js staying ordered a particular way.

This adds an explicit document.prerendering check via a waitForPrerenderingToEnd() helper, awaited early in detect() right after the existing cheap synchronous abort checks and before the resize-listener setup, the web-vitals import, and extension loading. If the document is not prerendering, it resolves immediately with no behavior change. If it is prerendering, it waits for the prerenderingchange event (added via document.addEventListener, since the event is dispatched on the document rather than the window) and, if available, a subsequent idle callback, before detection proceeds.

Test plan

  • npm run tsc passes.
  • npm run lint-js / npm run format-js pass on the changed file (via lint-staged on commit).
  • Manually verify via chrome://net-internals or a speculation-rules prerendered link that detection now explicitly waits on prerenderingchange rather than only incidentally via onLCP/intersection timing.

…n Detective

Add an explicit document.prerendering check in detect.js so that URL Metric
detection and extension loading/initialization wait for the page to be
shown (plus an idle callback) before proceeding, instead of relying on the
incidental timing of the intersection observer and onLCP callbacks.
@github-actions github-actions Bot added the [Plugin] Optimization Detective [slug: optimization-detective] label Oct 12, 2026
Replace Array<any>, ...*, and Record<string, any> JSDoc types with
Array<unknown>, ...unknown, and a localized Record<string, unknown> cast
inside recursiveFreeze(), satisfying the jsdoc/reject-any-type lint rule
without changing behavior.

This branch has not been deployed

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

Labels

[Plugin] Optimization Detective [slug: optimization-detective]

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Optimization Detective is not accounting for the prerendered state (with Speculative Loading)

1 participant