fix: use buffered observer in agent return path for LCP and CLS snippets#74
Merged
Conversation
Chrome does not expose largest-contentful-paint or layout-shift entries via performance.getEntriesByType() without an active PerformanceObserver. The previous sync return path silently returned "No LCP entries yet" in any automated context (Chrome DevTools MCP, CLI, Skills via CDP). Fix: make the IIFE async and collect buffered entries via a short-lived PerformanceObserver, resolving after a setTimeout(0) to ensure buffered callbacks have flushed. Affected snippets: LCP.js, CLS.js, LCP-Sub-Parts.js Chrome DevTools console is unaffected — it auto-awaits async IIFEs and displays the resolved value.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
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.
Finding
performance.getEntriesByType("largest-contentful-paint")and"layout-shift"always return an empty array in Chrome unless aPerformanceObserverhas been actively subscribed. This is a Chrome implementation detail: these entry types are observer-only and are not written to the shared performance timeline buffer.Impact
The "Synchronous return for agent" section at the bottom of these snippets was silently returning
{ status: "error", error: "No LCP entries yet" }in every automated context:mcp__chrome-devtools__evaluate_script)addInitScriptshim, but the root cause is in the snippets)Manual DevTools console usage was not affected because the preceding observer registration happens to populate a context-local buffer that the synchronous read can sometimes see on long-lived pages, but this was never reliable.
Verification
Fix
(() => {→(async () => {in the 3 affected snippets.getEntriesByTypewith a short-livedPerformanceObserverthat resolves aftersetTimeout(0)(guarantees all buffered microtasks have flushed).page.evaluate()(Playwright/CDP) is unaffected: it already awaits Promises.Affected snippets
LCP.jslargest-contentful-paintCLS.jslayout-shiftLCP-Sub-Parts.jslargest-contentful-paintTest plan
LCP.jsvia Chrome DevTools MCPevaluate_scripton a live page — result should be{ status: "ok", value: <ms>, ... }not{ status: "error" }.CLS.jsandLCP-Sub-Parts.js.LCP-Video-Candidate.jsis unaffected (it already used a proper observer pattern).