]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
feat(router): add global pathOptions
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 29 Apr 2020 11:56:54 +0000 (13:56 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 29 Apr 2020 11:56:54 +0000 (13:56 +0200)
playground/router.ts
src/router.ts

index 65d7ba79a4722ed3315de7522cb865f8e936082f..a253635687cafcc2b4d532cf59857aac2f2fe13d 100644 (file)
@@ -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)
index dde44d7673c03a17bf67f1cf8cb0a5f998d0d1c0..0eaa56e4e2a7c30d37899fd283339e2109cf42c3 100644 (file)
@@ -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<NavigationGuardWithThis<undefined>>()
   const beforeResolveGuards = useCallbacks<NavigationGuardWithThis<undefined>>()