Skip to content

Commit 4fbaa9f

Browse files
committed
fix: keep repeated params in query/hash relative locations
1 parent 7ddcc2d commit 4fbaa9f

File tree

2 files changed

+97
-39
lines changed

2 files changed

+97
-39
lines changed

src/util/location.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function normalizeLocation (
2727
}
2828

2929
// relative params
30-
if (!next.path && next.params && current) {
30+
if (!next.path && (next.params || next.query || next.hash) && current) {
3131
next = extend({}, next)
3232
next._normalized = true
3333
const params: any = extend(extend({}, current.params), next.params)

test/unit/specs/location.spec.js

+96-38
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ describe('Location utils', () => {
77
expect(loc._normalized).toBe(true)
88
expect(loc.path).toBe('/abc')
99
expect(loc.hash).toBe('#hello')
10-
expect(JSON.stringify(loc.query)).toBe(JSON.stringify({
11-
foo: 'bar',
12-
baz: 'qux'
13-
}))
10+
expect(JSON.stringify(loc.query)).toBe(
11+
JSON.stringify({
12+
foo: 'bar',
13+
baz: 'qux'
14+
})
15+
)
1416
})
1517

1618
it('empty string', function () {
@@ -36,23 +38,31 @@ describe('Location utils', () => {
3638
expect(loc._normalized).toBe(true)
3739
expect(loc.path).toBe('/root/abc')
3840
expect(loc.hash).toBe('#hello')
39-
expect(JSON.stringify(loc.query)).toBe(JSON.stringify({
40-
foo: 'bar',
41-
baz: 'qux'
42-
}))
41+
expect(JSON.stringify(loc.query)).toBe(
42+
JSON.stringify({
43+
foo: 'bar',
44+
baz: 'qux'
45+
})
46+
)
4347
})
4448

4549
it('relative append', () => {
46-
const loc = normalizeLocation('abc?foo=bar&baz=qux#hello', {
47-
path: '/root/next'
48-
}, true)
50+
const loc = normalizeLocation(
51+
'abc?foo=bar&baz=qux#hello',
52+
{
53+
path: '/root/next'
54+
},
55+
true
56+
)
4957
expect(loc._normalized).toBe(true)
5058
expect(loc.path).toBe('/root/next/abc')
5159
expect(loc.hash).toBe('#hello')
52-
expect(JSON.stringify(loc.query)).toBe(JSON.stringify({
53-
foo: 'bar',
54-
baz: 'qux'
55-
}))
60+
expect(JSON.stringify(loc.query)).toBe(
61+
JSON.stringify({
62+
foo: 'bar',
63+
baz: 'qux'
64+
})
65+
)
5666
})
5767

5868
it('relative query & hash', () => {
@@ -62,46 +72,92 @@ describe('Location utils', () => {
6272
expect(loc._normalized).toBe(true)
6373
expect(loc.path).toBe('/root/next')
6474
expect(loc.hash).toBe('#hello')
65-
expect(JSON.stringify(loc.query)).toBe(JSON.stringify({
66-
foo: 'bar',
67-
baz: 'qux'
68-
}))
75+
expect(JSON.stringify(loc.query)).toBe(
76+
JSON.stringify({
77+
foo: 'bar',
78+
baz: 'qux'
79+
})
80+
)
6981
})
7082

7183
it('relative params (named)', () => {
72-
const loc = normalizeLocation({ params: { lang: 'fr' }}, {
73-
name: 'hello',
74-
params: { lang: 'en', id: 'foo' }
75-
})
84+
const loc = normalizeLocation(
85+
{ params: { lang: 'fr' }},
86+
{
87+
name: 'hello',
88+
params: { lang: 'en', id: 'foo' }
89+
}
90+
)
7691
expect(loc._normalized).toBe(true)
7792
expect(loc.name).toBe('hello')
7893
expect(loc.params).toEqual({ lang: 'fr', id: 'foo' })
7994
})
8095

8196
it('relative params (non-named)', () => {
82-
const loc = normalizeLocation({ params: { lang: 'fr' }}, {
83-
path: '/en/foo',
84-
params: { lang: 'en', id: 'foo' },
85-
matched: [{ path: '/:lang(en|fr)/:id' }]
86-
})
97+
const loc = normalizeLocation(
98+
{ params: { lang: 'fr' }},
99+
{
100+
path: '/en/foo',
101+
params: { lang: 'en', id: 'foo' },
102+
matched: [{ path: '/:lang(en|fr)/:id' }]
103+
}
104+
)
87105
expect(loc._normalized).toBe(true)
88106
expect(loc.path).toBe('/fr/foo')
89107
})
90108

109+
it('relative query named', () => {
110+
const loc = normalizeLocation(
111+
{ query: { lang: 'fr' }},
112+
{
113+
name: 'hello',
114+
hash: '#foo',
115+
params: { id: 'foo' }
116+
}
117+
)
118+
expect(loc._normalized).toBe(true)
119+
expect(loc.name).toBe('hello')
120+
expect(loc.params).toEqual({ id: 'foo' })
121+
expect(loc.query).toEqual({ lang: 'fr' })
122+
expect(loc.hash).toBe(undefined)
123+
})
124+
125+
it('relative hash named', () => {
126+
const loc = normalizeLocation(
127+
{ hash: '#foo' },
128+
{
129+
name: 'hello',
130+
query: { lang: 'fr' },
131+
params: { id: 'foo' }
132+
}
133+
)
134+
expect(loc._normalized).toBe(true)
135+
expect(loc.name).toBe('hello')
136+
expect(loc.params).toEqual({ id: 'foo' })
137+
expect(loc.query).toBe(undefined)
138+
expect(loc.hash).toBe('#foo')
139+
})
140+
91141
it('custom regex can be case insensitive', () => {
92-
const loc = normalizeLocation({ params: { lang: 'FR' }}, {
93-
path: '/en/foo',
94-
params: { lang: 'en', id: 'foo' },
95-
matched: [{ path: '/:lang(en|fr)/:id' }]
96-
})
142+
const loc = normalizeLocation(
143+
{ params: { lang: 'FR' }},
144+
{
145+
path: '/en/foo',
146+
params: { lang: 'en', id: 'foo' },
147+
matched: [{ path: '/:lang(en|fr)/:id' }]
148+
}
149+
)
97150
expect(loc._normalized).toBe(true)
98151
expect(loc.path).toBe('/FR/foo')
99152
})
100153

101154
it('relative append', () => {
102155
const loc = normalizeLocation({ path: 'a' }, { path: '/b' }, true)
103156
expect(loc.path).toBe('/b/a')
104-
const loc2 = normalizeLocation({ path: 'a', append: true }, { path: '/b' })
157+
const loc2 = normalizeLocation(
158+
{ path: 'a', append: true },
159+
{ path: '/b' }
160+
)
105161
expect(loc2.path).toBe('/b/a')
106162
})
107163

@@ -114,10 +170,12 @@ describe('Location utils', () => {
114170
expect(loc._normalized).toBe(true)
115171
expect(loc.path).toBe('/abc')
116172
expect(loc.hash).toBe('#lol')
117-
expect(JSON.stringify(loc.query)).toBe(JSON.stringify({
118-
foo: 'bar',
119-
baz: 'qux'
120-
}))
173+
expect(JSON.stringify(loc.query)).toBe(
174+
JSON.stringify({
175+
foo: 'bar',
176+
baz: 'qux'
177+
})
178+
)
121179
})
122180

123181
it('skip normalized', () => {

0 commit comments

Comments
 (0)