diff --git a/packages/router/__tests__/history/memory.spec.ts b/packages/router/__tests__/history/memory.spec.ts index 6a6b19d25..5745c8043 100644 --- a/packages/router/__tests__/history/memory.spec.ts +++ b/packages/router/__tests__/history/memory.spec.ts @@ -1,11 +1,15 @@ import { createMemoryHistory } from '../../src/history/memory' -import { START, HistoryLocation } from '../../src/history/common' +import { START, HistoryLocation, HistoryState } from '../../src/history/common' import { vi, describe, expect, it } from 'vitest' const loc: HistoryLocation = '/foo' const loc2: HistoryLocation = '/bar' +const state: HistoryState = { foo: 'bar' } + +const state2: HistoryState = { foo: 'baz' } + describe('Memory history', () => { it('starts in nowhere', () => { const history = createMemoryHistory() @@ -51,6 +55,16 @@ describe('Memory history', () => { expect(history.location).toEqual(START) }) + it('stores a state', () => { + const history = createMemoryHistory() + history.push(loc, state) + expect(history.state).toEqual(state) + history.push(loc, state2) + expect(history.state).toEqual(state2) + history.go(-1) + expect(history.state).toEqual(state) + }) + it('does nothing with back if queue contains only one element', () => { const history = createMemoryHistory() history.go(-1) diff --git a/packages/router/src/history/memory.ts b/packages/router/src/history/memory.ts index f8f25a08c..3cb043f18 100644 --- a/packages/router/src/history/memory.ts +++ b/packages/router/src/history/memory.ts @@ -20,17 +20,17 @@ import { */ export function createMemoryHistory(base: string = ''): RouterHistory { let listeners: NavigationCallback[] = [] - let queue: HistoryLocation[] = [START] + let queue: [HistoryLocation, 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( @@ -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, data?: HistoryState) { // remove current entry and decrement position queue.splice(position--, 1) - setLocation(to) + setLocation(to, data) }, push(to, data?: HistoryState) { - setLocation(to) + setLocation(to, data) }, listen(callback) { @@ -75,7 +75,7 @@ export function createMemoryHistory(base: string = ''): RouterHistory { }, destroy() { listeners = [] - queue = [START] + queue = [[START, {}]] position = 0 }, @@ -98,14 +98,18 @@ 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) { const from = this.location - queue.splice(position++ + 1, queue.length, url) + queue.splice(position++ + 1, queue.length, [url, {}]) triggerListeners(this.location, from, { direction: NavigationDirection.unknown, delta: 0,