]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
fix(hash): only pushState the hash part
authorEduardo San Martin Morote <posva13@gmail.com>
Sat, 26 Sep 2020 15:38:57 +0000 (17:38 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Sat, 26 Sep 2020 15:38:57 +0000 (17:38 +0200)
Fix #495

__tests__/history/hash.spec.ts
__tests__/history/html5.spec.ts
src/history/hash.ts
src/history/html5.ts

index ccddf04d3c58f9bd93527ee4775ef2f24c90ddea..43a299c15c84435bbfef05f35229385e544923d4 100644 (file)
@@ -101,7 +101,7 @@ describe('History Hash', () => {
     it('should use a correct base', () => {
       createWebHashHistory()
       // both, a trailing / and none work
-      expect(createWebHistory).toHaveBeenCalledWith('/usr/some-file.html#')
+      expect(createWebHistory).toHaveBeenCalledWith('#')
     })
   })
 })
index 026bed0231bc5e440a371a76f47c51786dc578a1..c92ce0d62cba89bbd88e0be39dad8d19c0ff3e46 100644 (file)
@@ -93,7 +93,7 @@ describe('History HTMl5', () => {
     spy.mockRestore()
   })
 
-  it('works with file:/// urls and a base', () => {
+  it('calls push with hash part of the url with a base', () => {
     dom.reconfigure({ url: 'file:///usr/etc/index.html' })
     let history = createWebHistory('/usr/etc/index.html#/')
     let spy = jest.spyOn(window.history, 'pushState')
@@ -101,7 +101,20 @@ describe('History HTMl5', () => {
     expect(spy).toHaveBeenCalledWith(
       expect.anything(),
       expect.any(String),
-      'file:///usr/etc/index.html#/foo'
+      '#/foo'
+    )
+    spy.mockRestore()
+  })
+
+  it('works with something after the hash in the base', () => {
+    dom.reconfigure({ url: 'file:///usr/etc/index.html' })
+    let history = createWebHistory('#something')
+    let spy = jest.spyOn(window.history, 'pushState')
+    history.push('/foo')
+    expect(spy).toHaveBeenCalledWith(
+      expect.anything(),
+      expect.any(String),
+      '#something/foo'
     )
     spy.mockRestore()
   })
index 95e60acaa892ea7d59327cc63cf76dc86df7d3c9..7efeb5e437231409476e495339e142d864404529 100644 (file)
@@ -29,7 +29,7 @@ export function createWebHashHistory(base?: string): RouterHistory {
   // Make sure this implementation is fine in terms of encoding, specially for IE11
   // for `file://`, directly use the pathname and ignore the base
   // location.pathname contains an initial `/` even at the root: `https://example.com`
-  base = location.host ? base || location.pathname : location.pathname
+  base = location.host ? base || location.pathname : ''
   // allow the user to provide a `#` in the middle: `/base/#/app`
   if (base.indexOf('#') < 0) base += '#'
 
index 4d604e7541d3294fb72b0c2cd9a070f8c05e8cab..9656b252c3e04195588b5b40fe1cbba1ad32122e 100644 (file)
@@ -202,13 +202,12 @@ function useHistoryStateNavigation(base: string) {
     state: StateEntry,
     replace: boolean
   ): void {
+    // when the base has a `#`, only use that for the URL
+    const hashIndex = base.indexOf('#')
     const url =
-      createBaseLocation() +
-      // preserve any existing query when base has a hash
-      (base.indexOf('#') > -1 && location.search
-        ? location.pathname + location.search + '#'
-        : base) +
-      to
+      hashIndex > -1
+        ? base.slice(hashIndex) + to
+        : createBaseLocation() + base + to
     try {
       // BROWSER QUIRK
       // NOTE: Safari throws a SecurityError when calling this function 100 times in 30 seconds