Skip to content

Commit 457d77d

Browse files
committed
fix: preserve extra properties when resolving locations
1 parent 1c0718f commit 457d77d

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

packages/router/src/experimental/route-resolver/resolver-fixed.spec.ts

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, it } from 'vitest'
22
import { createFixedResolver } from './resolver-fixed'
3-
import { NO_MATCH_LOCATION } from './resolver-abstract'
3+
import { MatcherLocationRaw, NO_MATCH_LOCATION } from './resolver-abstract'
44
import {
55
EmptyParams,
66
MatcherPatternHash,
@@ -266,6 +266,37 @@ describe('fixed resolver', () => {
266266
})
267267
})
268268

269+
it('keeps extra properties like state and replace from target location', () => {
270+
const resolver = createFixedResolver([
271+
{ name: 'any-path', path: ANY_PATH_PATTERN_MATCHER },
272+
])
273+
274+
const currentLocation = resolver.resolve({ path: '/bar' })
275+
276+
// extra parameters that should be preserved
277+
const extra = { state: { a: 1 }, replace: true }
278+
279+
for (const path of ['foo', './foo', '../foo']) {
280+
expect(
281+
resolver.resolve(
282+
{
283+
// done this way because TS accepts this kind of combination
284+
path,
285+
...extra,
286+
},
287+
currentLocation
288+
)
289+
).toMatchObject({
290+
params: {},
291+
path: '/foo',
292+
query: {},
293+
hash: {},
294+
state: { a: 1 },
295+
replace: true,
296+
})
297+
}
298+
})
299+
269300
it('resolves relative object locations', () => {
270301
const resolver = createFixedResolver([
271302
{ name: 'any-path', path: ANY_PATH_PATTERN_MATCHER },
@@ -445,6 +476,30 @@ describe('fixed resolver', () => {
445476
params: { pathMatch: '/?a=a&b=b#h' },
446477
})
447478
})
479+
480+
it('keeps extra properties like state and replace from target location', () => {
481+
const resolver = createFixedResolver([
482+
{ name: 'any-path', path: ANY_PATH_PATTERN_MATCHER },
483+
])
484+
485+
// extra parameters that should be preserved
486+
const extra = { state: { a: 1 }, replace: true }
487+
488+
expect(
489+
resolver.resolve({
490+
// done this way because TS accepts this kind of combination
491+
path: '/foo',
492+
...extra,
493+
})
494+
).toMatchObject({
495+
params: {},
496+
path: '/foo',
497+
query: {},
498+
hash: {},
499+
state: { a: 1 },
500+
replace: true,
501+
})
502+
})
448503
})
449504

450505
describe('named locations', () => {
@@ -490,6 +545,32 @@ describe('fixed resolver', () => {
490545
).toThrowError('Record "nonexistent" not found')
491546
})
492547

548+
it('keeps extra properties like state and replace from target location', () => {
549+
const resolver = createFixedResolver([
550+
{ name: 'home', path: EMPTY_PATH_PATTERN_MATCHER },
551+
])
552+
553+
// extra parameters that should be preserved
554+
const extra = { state: { a: 1 }, replace: true }
555+
556+
expect(
557+
resolver.resolve({
558+
// done this way because TS accepts this kind of combination
559+
name: 'home',
560+
params: {},
561+
...extra,
562+
})
563+
).toMatchObject({
564+
name: 'home',
565+
params: {},
566+
path: '/',
567+
query: {},
568+
hash: {},
569+
state: { a: 1 },
570+
replace: true,
571+
})
572+
})
573+
493574
it('resolves named locations with explicit query', () => {
494575
const resolver = createFixedResolver([
495576
{

packages/router/src/experimental/route-resolver/resolver-fixed.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ export function createFixedResolver<
204204
// @ts-expect-error: to is never
205205
const path = to.path ?? '/'
206206
return {
207+
// type is never
208+
...(to as any),
207209
...NO_MATCH_LOCATION,
208210
fullPath: NEW_stringifyURL(stringifyQuery, path, query, hash),
209211
path,
@@ -248,6 +250,8 @@ export function createFixedResolver<
248250
)
249251

250252
const url: LocationNormalized = {
253+
// preserve other fields like `state` and `replace`
254+
...to,
251255
fullPath: NEW_stringifyURL(
252256
stringifyQuery,
253257
path,
@@ -279,6 +283,8 @@ export function createFixedResolver<
279283
const query = normalizeQuery(to.query)
280284
const path = resolveRelativePath(to.path, currentLocation?.path || '/')
281285
url = {
286+
// preserve other fields like `state` and `replace`
287+
...to,
282288
fullPath: NEW_stringifyURL(stringifyQuery, path, query, to.hash),
283289
path,
284290
query,

0 commit comments

Comments
 (0)