From: Eduardo San Martin Morote Date: Wed, 29 Apr 2020 11:56:54 +0000 (+0200) Subject: feat(router): add global pathOptions X-Git-Tag: v4.0.0-alpha.8~10 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=73835649f450ffc378b906c72aa5ae8a6a03feb2;p=thirdparty%2Fvuejs%2Frouter.git feat(router): add global pathOptions --- diff --git a/playground/router.ts b/playground/router.ts index 65d7ba79..a2536356 100644 --- a/playground/router.ts +++ b/playground/router.ts @@ -19,6 +19,7 @@ let removeRoute: (() => void) | undefined export const routerHistory = createWebHistory() export const router = createRouter({ history: routerHistory, + pathOptions: { strict: true }, routes: [ { path: '/home', redirect: '/' }, { @@ -150,6 +151,15 @@ export const router = createRouter({ const delay = (t: number) => new Promise(resolve => setTimeout(resolve, t)) +// remove trailing slashes +router.beforeEach((to, from, next) => { + if (/.\/$/.test(to.path)) { + to.meta.redirectCode = 301 + next(to.path.replace(/\/$/, '')) + } else next() + // next() +}) + router.beforeEach(async (to, from, next) => { // console.log(`Guard from ${from.fullPath} to ${to.fullPath}`) if (to.params.id === 'no-name') return next(false) diff --git a/src/router.ts b/src/router.ts index dde44d76..0eaa56e4 100644 --- a/src/router.ts +++ b/src/router.ts @@ -24,7 +24,7 @@ import { computeScrollPosition, scrollToPosition, } from './scrollBehavior' -import { createRouterMatcher } from './matcher' +import { createRouterMatcher, PathParserOptions } from './matcher' import { createRouterError, ErrorTypes, @@ -111,7 +111,11 @@ export interface RouterOptions { * {@link RouterOptions.parseQuery | `parseQuery`} counterpart to handle query parsing. */ stringifyQuery?: typeof originalStringifyQuery - // TODO: allow customizing encoding functions + + /** + * Global matcher rules applied to every route record. + */ + pathOptions?: PathParserOptions } export interface Router { @@ -155,8 +159,9 @@ export function createRouter({ scrollBehavior, parseQuery = originalParseQuery, stringifyQuery = originalStringifyQuery, + pathOptions = {}, }: RouterOptions): Router { - const matcher = createRouterMatcher(routes, {}) + const matcher = createRouterMatcher(routes, pathOptions) const beforeGuards = useCallbacks>() const beforeResolveGuards = useCallbacks>()