Defer CLS logic until after onFCP callback #297
Merged
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.
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 anonFCP()
call (and ensuring the logic only runs the first timeonFCP()
is invoked).The "ensuring the logic only runs the first time
onFCP()
is invoked" bit is accomplished by introducing a newrunOnce()
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).