Skip to content

fix: update state key references from 'key' to '__TSR_key' #4356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
```
18 changes: 13 additions & 5 deletions packages/history/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
},
'',
)
Expand Down Expand Up @@ -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
})(),
}
}

Expand Down
9 changes: 6 additions & 3 deletions packages/router-core/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
},
},
},
Expand Down