Fix race condition in navigation (and lifecycle events) #943
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.
TLDR: Skip the initial blank page navigation/lifecycle event if we want to navigate to a non-blank page so that we can wait for the navigation to complete. This way, we can receive custom metrics like Web Vitals. Custom metrics would be randomly skipped without this.
In order to make sure we receive custom metrics like Web Vitals, it is necessary to skip the initial blank page navigation event when navigating to a non-blank page. By doing this, we can wait for the navigation to complete fully. Without skipping this event, custom metrics could be randomly skipped.
Before this fix, this test's Web Vital metrics were flaky. Sometimes they show up, and sometimes, they don't.
I'll go ahead and explain the problem using the provided script.
The issue of inconsistency arose because we were removing the Web Vital bindings using
page.Close
before the navigation had finished. The navigation process was prematurely interrupted due to the initial blank page lifecycle events. As a result, we were not able to receive all the events from the Web Vitals library since we didn't wait for the navigation to complete.This pull request resolves the problem of unreliable behavior in emitting Web Vitals (or other custom metrics). However, it doesn't address the issue of displaying all Web Vital reports. According to Google's Web Vital documentation, most metrics are meant to be reported only occasionally.
P.S.: I attempted to create a test for this behavior, but I was unable to replicate it. Please let me know if you have any ideas. The existing tests locally pass!
Tip: I noticed that reloading the page just before closing it leads to more emitted metrics. Another pull request could tackle this aspect. Update: See #949.
Updates: #914.