Skip to content

Commit c91dd64

Browse files
ggwujunGGwujunposva
authored
fix(hash): allow characters after # in base e.g. #! (vuejs#925)
Co-authored-by: dashixiong <[email protected]> Co-authored-by: Eduardo San Martin Morote <[email protected]>
1 parent cd11f41 commit c91dd64

File tree

2 files changed

+106
-25
lines changed

2 files changed

+106
-25
lines changed

__tests__/history/html5.spec.ts

+101-23
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ describe('History HTMl5', () => {
1616
dom = createDom()
1717
})
1818

19+
beforeEach(() => {
20+
// empty the state to simulate an initial navigation by default
21+
window.history.replaceState(null, '', '')
22+
})
23+
1924
afterAll(() => {
2025
dom.window.close()
2126
})
@@ -30,6 +35,9 @@ describe('History HTMl5', () => {
3035
it('handles a basic base', () => {
3136
expect(createWebHistory().base).toBe('')
3237
expect(createWebHistory('/').base).toBe('')
38+
expect(createWebHistory('/#').base).toBe('/#')
39+
expect(createWebHistory('#!').base).toBe('#!')
40+
expect(createWebHistory('#other').base).toBe('#other')
3341
})
3442

3543
it('handles a base tag', () => {
@@ -68,11 +76,15 @@ describe('History HTMl5', () => {
6876
it('handles a single hash base', () => {
6977
expect(createWebHistory('#').base).toBe('#')
7078
expect(createWebHistory('#/').base).toBe('#')
79+
expect(createWebHistory('#!/').base).toBe('#!')
80+
expect(createWebHistory('#other/').base).toBe('#other')
7181
})
7282

7383
it('handles a non-empty hash base', () => {
7484
expect(createWebHistory('#/bar').base).toBe('#/bar')
7585
expect(createWebHistory('#/bar/').base).toBe('#/bar')
86+
expect(createWebHistory('#!/bar/').base).toBe('#!/bar')
87+
expect(createWebHistory('#other/bar/').base).toBe('#other/bar')
7688
})
7789

7890
it('prepends the host to support // urls', () => {
@@ -93,29 +105,95 @@ describe('History HTMl5', () => {
93105
spy.mockRestore()
94106
})
95107

96-
it('calls push with hash part of the url with a base', () => {
97-
dom.reconfigure({ url: 'file:///usr/etc/index.html' })
98-
let history = createWebHistory('#')
99-
let spy = jest.spyOn(window.history, 'pushState')
100-
history.push('/foo')
101-
expect(spy).toHaveBeenCalledWith(
102-
expect.anything(),
103-
expect.any(String),
104-
'#/foo'
105-
)
106-
spy.mockRestore()
107-
})
108+
describe('specific to base containing a hash', () => {
109+
it('calls push with hash part of the url with a base', () => {
110+
dom.reconfigure({ url: 'file:///usr/etc/index.html' })
111+
let initialSpy = jest.spyOn(window.history, 'replaceState')
112+
let history = createWebHistory('#')
113+
// initial navigation
114+
expect(initialSpy).toHaveBeenCalledWith(
115+
expect.anything(),
116+
expect.any(String),
117+
'#/'
118+
)
119+
let spy = jest.spyOn(window.history, 'pushState')
120+
history.push('/foo')
121+
expect(spy).toHaveBeenCalledWith(
122+
expect.anything(),
123+
expect.any(String),
124+
'#/foo'
125+
)
126+
spy.mockRestore()
127+
initialSpy.mockRestore()
128+
})
108129

109-
it('works with something after the hash in the base', () => {
110-
dom.reconfigure({ url: 'file:///usr/etc/index.html' })
111-
let history = createWebHistory('#something')
112-
let spy = jest.spyOn(window.history, 'pushState')
113-
history.push('/foo')
114-
expect(spy).toHaveBeenCalledWith(
115-
expect.anything(),
116-
expect.any(String),
117-
'#something/foo'
118-
)
119-
spy.mockRestore()
130+
it('works with something after the hash in the base', () => {
131+
dom.reconfigure({ url: 'file:///usr/etc/index.html' })
132+
let initialSpy = jest.spyOn(window.history, 'replaceState')
133+
let history = createWebHistory('#something')
134+
// initial navigation
135+
expect(initialSpy).toHaveBeenCalledWith(
136+
expect.anything(),
137+
expect.any(String),
138+
'#something/'
139+
)
140+
let spy = jest.spyOn(window.history, 'pushState')
141+
history.push('/foo')
142+
expect(spy).toHaveBeenCalledWith(
143+
expect.anything(),
144+
expect.any(String),
145+
'#something/foo'
146+
)
147+
spy.mockRestore()
148+
initialSpy.mockRestore()
149+
})
150+
151+
it('works with #! and on a file with initial location', () => {
152+
dom.reconfigure({ url: 'file:///usr/etc/index.html#!/foo' })
153+
let spy = jest.spyOn(window.history, 'replaceState')
154+
createWebHistory('#!')
155+
expect(spy).toHaveBeenCalledWith(
156+
expect.anything(),
157+
expect.any(String),
158+
'#!/foo'
159+
)
160+
spy.mockRestore()
161+
})
162+
163+
it('works with #other', () => {
164+
dom.reconfigure({ url: 'file:///usr/etc/index.html' })
165+
let spy = jest.spyOn(window.history, 'replaceState')
166+
createWebHistory('#other')
167+
expect(spy).toHaveBeenCalledWith(
168+
expect.anything(),
169+
expect.any(String),
170+
'#other/'
171+
)
172+
spy.mockRestore()
173+
})
174+
175+
it('works with custom#other in domain', () => {
176+
dom.reconfigure({ url: 'https://esm.dev/custom' })
177+
let spy = jest.spyOn(window.history, 'replaceState')
178+
createWebHistory('custom#other')
179+
expect(spy).toHaveBeenCalledWith(
180+
expect.anything(),
181+
expect.any(String),
182+
'#other/'
183+
)
184+
spy.mockRestore()
185+
})
186+
187+
it('works with #! and a host with initial location', () => {
188+
dom.reconfigure({ url: 'https://esm.dev/#!/foo' })
189+
let spy = jest.spyOn(window.history, 'replaceState')
190+
createWebHistory('/#!')
191+
expect(spy).toHaveBeenCalledWith(
192+
expect.anything(),
193+
expect.any(String),
194+
'#!/foo'
195+
)
196+
spy.mockRestore()
197+
})
120198
})
121199
})

src/history/html5.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,14 @@ function createCurrentLocation(
3939
location: Location
4040
): HistoryLocation {
4141
const { pathname, search, hash } = location
42-
// allows hash based url
42+
// allows hash bases like #, /#, #/, #!, #!/, /#!/, or even /folder#end
4343
const hashPos = base.indexOf('#')
4444
if (hashPos > -1) {
45+
let slicePos = hash.includes(base.slice(hashPos))
46+
? base.slice(hashPos).length
47+
: 1
48+
let pathFromHash = hash.slice(slicePos)
4549
// prepend the starting slash to hash so the url starts with /#
46-
let pathFromHash = hash.slice(1)
4750
if (pathFromHash[0] !== '/') pathFromHash = '/' + pathFromHash
4851
return stripBase(pathFromHash, '')
4952
}

0 commit comments

Comments
 (0)