Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defer CLS logic until after onFCP callback #297

Merged
merged 2 commits into from
Jan 9, 2023
Merged

Conversation

philipwalton
Copy link
Member

This PR fixes #180 by removing some of the logic that would listen and keep track of whether or not FCP had fired, and instead just wraps all of the onCLS() logic inside of an onFCP() call (and ensuring the logic only runs the first time onFCP() is invoked).

The "ensuring the logic only runs the first time onFCP() is invoked" bit is accomplished by introducing a new runOnce() utility function, and I took this opportunity to refactor some of the other code to make use of that. The end result was a little bit of byte savings, so it was worth it.

Also, #180 caught a bug in the library where, if the library was lazy-loaded, CLS wouldn't properly be reported when reportAllChanges was true. I realized there were no tests to ensure things work when the library is lazy-loaded, so I added some, which also required refactoring some of the testing logic.

Lastly, this PR also updates a bit of the formatting style (continuing what was done in #288) since some of that was auto-formatting unrelated files anyway (not sure why, but seems to be fine now).

Copy link
Member

@tunetheweb tunetheweb left a comment

Choose a reason for hiding this comment

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

LGTM but have some comments (mostly for my own curiosity).

src/onCLS.ts Show resolved Hide resolved
src/onFID.ts Show resolved Hide resolved
test/views/layout.njk Outdated Show resolved Hide resolved
test/views/layout.njk Outdated Show resolved Hide resolved
@philipwalton philipwalton merged commit e5e4a6d into main Jan 9, 2023
@philipwalton philipwalton deleted the late-execution branch January 9, 2023 22:05
@Kobe
Copy link

Kobe commented Jan 31, 2023

@philipwalton @tunetheweb does this adjustment have an impact on concrete CLS numbers?

@philipwalton
Copy link
Member Author

It does not, other than cases affected by the bug outlined in this comment: #180 (comment)

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.

Reporting CLS and LCP in a lab environment using Playwright is flakey / unreliable
3 participants