@@ -181,8 +181,9 @@ function testNavigationApiHash() {
181181}
182182
183183function testNavigationApiRoute ( route , navigationType ) {
184- if ( 'navigation' in window ) {
185- try {
184+ try {
185+ // Check if Navigation API is available
186+ if ( 'navigation' in window ) {
186187 // Log current navigation.activation.navigationType before navigation
187188 console . log ( '🧭 Before Navigation API navigate():' , {
188189 currentNavigationType : window . navigation . activation ?. navigationType ,
@@ -204,50 +205,60 @@ function testNavigationApiRoute(route, navigationType) {
204205 } ,
205206 } ) ;
206207 } , 50 ) ;
208+ } else {
209+ // Fallback to history API for browsers without Navigation API
210+ console . log ( '🧭 Navigation API not available, using history API fallback' ) ;
211+ if ( navigationType === 'replace' ) {
212+ history . replaceState ( { } , '' , route ) ;
213+ } else {
214+ history . pushState ( { } , '' , route ) ;
215+ }
216+ }
207217
208- // Update content after navigation
209- setTimeout ( ( ) => {
210- const contentElement = document . getElementById ( 'nav-api-content' ) ;
211-
212- // Create elements safely to avoid XSS
213- const resultDiv = document . createElement ( 'div' ) ;
214- resultDiv . className = 'nav-result' ;
215-
216- const title = document . createElement ( 'h4' ) ;
217- title . textContent = `✅ ${ navigationType } Completed` ;
218-
219- const targetPara = document . createElement ( 'p' ) ;
220- targetPara . innerHTML = '<strong>Target:</strong> <code></code>' ;
221- targetPara . querySelector ( 'code' ) . textContent = route ; // Safe text assignment
222-
223- const urlPara = document . createElement ( 'p' ) ;
224- urlPara . innerHTML = '<strong>Current URL:</strong> <code></code>' ;
225- urlPara . querySelector ( 'code' ) . textContent = window . location . href ; // Safe text assignment
226-
227- const apiPara = document . createElement ( 'p' ) ;
228- apiPara . innerHTML = '<strong>Navigation API:</strong> Supported ✓' ;
229-
230- const button = document . createElement ( 'button' ) ;
231- button . id = 'navApiHashBtn' ;
232- button . style . display = 'none' ;
233- button . textContent = 'Nav API: Hash' ;
234-
235- const consoleNote = document . createElement ( 'p' ) ;
236- consoleNote . className = 'console-note' ;
237- consoleNote . textContent =
238- '📊 Check console for detailed navigation events and instrumentation data!' ;
239-
240- resultDiv . appendChild ( title ) ;
241- resultDiv . appendChild ( targetPara ) ;
242- resultDiv . appendChild ( urlPara ) ;
243- resultDiv . appendChild ( apiPara ) ;
244- resultDiv . appendChild ( button ) ;
245- resultDiv . appendChild ( consoleNote ) ;
246-
247- contentElement . innerHTML = '' ; // Clear existing content
248- contentElement . appendChild ( resultDiv ) ;
249- } , 100 ) ;
250- } catch ( error ) {
218+ // Update content after navigation
219+ setTimeout ( ( ) => {
220+ const contentElement = document . getElementById ( 'nav-api-content' ) ;
221+
222+ // Create elements safely to avoid XSS
223+ const resultDiv = document . createElement ( 'div' ) ;
224+ resultDiv . className = 'nav-result' ;
225+
226+ const title = document . createElement ( 'h4' ) ;
227+ title . textContent = `✅ ${ navigationType } Completed` ;
228+
229+ const targetPara = document . createElement ( 'p' ) ;
230+ targetPara . innerHTML = '<strong>Target:</strong> <code></code>' ;
231+ targetPara . querySelector ( 'code' ) . textContent = route ; // Safe text assignment
232+
233+ const urlPara = document . createElement ( 'p' ) ;
234+ urlPara . innerHTML = '<strong>Current URL:</strong> <code></code>' ;
235+ urlPara . querySelector ( 'code' ) . textContent = window . location . href ; // Safe text assignment
236+
237+ const apiPara = document . createElement ( 'p' ) ;
238+ apiPara . innerHTML = '<strong>Navigation API:</strong> ' +
239+ ( 'navigation' in window ? 'Supported ✓' : 'Not Available ❌' ) ;
240+
241+ const button = document . createElement ( 'button' ) ;
242+ button . id = 'navApiHashBtn' ;
243+ button . style . display = 'none' ;
244+ button . textContent = 'Nav API: Hash' ;
245+
246+ const consoleNote = document . createElement ( 'p' ) ;
247+ consoleNote . className = 'console-note' ;
248+ consoleNote . textContent =
249+ '📊 Check console for detailed navigation events and instrumentation data!' ;
250+
251+ resultDiv . appendChild ( title ) ;
252+ resultDiv . appendChild ( targetPara ) ;
253+ resultDiv . appendChild ( urlPara ) ;
254+ resultDiv . appendChild ( apiPara ) ;
255+ resultDiv . appendChild ( button ) ;
256+ resultDiv . appendChild ( consoleNote ) ;
257+
258+ contentElement . innerHTML = '' ; // Clear existing content
259+ contentElement . appendChild ( resultDiv ) ;
260+ } , 100 ) ;
261+ } catch ( error ) {
251262 console . error ( `❌ Navigation API error for ${ navigationType } :` , error ) ;
252263 // Fallback to history API
253264 const fallbackRoute = route . startsWith ( '#' )
@@ -279,40 +290,6 @@ function testNavigationApiRoute(route, navigationType) {
279290
280291 contentElement . innerHTML = '' ;
281292 contentElement . appendChild ( errorDiv ) ;
282- }
283- } else {
284- // Fallback for browsers without Navigation API
285- const fallbackRoute = route . startsWith ( '#' )
286- ? route
287- : `?fallback=${ Date . now ( ) } ` ;
288- history . pushState ( { } , '' , fallbackRoute ) ;
289-
290- // Create fallback content safely
291- const contentElement = document . getElementById ( 'nav-api-content' ) ;
292- const fallbackDiv = document . createElement ( 'div' ) ;
293- fallbackDiv . className = 'nav-result fallback' ;
294-
295- const title = document . createElement ( 'h4' ) ;
296- title . textContent = '📱 Navigation API Not Available' ;
297-
298- const fallbackPara = document . createElement ( 'p' ) ;
299- fallbackPara . innerHTML = '<strong>Fallback used:</strong> <code></code>' ;
300- fallbackPara . querySelector ( 'code' ) . textContent = fallbackRoute ;
301-
302- const methodPara = document . createElement ( 'p' ) ;
303- methodPara . innerHTML = '<strong>Method:</strong> history.pushState()' ;
304-
305- const consoleNote = document . createElement ( 'p' ) ;
306- consoleNote . className = 'console-note' ;
307- consoleNote . textContent = '📊 Check console for instrumentation data!' ;
308-
309- fallbackDiv . appendChild ( title ) ;
310- fallbackDiv . appendChild ( fallbackPara ) ;
311- fallbackDiv . appendChild ( methodPara ) ;
312- fallbackDiv . appendChild ( consoleNote ) ;
313-
314- contentElement . innerHTML = '' ;
315- contentElement . appendChild ( fallbackDiv ) ;
316293 }
317294}
318295
0 commit comments