@@ -23,16 +23,19 @@ export class NavigationPlugin implements Plugin {
2323 }
2424
2525 /**
26- * If the client is initialized after the window has fired a load event,
27- * invoke the callback method directly to trigger the record event .
28- * Otherwise, keep the original implementation to add callback method to eventListener.
29- * However, if the page finishes loading right before adding addEventListener, we still cannot provide data
26+ * loadEventEnd is populated as 0 if the web page has not loaded completely, even though LOAD has been fired.
27+ * As a result, if loadEventEnd is populated, we can ignore eventListener and record the data directly .
28+ * On the other hand, if not, we have to use eventListener to initializes PerformanceObserver .
29+ * PerformanceObserver will act as a second check for the final load timings.
3030 */
3131 load ( context : PluginContext ) : void {
3232 this . recordEvent = context . record ;
3333 if ( this . enabled ) {
3434 if ( this . hasTheWindowLoadEventFired ( ) ) {
35- this . eventListener ( ) ;
35+ const navData = window . performance . getEntriesByType (
36+ NAVIGATION
37+ ) [ 0 ] as PerformanceNavigationTiming ;
38+ this . performanceNavigationEventHandlerTimingLevel2 ( navData ) ;
3639 } else {
3740 window . addEventListener ( LOAD , this . eventListener ) ;
3841 }
@@ -94,9 +97,18 @@ export class NavigationPlugin implements Plugin {
9497 if ( performance . getEntriesByType ( NAVIGATION ) . length === 0 ) {
9598 this . performanceNavigationEventHandlerTimingLevel1 ( ) ;
9699 } else {
97- this . performanceNavigationEventHandlerTimingLevel2 (
98- performance . getEntriesByType ( NAVIGATION ) [ 0 ]
99- ) ;
100+ const navigationObserver = new PerformanceObserver ( ( list ) => {
101+ list . getEntries ( ) . forEach ( ( event ) => {
102+ if ( event . entryType === NAVIGATION ) {
103+ this . performanceNavigationEventHandlerTimingLevel2 (
104+ event
105+ ) ;
106+ }
107+ } ) ;
108+ } ) ;
109+ navigationObserver . observe ( {
110+ entryTypes : [ NAVIGATION ]
111+ } ) ;
100112 }
101113 } ;
102114
0 commit comments