File tree Expand file tree Collapse file tree 2 files changed +51
-1
lines changed Expand file tree Collapse file tree 2 files changed +51
-1
lines changed Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ import { z } from 'zod'
7
7
import {
8
8
RouterProvider ,
9
9
createBrowserHistory ,
10
+ createMemoryHistory ,
10
11
createRootRoute ,
11
12
createRoute ,
12
13
createRouter ,
@@ -563,4 +564,33 @@ describe('useBlocker', () => {
563
564
564
565
expect ( window . location . pathname ) . toBe ( '/non-existent' )
565
566
} )
567
+
568
+ test ( 'navigate function should handle external URLs with ignoreBlocker' , async ( ) => {
569
+ const rootRoute = createRootRoute ( )
570
+ const indexRoute = createRoute ( {
571
+ getParentRoute : ( ) => rootRoute ,
572
+ path : '/' ,
573
+ component : ( ) => < div > Home</ div > ,
574
+ } )
575
+
576
+ const router = createRouter ( {
577
+ routeTree : rootRoute . addChildren ( [ indexRoute ] ) ,
578
+ history : createMemoryHistory ( {
579
+ initialEntries : [ '/' ] ,
580
+ } ) ,
581
+ } )
582
+
583
+ await expect (
584
+ router . navigate ( {
585
+ to : 'https://example.com' ,
586
+ ignoreBlocker : true ,
587
+ } ) ,
588
+ ) . resolves . toBeUndefined ( )
589
+
590
+ await expect (
591
+ router . navigate ( {
592
+ to : 'https://example.com' ,
593
+ } ) ,
594
+ ) . resolves . toBeUndefined ( )
595
+ } )
566
596
} )
Original file line number Diff line number Diff line change @@ -1778,7 +1778,7 @@ export class RouterCore<
1778
1778
} )
1779
1779
}
1780
1780
1781
- navigate : NavigateFn = ( { to, reloadDocument, href, ...rest } ) => {
1781
+ navigate : NavigateFn = async ( { to, reloadDocument, href, ...rest } ) => {
1782
1782
if ( ! reloadDocument && href ) {
1783
1783
try {
1784
1784
new URL ( `${ href } ` )
@@ -1791,6 +1791,26 @@ export class RouterCore<
1791
1791
const location = this . buildLocation ( { to, ...rest } as any )
1792
1792
href = this . history . createHref ( location . href )
1793
1793
}
1794
+
1795
+ // Check blockers for external URLs unless ignoreBlocker is true
1796
+ if ( ! rest . ignoreBlocker ) {
1797
+ // Cast to access internal getBlockers method
1798
+ const historyWithBlockers = this . history as any
1799
+ const blockers = historyWithBlockers . getBlockers ?.( ) ?? [ ]
1800
+ for ( const blocker of blockers ) {
1801
+ if ( blocker ?. blockerFn ) {
1802
+ const shouldBlock = await blocker . blockerFn ( {
1803
+ currentLocation : this . latestLocation ,
1804
+ nextLocation : this . latestLocation , // External URLs don't have a next location in our router
1805
+ action : 'PUSH' ,
1806
+ } )
1807
+ if ( shouldBlock ) {
1808
+ return Promise . resolve ( )
1809
+ }
1810
+ }
1811
+ }
1812
+ }
1813
+
1794
1814
if ( rest . replace ) {
1795
1815
window . location . replace ( href )
1796
1816
} else {
You can’t perform that action at this time.
0 commit comments