Skip to content

Commit

Permalink
feat(router): add global pathOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Apr 29, 2020
1 parent 4fb713a commit 7383564
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
10 changes: 10 additions & 0 deletions playground/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '/' },
{
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 8 additions & 3 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
computeScrollPosition,
scrollToPosition,
} from './scrollBehavior'
import { createRouterMatcher } from './matcher'
import { createRouterMatcher, PathParserOptions } from './matcher'
import {
createRouterError,
ErrorTypes,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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>>()
Expand Down

0 comments on commit 7383564

Please sign in to comment.