diff --git a/docs/router/framework/react/api/router/ParsedHistoryStateType.md b/docs/router/framework/react/api/router/ParsedHistoryStateType.md index acf76739a8..bbc398e7a2 100644 --- a/docs/router/framework/react/api/router/ParsedHistoryStateType.md +++ b/docs/router/framework/react/api/router/ParsedHistoryStateType.md @@ -7,7 +7,8 @@ The `ParsedHistoryState` type represents a parsed state object. Additionally to ```tsx export type ParsedHistoryState = HistoryState & { - key?: string + key?: string // TODO: Remove in v2 - use __TSR_key instead + __TSR_key?: string __TSR_index: number } ``` diff --git a/packages/history/src/index.ts b/packages/history/src/index.ts index 3428c41939..c33ee24feb 100644 --- a/packages/history/src/index.ts +++ b/packages/history/src/index.ts @@ -53,7 +53,8 @@ export interface ParsedPath { export interface HistoryState {} export type ParsedHistoryState = HistoryState & { - key?: string + key?: string // TODO: Remove in v2 - use __TSR_key instead + __TSR_key?: string __TSR_index: number } @@ -252,9 +253,11 @@ function assignKeyAndIndex(index: number, state: HistoryState | undefined) { if (!state) { state = {} as HistoryState } + const key = createRandomKey() return { ...state, - key: createRandomKey(), + key, // TODO: Remove in v2 - use __TSR_key instead + __TSR_key: key, [stateIndexKey]: index, } as ParsedHistoryState } @@ -302,11 +305,13 @@ export function createBrowserHistory(opts?: { )) // Ensure there is always a key to start - if (!win.history.state?.key) { + if (!win.history.state?.__TSR_key && !win.history.state?.key) { + const key = createRandomKey() win.history.replaceState( { [stateIndexKey]: 0, - key: createRandomKey(), + key, // TODO: Remove in v2 - use __TSR_key instead + __TSR_key: key, }, '', ) @@ -632,7 +637,10 @@ export function parseHref( searchIndex > -1 ? href.slice(searchIndex, hashIndex === -1 ? undefined : hashIndex) : '', - state: state || { [stateIndexKey]: 0, key: createRandomKey() }, + state: state || (() => { + const key = createRandomKey() + return { [stateIndexKey]: 0, key, __TSR_key: key } // TODO: Remove key in v2 - use __TSR_key instead + })(), } } diff --git a/packages/router-core/src/router.ts b/packages/router-core/src/router.ts index ed2fb689b7..aafd3e8903 100644 --- a/packages/router-core/src/router.ts +++ b/packages/router-core/src/router.ts @@ -1102,7 +1102,8 @@ export class RouterCore< if (__tempLocation && (!__tempKey || __tempKey === this.tempLocationKey)) { // Sync up the location keys const parsedTempLocation = parse(__tempLocation) as any - parsedTempLocation.state.key = location.state.key + parsedTempLocation.state.key = location.state.key // TODO: Remove in v2 - use __TSR_key instead + parsedTempLocation.state.__TSR_key = location.state.__TSR_key delete parsedTempLocation.state.__tempLocation @@ -1823,7 +1824,8 @@ export class RouterCore< // temporarily add the previous values to the next state so they don't affect // the comparison const ignoredProps = [ - 'key', + 'key', // TODO: Remove in v2 - use __TSR_key instead + '__TSR_key', '__TSR_index', '__hashScrollIntoViewOptions', ] as const @@ -1864,7 +1866,8 @@ export class RouterCore< ...nextHistory.state, __tempKey: undefined!, __tempLocation: undefined!, - key: undefined!, + __TSR_key: undefined!, + key: undefined!, // TODO: Remove in v2 - use __TSR_key instead }, }, },