@@ -2,12 +2,7 @@ import { BROWSER, DEV } from 'esm-env';
2
2
import { isActive } from './helpers/is-active.js' ;
3
3
import { matchRoute } from './helpers/match-route.js' ;
4
4
import { preloadOnHover } from './helpers/preload-on-hover.js' ;
5
- import {
6
- constructPath ,
7
- join ,
8
- resolveRouteComponents ,
9
- wrapInViewTransition ,
10
- } from './helpers/utils.js' ;
5
+ import { constructPath , join , resolveRouteComponents } from './helpers/utils.js' ;
11
6
import { syncSearchParams } from './search-params.svelte.js' ;
12
7
13
8
/** @type {import('./index.d.ts').Routes } */
@@ -21,6 +16,9 @@ export let params = $state({ value: {} });
21
16
22
17
export let location = $state ( updatedLocation ( ) ) ;
23
18
19
+ let navigationIndex = 0 ;
20
+ let pendingNavigationIndex = 0 ;
21
+
24
22
/** @type {{ name?: string } } */
25
23
export const base = {
26
24
name : undefined ,
@@ -95,6 +93,10 @@ export async function onNavigate(path, options = {}) {
95
93
if ( ! routes ) {
96
94
throw new Error ( 'Router not initialized: `createRouter` was not called.' ) ;
97
95
}
96
+
97
+ navigationIndex ++ ;
98
+ const currentNavigationIndex = navigationIndex ;
99
+
98
100
let matchPath = path || globalThis . location . pathname ;
99
101
if ( base . name && matchPath . startsWith ( base . name ) ) {
100
102
matchPath = matchPath . slice ( base . name . length ) || '/' ;
@@ -103,15 +105,22 @@ export async function onNavigate(path, options = {}) {
103
105
104
106
for ( const { beforeLoad } of hooks ) {
105
107
try {
108
+ pendingNavigationIndex = currentNavigationIndex ;
106
109
await beforeLoad ?. ( ) ;
107
110
} catch {
108
111
return ;
109
112
}
110
113
}
111
114
112
- await wrapInViewTransition ( async ( ) => {
113
- componentTree . value = await resolveRouteComponents ( match ? [ ...layouts , match ] : layouts ) ;
114
- } , options . viewTransition ) ;
115
+ const fromBeforeLoadHook = new Error ( ) . stack ?. includes ( 'beforeLoad' ) ;
116
+
117
+ const routeComponents = await resolveRouteComponents ( match ? [ ...layouts , match ] : layouts ) ;
118
+ if (
119
+ navigationIndex !== currentNavigationIndex ||
120
+ ( fromBeforeLoadHook && pendingNavigationIndex + 1 !== currentNavigationIndex )
121
+ ) {
122
+ return ;
123
+ }
115
124
116
125
if ( path ) {
117
126
if ( options . search ) path += options . search ;
@@ -121,6 +130,13 @@ export async function onNavigate(path, options = {}) {
121
130
globalThis . history [ historyMethod ] ( options . state || { } , '' , to ) ;
122
131
}
123
132
133
+ if ( options . viewTransition && document . startViewTransition !== undefined ) {
134
+ document . startViewTransition ( ( ) => {
135
+ componentTree . value = routeComponents ;
136
+ } ) ;
137
+ } else {
138
+ componentTree . value = routeComponents ;
139
+ }
124
140
params . value = newParams || { } ;
125
141
syncSearchParams ( ) ;
126
142
Object . assign ( location , updatedLocation ( ) ) ;
0 commit comments