@@ -42,8 +42,10 @@ import { compact } from '../../utils/array.js';
42
42
import {
43
43
INVALIDATED_PARAM ,
44
44
TRAILING_SLASH_PARAM ,
45
+ validate_load_response ,
46
+ stringify ,
45
47
validate_depends ,
46
- validate_load_response
48
+ parse as unstringify
47
49
} from '../shared.js' ;
48
50
import { get_message , get_status } from '../../utils/error.js' ;
49
51
import { writable } from 'svelte/store' ;
@@ -1641,7 +1643,7 @@ async function navigate({
1641
1643
const entry = {
1642
1644
[ HISTORY_INDEX ] : ( current_history_index += change ) ,
1643
1645
[ NAVIGATION_INDEX ] : ( current_navigation_index += change ) ,
1644
- [ STATES_KEY ] : state
1646
+ [ STATES_KEY ] : stringify ( state , app . hooks . transport )
1645
1647
} ;
1646
1648
1647
1649
const fn = replace_state ? history . replaceState : history . pushState ;
@@ -2198,18 +2200,8 @@ export function pushState(url, state) {
2198
2200
throw new Error ( 'Cannot call pushState(...) on the server' ) ;
2199
2201
}
2200
2202
2201
- if ( DEV ) {
2202
- if ( ! started ) {
2203
- throw new Error ( 'Cannot call pushState(...) before router is initialized' ) ;
2204
- }
2205
-
2206
- try {
2207
- // use `devalue.stringify` as a convenient way to ensure we exclude values that can't be properly rehydrated, such as custom class instances
2208
- devalue . stringify ( state ) ;
2209
- } catch ( error ) {
2210
- // @ts -expect-error
2211
- throw new Error ( `Could not serialize state${ error . path } ` ) ;
2212
- }
2203
+ if ( DEV && ! started ) {
2204
+ throw new Error ( 'Cannot call pushState(...) before router is initialized' ) ;
2213
2205
}
2214
2206
2215
2207
update_scroll_positions ( current_history_index ) ;
@@ -2218,7 +2210,7 @@ export function pushState(url, state) {
2218
2210
[ HISTORY_INDEX ] : ( current_history_index += 1 ) ,
2219
2211
[ NAVIGATION_INDEX ] : current_navigation_index ,
2220
2212
[ PAGE_URL_KEY ] : page . url . href ,
2221
- [ STATES_KEY ] : state
2213
+ [ STATES_KEY ] : stringify ( state , app . hooks . transport )
2222
2214
} ;
2223
2215
2224
2216
history . pushState ( opts , '' , resolve_url ( url ) ) ;
@@ -2245,25 +2237,15 @@ export function replaceState(url, state) {
2245
2237
throw new Error ( 'Cannot call replaceState(...) on the server' ) ;
2246
2238
}
2247
2239
2248
- if ( DEV ) {
2249
- if ( ! started ) {
2250
- throw new Error ( 'Cannot call replaceState(...) before router is initialized' ) ;
2251
- }
2252
-
2253
- try {
2254
- // use `devalue.stringify` as a convenient way to ensure we exclude values that can't be properly rehydrated, such as custom class instances
2255
- devalue . stringify ( state ) ;
2256
- } catch ( error ) {
2257
- // @ts -expect-error
2258
- throw new Error ( `Could not serialize state${ error . path } ` ) ;
2259
- }
2240
+ if ( DEV && ! started ) {
2241
+ throw new Error ( 'Cannot call replaceState(...) before router is initialized' ) ;
2260
2242
}
2261
2243
2262
2244
const opts = {
2263
2245
[ HISTORY_INDEX ] : current_history_index ,
2264
2246
[ NAVIGATION_INDEX ] : current_navigation_index ,
2265
2247
[ PAGE_URL_KEY ] : page . url . href ,
2266
- [ STATES_KEY ] : state
2248
+ [ STATES_KEY ] : stringify ( state , app . hooks . transport )
2267
2249
} ;
2268
2250
2269
2251
history . replaceState ( opts , '' , resolve_url ( url ) ) ;
@@ -2570,7 +2552,7 @@ function _start_router() {
2570
2552
if ( history_index === current_history_index ) return ;
2571
2553
2572
2554
const scroll = scroll_positions [ history_index ] ;
2573
- const state = event . state [ STATES_KEY ] ?? { } ;
2555
+ const state = unstringify ( event . state [ STATES_KEY ] , app . hooks . transport ) ?? { } ;
2574
2556
const url = new URL ( event . state [ PAGE_URL_KEY ] ?? location . href ) ;
2575
2557
const navigation_index = event . state [ NAVIGATION_INDEX ] ;
2576
2558
const is_hash_change = current . url ? strip_hash ( location ) === strip_hash ( current . url ) : false ;
0 commit comments