import fakePromise from 'faked-promise'
-import { createRouter, createMemoryHistory, createWebHistory } from '../src'
+import {
+ createRouter,
+ createMemoryHistory,
+ createWebHistory,
+ createWebHashHistory,
+} from '../src'
import { NavigationFailureType } from '../src/errors'
import { createDom, components, tick, nextNavigation } from './utils'
import {
expect(router.currentRoute.value).not.toBe(START_LOCATION_NORMALIZED)
})
+ it('resolves hash history as a relative hash link', async () => {
+ let history = createWebHashHistory()
+ let { router } = await newRouter({ history })
+ expect(router.resolve('/foo?bar=baz#hey')).toMatchObject({
+ fullPath: '/foo?bar=baz#hey',
+ href: '#/foo?bar=baz#hey',
+ })
+ history = createWebHashHistory('/with/base/')
+ ;({ router } = await newRouter({ history }))
+ expect(router.resolve('/foo?bar=baz#hey')).toMatchObject({
+ fullPath: '/foo?bar=baz#hey',
+ href: '#/foo?bar=baz#hey',
+ })
+ history = createWebHashHistory('/with/#/base/')
+ ;({ router } = await newRouter({ history }))
+ expect(router.resolve('/foo?bar=baz#hey')).toMatchObject({
+ fullPath: '/foo?bar=baz#hey',
+ href: '#/base/foo?bar=baz#hey',
+ })
+ })
+
it('can await router.go', async () => {
const { router } = await newRouter()
await router.push('/foo')
* @returns a callback to remove the listener
*/
listen(callback: NavigationCallback): () => void
+
+ /**
+ * Generates the corresponding href to be used in an anchor tag.
+ *
+ * @param location
+ */
+ createHref(location: HistoryLocationNormalized): string
+
+ /**
+ * Clears any event listener attached by the history implementation.
+ */
destroy(): void
}
// to build an href
return removeTrailingSlash(base)
}
+
+// remove any character before the hash
+const BEFORE_HASH_RE = /^[^#]+#/
+export function createHref(
+ base: string,
+ location: HistoryLocationNormalized
+): string {
+ return base.replace(BEFORE_HASH_RE, '#') + location.fullPath
+}
RawHistoryLocation,
ValueContainer,
normalizeBase,
+ createHref,
} from './common'
import {
computeScrollPosition,
location: ('' as unknown) as HistoryLocationNormalized,
base,
go,
+ createHref: createHref.bind(null, base),
},
historyNavigation,
NavigationType,
NavigationDirection,
NavigationInformation,
+ createHref,
} from './common'
// TODO: verify base is working for SSR
location: START,
state: {},
base,
+ createHref: createHref.bind(null, base),
replace(to) {
const toNormalized = normalizeHistoryLocation(to)
currentLocation
)
+ let href = routerHistory.createHref(locationNormalized)
if (__DEV__) {
- let href = routerHistory.base + locationNormalized.fullPath
if (href.startsWith('//'))
warn(
`Location "${rawLocation}" resolved to "${href}". A resolved location cannot start with multiple slashes.`
return assign(locationNormalized, matchedRoute, {
params: decodeParams(matchedRoute.params),
redirectedFrom: undefined,
- href: routerHistory.base + locationNormalized.fullPath,
+ href,
})
}
let matchedRoute = matcher.resolve(matcherLocation, currentLocation)
const hash = encodeHash(rawLocation.hash || '')
- if (__DEV__ && hash && hash[0] !== '#') {
+ if (__DEV__ && hash && !hash.startsWith('#')) {
warn(
`A \`hash\` should always start with the character "#". Replace "${hash}" with "#${hash}".`
)
})
)
+ let href = routerHistory.createHref({ fullPath })
if (__DEV__) {
- let href = routerHistory.base + fullPath
if (href.startsWith('//'))
warn(
`Location "${rawLocation}" resolved to "${href}". A resolved location cannot start with multiple slashes.`
matchedRoute,
{
redirectedFrom: undefined,
- href: routerHistory.base + fullPath,
+ href,
}
)
}