Skip to content

feat: memory history stores state #2491

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

Merged
merged 4 commits into from
Apr 25, 2025
Merged
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
10 changes: 10 additions & 0 deletions packages/router/__tests__/history/memory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ describe('Memory history', () => {
expect(history.location).toEqual(START)
})

it('stores a state', () => {
const history = createMemoryHistory()
history.push(loc, { foo: 'bar' })
expect(history.state).toEqual({ foo: 'bar' })
history.push(loc, { foo: 'baz' })
expect(history.state).toEqual({ foo: 'baz' })
history.go(-1)
expect(history.state).toEqual({ foo: 'bar' })
})

it('does nothing with back if queue contains only one element', () => {
const history = createMemoryHistory()
history.go(-1)
Expand Down
29 changes: 17 additions & 12 deletions packages/router/src/history/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ import {
*/
export function createMemoryHistory(base: string = ''): RouterHistory {
let listeners: NavigationCallback[] = []
let queue: HistoryLocation[] = [START]
let queue: [url: HistoryLocation, state: HistoryState][] = [[START, {}]]
let position: number = 0
base = normalizeBase(base)

function setLocation(location: HistoryLocation) {
function setLocation(location: HistoryLocation, state: HistoryState = {}) {
position++
if (position !== queue.length) {
// we are in the middle, we remove everything from here in the queue
queue.splice(position)
}
queue.push(location)
queue.push([location, state])
}

function triggerListeners(
Expand All @@ -51,19 +51,19 @@ export function createMemoryHistory(base: string = ''): RouterHistory {
const routerHistory: RouterHistory = {
// rewritten by Object.defineProperty
location: START,
// TODO: should be kept in queue
// rewritten by Object.defineProperty
state: {},
base,
createHref: createHref.bind(null, base),

replace(to) {
replace(to, state?: HistoryState) {
// remove current entry and decrement position
queue.splice(position--, 1)
setLocation(to)
setLocation(to, state)
},

push(to, data?: HistoryState) {
setLocation(to)
push(to, state?: HistoryState) {
setLocation(to, state)
},

listen(callback) {
Expand All @@ -75,7 +75,7 @@ export function createMemoryHistory(base: string = ''): RouterHistory {
},
destroy() {
listeners = []
queue = [START]
queue = [[START, {}]]
position = 0
},

Expand All @@ -98,14 +98,19 @@ export function createMemoryHistory(base: string = ''): RouterHistory {

Object.defineProperty(routerHistory, 'location', {
enumerable: true,
get: () => queue[position],
get: () => queue[position][0],
})

Object.defineProperty(routerHistory, 'state', {
enumerable: true,
get: () => queue[position][1],
})

if (__TEST__) {
// @ts-expect-error: only for tests
routerHistory.changeURL = function (url: string) {
routerHistory.changeURL = function (url: string, state: HistoryState = {}) {
const from = this.location
queue.splice(position++ + 1, queue.length, url)
queue.splice(position++ + 1, queue.length, [url, state])
triggerListeners(this.location, from, {
direction: NavigationDirection.unknown,
delta: 0,
Expand Down