From: Eduardo San Martin Morote Date: Fri, 15 May 2020 14:09:54 +0000 (+0200) Subject: fix(hash): allow base with non trailing slash X-Git-Tag: v4.0.0-alpha.12~15 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f5cc0505f9e0cc30ff94e362ceb24d300afd684d;p=thirdparty%2Fvuejs%2Frouter.git fix(hash): allow base with non trailing slash Close #247 BREAKING CHANGE: When providing a base for hash histories, it is now necessary to include a traling slash to create a url that starts with `/#/`, otherwise it will result in a url starting with `#/`. This allows users to use the routing system directly in simple files without needing to configure a server at all: - `https://example.com/file.html` + `base: 'file.html` will produce a final url of `https://example.com/file.html#/` - `https://example.com/folder` + `base: 'folder` will produce a final url of `https://example.com/folder#/` - `https://example.com/folder` + `base: 'folder/` will produce a final url of `https://example.com/folder/#/` --- diff --git a/__tests__/history/hash.spec.ts b/__tests__/history/hash.spec.ts index 1efffcb5..50cfcdc9 100644 --- a/__tests__/history/hash.spec.ts +++ b/__tests__/history/hash.spec.ts @@ -36,7 +36,7 @@ describe('History Hash', () => { dom.reconfigure({ url: 'https://example.com' }) }) - it('should use a correct', () => { + it('should use a correct base', () => { createWebHashHistory() expect(createWebHistory).toHaveBeenCalledWith('/#') }) @@ -48,15 +48,7 @@ describe('History Hash', () => { it('should be able to provide a base with no trailing slash', () => { createWebHashHistory('/folder') - expect(createWebHistory).toHaveBeenCalledWith('/folder/#') - }) - - it('should read the base tag', () => { - const baseEl = document.createElement('base') - baseEl.href = '/foo/' - document.head.appendChild(baseEl) - createWebHashHistory() - expect(createWebHistory).toHaveBeenCalledWith('/foo/#') + expect(createWebHistory).toHaveBeenCalledWith('/folder#') }) it('should use the base option over the base tag', () => { diff --git a/e2e/hash/index.ts b/e2e/hash/index.ts index 8de3d1ad..5b014b4e 100644 --- a/e2e/hash/index.ts +++ b/e2e/hash/index.ts @@ -18,7 +18,9 @@ const Unicode: RouteComponent = { } const router = createRouter({ - history: createWebHashHistory('/' + __dirname), + // keep a trailing slash in this specific case because we are using a hash + // history + history: createWebHashHistory('/' + __dirname + '/'), routes: [ { path: '/', component: Home }, { path: '/foo', component: Foo }, diff --git a/src/history/hash.ts b/src/history/hash.ts index f9a64868..1c1286be 100644 --- a/src/history/hash.ts +++ b/src/history/hash.ts @@ -1,7 +1,7 @@ -import { RouterHistory, normalizeBase } from './common' +import { RouterHistory } from './common' import { createWebHistory } from './html5' -export function createWebHashHistory(base?: string): RouterHistory { +export function createWebHashHistory(base: string = '/'): RouterHistory { // Make sure this implementation is fine in terms of encoding, specially for IE11 - return createWebHistory(location.host ? normalizeBase(base) + '/#' : '#') + return createWebHistory(location.host ? base + '#' : '#') }