@@ -39,7 +39,13 @@ import {
39
39
} from './constants.js' ;
40
40
import { validate_page_exports } from '../../utils/exports.js' ;
41
41
import { compact } from '../../utils/array.js' ;
42
- import { INVALIDATED_PARAM , TRAILING_SLASH_PARAM , validate_depends } from '../shared.js' ;
42
+ import {
43
+ INVALIDATED_PARAM ,
44
+ stringify ,
45
+ TRAILING_SLASH_PARAM ,
46
+ validate_depends ,
47
+ parse as unstringify
48
+ } from '../shared.js' ;
43
49
import { get_message , get_status } from '../../utils/error.js' ;
44
50
import { writable } from 'svelte/store' ;
45
51
import { page , update , navigating } from './state.svelte.js' ;
@@ -1590,7 +1596,7 @@ async function navigate({
1590
1596
const entry = {
1591
1597
[ HISTORY_INDEX ] : ( current_history_index += change ) ,
1592
1598
[ NAVIGATION_INDEX ] : ( current_navigation_index += change ) ,
1593
- [ STATES_KEY ] : state
1599
+ [ STATES_KEY ] : stringify ( state , app . hooks . transport )
1594
1600
} ;
1595
1601
1596
1602
const fn = replace_state ? history . replaceState : history . pushState ;
@@ -2144,18 +2150,8 @@ export function pushState(url, state) {
2144
2150
throw new Error ( 'Cannot call pushState(...) on the server' ) ;
2145
2151
}
2146
2152
2147
- if ( DEV ) {
2148
- if ( ! started ) {
2149
- throw new Error ( 'Cannot call pushState(...) before router is initialized' ) ;
2150
- }
2151
-
2152
- try {
2153
- // use `devalue.stringify` as a convenient way to ensure we exclude values that can't be properly rehydrated, such as custom class instances
2154
- devalue . stringify ( state ) ;
2155
- } catch ( error ) {
2156
- // @ts -expect-error
2157
- throw new Error ( `Could not serialize state${ error . path } ` ) ;
2158
- }
2153
+ if ( DEV && ! started ) {
2154
+ throw new Error ( 'Cannot call pushState(...) before router is initialized' ) ;
2159
2155
}
2160
2156
2161
2157
update_scroll_positions ( current_history_index ) ;
@@ -2164,7 +2160,7 @@ export function pushState(url, state) {
2164
2160
[ HISTORY_INDEX ] : ( current_history_index += 1 ) ,
2165
2161
[ NAVIGATION_INDEX ] : current_navigation_index ,
2166
2162
[ PAGE_URL_KEY ] : page . url . href ,
2167
- [ STATES_KEY ] : state
2163
+ [ STATES_KEY ] : stringify ( state , app . hooks . transport )
2168
2164
} ;
2169
2165
2170
2166
history . pushState ( opts , '' , resolve_url ( url ) ) ;
@@ -2191,25 +2187,15 @@ export function replaceState(url, state) {
2191
2187
throw new Error ( 'Cannot call replaceState(...) on the server' ) ;
2192
2188
}
2193
2189
2194
- if ( DEV ) {
2195
- if ( ! started ) {
2196
- throw new Error ( 'Cannot call replaceState(...) before router is initialized' ) ;
2197
- }
2198
-
2199
- try {
2200
- // use `devalue.stringify` as a convenient way to ensure we exclude values that can't be properly rehydrated, such as custom class instances
2201
- devalue . stringify ( state ) ;
2202
- } catch ( error ) {
2203
- // @ts -expect-error
2204
- throw new Error ( `Could not serialize state${ error . path } ` ) ;
2205
- }
2190
+ if ( DEV && ! started ) {
2191
+ throw new Error ( 'Cannot call replaceState(...) before router is initialized' ) ;
2206
2192
}
2207
2193
2208
2194
const opts = {
2209
2195
[ HISTORY_INDEX ] : current_history_index ,
2210
2196
[ NAVIGATION_INDEX ] : current_navigation_index ,
2211
2197
[ PAGE_URL_KEY ] : page . url . href ,
2212
- [ STATES_KEY ] : state
2198
+ [ STATES_KEY ] : stringify ( state , app . hooks . transport )
2213
2199
} ;
2214
2200
2215
2201
history . replaceState ( opts , '' , resolve_url ( url ) ) ;
@@ -2519,7 +2505,7 @@ function _start_router() {
2519
2505
if ( history_index === current_history_index ) return ;
2520
2506
2521
2507
const scroll = scroll_positions [ history_index ] ;
2522
- const state = event . state [ STATES_KEY ] ?? { } ;
2508
+ const state = unstringify ( event . state [ STATES_KEY ] , app . hooks . transport ) ?? { } ;
2523
2509
const url = new URL ( event . state [ PAGE_URL_KEY ] ?? location . href ) ;
2524
2510
const navigation_index = event . state [ NAVIGATION_INDEX ] ;
2525
2511
const is_hash_change = current . url ? strip_hash ( location ) === strip_hash ( current . url ) : false ;
0 commit comments