@@ -151,18 +151,21 @@ export function useRouter() {
151
151
} ,
152
152
) => {
153
153
const url = new URL ( to , window . location . href ) ;
154
- const newPath = url . pathname !== window . location . pathname ;
155
- window . history . pushState (
156
- {
157
- ...window . history . state ,
158
- waku_new_path : newPath ,
159
- } ,
160
- '' ,
161
- url ,
162
- ) ;
154
+ const currentPath = window . location . pathname ;
155
+ const newPath = url . pathname !== currentPath ;
163
156
await changeRoute ( parseRoute ( url ) , {
164
157
shouldScroll : options ?. scroll ?? newPath ,
165
158
} ) ;
159
+ if ( window . location . pathname === currentPath ) {
160
+ window . history . pushState (
161
+ {
162
+ ...window . history . state ,
163
+ waku_new_path : newPath ,
164
+ } ,
165
+ '' ,
166
+ url ,
167
+ ) ;
168
+ }
166
169
} ,
167
170
[ changeRoute ] ,
168
171
) ;
@@ -180,11 +183,14 @@ export function useRouter() {
180
183
} ,
181
184
) => {
182
185
const url = new URL ( to , window . location . href ) ;
183
- const newPath = url . pathname != = window . location . pathname ;
184
- window . history . replaceState ( window . history . state , '' , url ) ;
186
+ const currentPath = window . location . pathname ;
187
+ const newPath = url . pathname !== currentPath ;
185
188
await changeRoute ( parseRoute ( url ) , {
186
189
shouldScroll : options ?. scroll ?? newPath ,
187
190
} ) ;
191
+ if ( window . location . pathname === currentPath ) {
192
+ window . history . replaceState ( window . history . state , '' , url ) ;
193
+ }
188
194
} ,
189
195
[ changeRoute ] ,
190
196
) ;
@@ -331,19 +337,26 @@ export function Link({
331
337
const route = parseRoute ( url ) ;
332
338
prefetchRoute ( route ) ;
333
339
startTransitionFn ( async ( ) => {
334
- const newPath = url . pathname !== window . location . pathname ;
335
- window . history . pushState (
336
- {
337
- ...window . history . state ,
338
- waku_new_path : newPath ,
339
- } ,
340
- '' ,
341
- url ,
342
- ) ;
343
- await changeRoute ( route , {
344
- shouldScroll : scroll ?? newPath ,
345
- unstable_startTransition : startTransitionFn ,
346
- } ) ;
340
+ const currentPath = window . location . pathname ;
341
+ const newPath = url . pathname !== currentPath ;
342
+ try {
343
+ await changeRoute ( route , {
344
+ shouldScroll : scroll ?? newPath ,
345
+ unstable_startTransition : startTransitionFn ,
346
+ } ) ;
347
+ } finally {
348
+ if ( window . location . pathname === currentPath ) {
349
+ // Update history if it wasn't already updated
350
+ window . history . pushState (
351
+ {
352
+ ...window . history . state ,
353
+ waku_new_path : newPath ,
354
+ } ,
355
+ '' ,
356
+ url ,
357
+ ) ;
358
+ }
359
+ }
347
360
} ) ;
348
361
}
349
362
} ;
@@ -474,15 +487,8 @@ const Redirect = ({
474
487
window . location . replace ( to ) ;
475
488
return ;
476
489
}
477
- const newPath = url . pathname !== window . location . pathname ;
478
- window . history . pushState (
479
- {
480
- ...window . history . state ,
481
- waku_new_path : newPath ,
482
- } ,
483
- '' ,
484
- url ,
485
- ) ;
490
+ const currentPath = window . location . pathname ;
491
+ const newPath = url . pathname !== currentPath ;
486
492
changeRoute ( parseRoute ( url ) , { shouldScroll : newPath } )
487
493
. then ( ( ) => {
488
494
// FIXME: As we understand it, we should have a proper solution.
@@ -492,6 +498,18 @@ const Redirect = ({
492
498
} )
493
499
. catch ( ( err ) => {
494
500
console . log ( 'Error while navigating to redirect:' , err ) ;
501
+ } )
502
+ . finally ( ( ) => {
503
+ if ( window . location . pathname === currentPath ) {
504
+ window . history . replaceState (
505
+ {
506
+ ...window . history . state ,
507
+ waku_new_path : newPath ,
508
+ } ,
509
+ '' ,
510
+ url ,
511
+ ) ;
512
+ }
495
513
} ) ;
496
514
} , [ error , to , reset , changeRoute ] ) ;
497
515
return null ;
@@ -613,6 +631,7 @@ const handleScroll = () => {
613
631
const InnerRouter = ( { initialRoute } : { initialRoute : RouteProps } ) => {
614
632
const elementsPromise = useElementsPromise ( ) ;
615
633
const [ has404 , setHas404 ] = useState ( false ) ;
634
+ const requestedRouteRef = useRef < RouteProps > ( initialRoute ) ;
616
635
const staticPathSetRef = useRef ( new Set < string > ( ) ) ;
617
636
const cachedIdSetRef = useRef ( new Set < string > ( ) ) ;
618
637
useEffect ( ( ) => {
@@ -679,11 +698,9 @@ const InnerRouter = ({ initialRoute }: { initialRoute: RouteProps }) => {
679
698
elements ;
680
699
if ( routeData ) {
681
700
const [ path , query ] = routeData as [ string , string ] ;
682
- // FIXME this check here seems ad-hoc (less readable code)
683
701
if (
684
- window . location . pathname !== path ||
685
- ( ! isStatic &&
686
- window . location . search . replace ( / ^ \? / , '' ) !== query )
702
+ requestedRouteRef . current . path !== path ||
703
+ ( ! isStatic && requestedRouteRef . current . query !== query )
687
704
) {
688
705
locationListeners . forEach ( ( listener ) =>
689
706
listener ( path , query ) ,
@@ -767,6 +784,7 @@ const InnerRouter = ({ initialRoute }: { initialRoute: RouteProps }) => {
767
784
const refetching = useRef < [ onFinish ?: ( ) => void ] | null > ( null ) ;
768
785
const changeRoute : ChangeRoute = useCallback (
769
786
async ( route , options ) => {
787
+ requestedRouteRef . current = route ;
770
788
executeListeners ( 'start' , route ) ;
771
789
const startTransitionFn =
772
790
options . unstable_startTransition || ( ( fn : TransitionFunction ) => fn ( ) ) ;
@@ -827,22 +845,25 @@ const InnerRouter = ({ initialRoute }: { initialRoute: RouteProps }) => {
827
845
url . pathname = path ;
828
846
url . search = query ;
829
847
url . hash = '' ;
830
- if ( path !== '/404' ) {
831
- window . history . pushState (
832
- {
833
- ...window . history . state ,
834
- waku_new_path : url . pathname !== window . location . pathname ,
835
- } ,
836
- '' ,
837
- url ,
838
- ) ;
839
- }
840
848
changeRoute ( parseRoute ( url ) , {
841
849
skipRefetch : true ,
842
850
shouldScroll : false ,
843
- } ) . catch ( ( err ) => {
844
- console . log ( 'Error while navigating to new route:' , err ) ;
845
- } ) ;
851
+ } )
852
+ . catch ( ( err ) => {
853
+ console . log ( 'Error while handling location listeners:' , err ) ;
854
+ } )
855
+ . finally ( ( ) => {
856
+ if ( path !== '/404' ) {
857
+ window . history . pushState (
858
+ {
859
+ ...window . history . state ,
860
+ waku_new_path : url . pathname !== window . location . pathname ,
861
+ } ,
862
+ '' ,
863
+ url ,
864
+ ) ;
865
+ }
866
+ } ) ;
846
867
} ;
847
868
if ( refetching . current ) {
848
869
refetching . current . push ( fn ) ;
0 commit comments