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 history = createWebHistory('#')
let spy = jest.spyOn(window.history, 'pushState')
history.push('/foo')
expect(spy).toHaveBeenCalledWith(
### Parameters
-| Parameter | Type | Description |
-| --------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
-| base | `string` | optional base to provide. Defaults to `location.pathname` or `/` if at root. If there is a `<base>` tag in the `head`, its value will be ignored in favor of this parameter **but note it affects all the history.pushState() calls**, meaning that if you use a `<base>` tag, its `href` value **has to match this parameter** (ignoring anything after the |
-| `#`) |
+| Parameter | Type | Description |
+| --------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
+| base | `string` | optional base to provide. Defaults to `location.pathname + location.search`. If there is a `<base>` tag in the `head`, its value will be ignored in favor of this parameter **but note it affects all the history.pushState() calls**, meaning that if you use a `<base>` tag, its `href` value **has to match this parameter** (ignoring anything after the `#`) |
### Examples
const router = createRouter({
// keep a trailing slash in this specific case because we are using a hash
// history
- history: createWebHashHistory('/' + __dirname + '/#/'),
+ history: createWebHashHistory(),
routes: [
{ path: '/', component: Home },
{ path: '/redirect', name: 'redirect', redirect: '/foo' },
.execute(function () {
window.history.replaceState(history.state, '', '/hash/#/foo')
})
+ .assert.urlEquals(baseURL + '/foo')
.click('li:nth-child(3) a')
.assert.urlEquals(baseURL + '/bar')
.back()
* Creates a hash history. Useful for web applications with no host (e.g.
* `file://`) or when configuring a server to handle any URL is not possible.
*
- * @param base - optional base to provide. Defaults to `location.pathname` or
- * `/` if at root. If there is a `<base>` tag in the `head`, its value will be
+ * @param base - optional base to provide. Defaults to `location.pathname +
+ * location.search` If there is a `<base>` tag in the `head`, its value will be
* ignored in favor of this parameter **but note it affects all the
* history.pushState() calls**, meaning that if you use a `<base>` tag, it's
* `href` value **has to match this parameter** (ignoring anything after the
// 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 : ''
+ base = location.host ? base || location.pathname + location.search : ''
// allow the user to provide a `#` in the middle: `/base/#/app`
if (base.indexOf('#') < 0) base += '#'
state: StateEntry,
replace: boolean
): void {
- // when the base has a `#`, only use that for the URL
+ /**
+ * if a base tag is provided and we are on a normal domain, we have to
+ * respect the provided `base` attribute because pushState() will use it and
+ * potentially erase anything before the `#` like at
+ * https://github.com/vuejs/vue-router-next/issues/685 where a base of
+ * `/folder/#` but a base of `/` would erase the `/folder/` section. If
+ * there is no host, the `<base>` tag makes no sense and if there isn't a
+ * base tag we can just use everything after the `#`.
+ */
const hashIndex = base.indexOf('#')
const url =
hashIndex > -1
- ? base.slice(hashIndex) + to
+ ? (location.host && document.querySelector('base')
+ ? base
+ : base.slice(hashIndex)) + to
: createBaseLocation() + base + to
try {
// BROWSER QUIRK