@@ -40,7 +40,15 @@ logs.setGlobalLoggerProvider(loggerProvider);
4040registerInstrumentations ( {
4141 instrumentations : [ new BrowserNavigationInstrumentation ( {
4242 enabled : true ,
43- useNavigationApiIfAvailable : true
43+ useNavigationApiIfAvailable : true ,
44+ applyCustomLogRecordData : ( logRecord ) => {
45+ // Add custom attributes to navigation log records
46+ if ( ! logRecord . attributes ) {
47+ logRecord . attributes = { } ;
48+ }
49+ logRecord . attributes [ 'app.name' ] = 'navigation-example' ;
50+ logRecord . attributes [ 'app.version' ] = '1.0.0' ;
51+ }
4452 } ) ] ,
4553} ) ;
4654
@@ -85,16 +93,53 @@ function testHashChange() {
8593 hashCounter ++ ;
8694 const newHash = `section-${ hashCounter } ` ;
8795 location . hash = newHash ;
96+
8897 document . getElementById ( 'hash-content' ) . innerHTML =
89- `<p>Hash changed to: #${ newHash } </p><p>Check console for navigation events!</p>` ;
98+ `<div class="nav-result">
99+ <h4>🔗 Hash Navigation</h4>
100+ <p><strong>New Hash:</strong> <code>#${ newHash } </code></p>
101+ <p><strong>Method:</strong> location.hash assignment</p>
102+ <p><strong>Expected Instrumentation:</strong></p>
103+ <ul>
104+ <li>✓ same_document: true</li>
105+ <li>✓ hash_change: true</li>
106+ <li>✓ type: traverse or push</li>
107+ </ul>
108+ <p class="console-note">📊 Check console for navigation events!</p>
109+ </div>` ;
90110}
91111
92112// Back button functionality
93113function goBack ( ) {
94114 if ( window . history . length > 1 ) {
115+
95116 window . history . back ( ) ;
117+
118+ // Update content to show back navigation happened
119+ setTimeout ( ( ) => {
120+ const contentDiv = document . getElementById ( 'content' ) || document . body ;
121+ const backIndicator = document . createElement ( 'div' ) ;
122+ backIndicator . className = 'nav-result' ;
123+ backIndicator . innerHTML =
124+ `<div class="nav-result">
125+ <h4>⬅️ Back Navigation</h4>
126+ <p><strong>Method:</strong> history.back()</p>
127+ <p><strong>Current URL:</strong> <code>${ location . href } </code></p>
128+ <p><strong>Expected Instrumentation:</strong></p>
129+ <ul>
130+ <li>✓ same_document: true</li>
131+ <li>✓ hash_change: depends on URL change</li>
132+ <li>✓ type: traverse</li>
133+ </ul>
134+ <p class="console-note">📊 Check console for navigation events!</p>
135+ </div>` ;
136+ contentDiv . appendChild ( backIndicator ) ;
137+
138+ // Remove the indicator after 5 seconds
139+ setTimeout ( ( ) => backIndicator . remove ( ) , 5000 ) ;
140+ } , 100 ) ;
96141 } else {
97- console . log ( 'No history to go back to' ) ;
142+ alert ( 'No history to go back to! ' ) ;
98143 }
99144}
100145
@@ -112,53 +157,46 @@ function testNavigationApiHash() {
112157function testNavigationApiRoute ( route , navigationType ) {
113158 if ( 'navigation' in window ) {
114159 try {
115- // Add a navigate event listener to debug the event properties
116- const debugHandler = ( event ) => {
117- console . log ( `${ navigationType } - Navigation API event details:` , {
118- destination : event . destination ,
119- sameDocument : event . destination ?. sameDocument ,
120- hashChange : event . hashChange ,
121- url : event . destination ?. url ,
122- canIntercept : event . canIntercept ,
123- userInitiated : event . userInitiated
124- } ) ;
125-
126- // Don't prevent the navigation - let it proceed
127- // event.preventDefault(); // DON'T do this
128- } ;
129- window . navigation . addEventListener ( 'navigate' , debugHandler , { once : true } ) ;
160+
130161
131162 // Use Navigation API navigate method
132163 window . navigation . navigate ( route ) ;
133164
134- // Update content to show Navigation API usage
165+ // Update content after navigation
135166 setTimeout ( ( ) => {
136- document . getElementById ( 'content' ) . innerHTML =
137- `<h2>${ navigationType } Test</h2>
138- <p>Used navigation.navigate() to navigate to: <code>${ route } </code></p>
139- <p>Current URL: <code>${ window . location . href } </code></p>
140- <p>Check console for navigation events and sameDocument property!</p>` ;
167+ document . getElementById ( 'nav-api-content' ) . innerHTML =
168+ `<div class="nav-result">
169+ <h4>✅ ${ navigationType } Completed</h4>
170+ <p><strong>Target:</strong> <code>${ route } </code></p>
171+ <p><strong>Current URL:</strong> <code>${ window . location . href } </code></p>
172+ <p><strong>Navigation API:</strong> Supported ✓</p>
173+ <p class="console-note">📊 Check console for detailed navigation events and instrumentation data!</p>
174+ </div>` ;
141175 } , 100 ) ;
142-
176+
143177 } catch ( error ) {
144- console . error ( `Navigation API ${ navigationType } failed :` , error ) ;
178+ console . error ( `❌ Navigation API error for ${ navigationType } :` , error ) ;
145179 // Fallback to history API
146- const fallbackRoute = `/navigation/fallback-${ navApiCounter } ` ;
147- navigateTo ( fallbackRoute ) ;
148- document . getElementById ( 'content' ) . innerHTML =
149- `<h2>${ navigationType } Fallback</h2>
150- <p>Navigation API not supported, used history.pushState() instead</p>
151- <p>Navigated to: <code>${ fallbackRoute } </code></p>` ;
180+ const fallbackRoute = route . startsWith ( '#' ) ? route : `?fallback=${ Date . now ( ) } ` ;
181+ history . pushState ( { } , '' , fallbackRoute ) ;
182+ document . getElementById ( 'nav-api-content' ) . innerHTML =
183+ `<div class="nav-result error">
184+ <h4>⚠️ Navigation API Failed</h4>
185+ <p><strong>Fallback used:</strong> <code>${ fallbackRoute } </code></p>
186+ <p><strong>Error:</strong> ${ error . message } </p>
187+ </div>` ;
152188 }
153189 } else {
154- console . log ( `Navigation API not supported - ${ navigationType } ` ) ;
155- // Fallback to history API
156- const fallbackRoute = `/navigation/no-nav-api-${ navApiCounter } ` ;
157- navigateTo ( fallbackRoute ) ;
158- document . getElementById ( 'content' ) . innerHTML =
159- `<h2>No Navigation API Support - ${ navigationType } </h2>
160- <p>Navigation API not available, used history.pushState() instead</p>
161- <p>Navigated to: <code>${ fallbackRoute } </code></p>` ;
190+ // Fallback for browsers without Navigation API
191+ const fallbackRoute = route . startsWith ( '#' ) ? route : `?fallback=${ Date . now ( ) } ` ;
192+ history . pushState ( { } , '' , fallbackRoute ) ;
193+ document . getElementById ( 'nav-api-content' ) . innerHTML =
194+ `<div class="nav-result fallback">
195+ <h4>📱 Navigation API Not Available</h4>
196+ <p><strong>Fallback used:</strong> <code>${ fallbackRoute } </code></p>
197+ <p><strong>Method:</strong> history.pushState()</p>
198+ <p class="console-note">📊 Check console for instrumentation data!</p>
199+ </div>` ;
162200 }
163201}
164202
@@ -171,8 +209,5 @@ const loadTimeSetup = () => {
171209 document . getElementById ( 'hashChangeBtn' ) . addEventListener ( 'click' , testHashChange ) ;
172210 document . getElementById ( 'backBtn' ) . addEventListener ( 'click' , goBack ) ;
173211 document . getElementById ( 'navApiHashBtn' ) . addEventListener ( 'click' , testNavigationApiHash ) ;
174-
175- console . log ( 'Browser Navigation Instrumentation Example loaded!' ) ;
176- console . log ( 'Try clicking the buttons to see navigation events in the console.' ) ;
177212} ;
178213window . addEventListener ( 'load' , loadTimeSetup ) ;
0 commit comments