Skip to content

Commit

Permalink
fix: 修复 router 校验的bug (NervJS#15716)
Browse files Browse the repository at this point in the history
* fix: 修复路由判定逻辑

* fix: 修复 router 校验的bug

---------

Co-authored-by: liuzejia <liuzejia@SZMAC-FV0MR4G7.local>
  • Loading branch information
ZEJIA-LIU and liuzejia authored May 14, 2024
1 parent 5335db8 commit a91e4de
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions packages/taro-router/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ import { RouterConfig } from './router'
import stacks from './router/stack'
import { routesAlias } from './utils'

import type { Path } from 'history'
import type { NavigateBackOption, NavigateOption, Option } from '../types/api'

type MethodName = 'navigateTo' | 'navigateBack' | 'switchTab' | 'redirectTo' | 'reLaunch'

const routeEvtChannel = EventChannel.routeChannel

function processNavigateUrl (option: Option) {
// Note: 该方法仅用于处理 navigateTo、redirectTo、reLaunch 方法的 url 参数,主要处理相对路径
function processNavigateUrlWithRelativePath (option: Option): Partial<Path> {
const pathPieces = parsePath(option.url)

// 处理相对路径
if (pathPieces.pathname?.includes('./')) {
const parts = routesAlias.getOrigin(history.location.pathname).split('/')
Expand All @@ -30,15 +31,20 @@ function processNavigateUrl (option: Option) {
pathPieces.pathname = parts.join('/')
}

// hack fix history v5 bug: https://github.com/remix-run/history/issues/814
if (!pathPieces.search) pathPieces.search = ''

return pathPieces
}

// Note: 该方法仅用于处理 navigateTo、redirectTo、reLaunch 方法的 url 参数,主要处理自定义路由、basename
function processNavigateUrlWithAliasAndBasename (pathPieces: Partial<Path>): Partial<Path> {
// 处理自定义路由
pathPieces.pathname = routesAlias.getAlias(addLeadingSlash(pathPieces.pathname))

// 处理 basename
pathPieces.pathname = prependBasename(pathPieces.pathname)

// hack fix history v5 bug: https://github.com/remix-run/history/issues/814
if (!pathPieces.search) pathPieces.search = ''

return pathPieces
}

Expand All @@ -60,7 +66,9 @@ async function navigate (option: Option | NavigateBackOption, method: MethodName

try {
if ('url' in option) {
if (!RouterConfig.isPage(addLeadingSlash(option.url))) {
let pathPieces = processNavigateUrlWithRelativePath(option)
// Note: 因为 RouterConfig.isPage 方法不对 customRoutes 和 basename 进行处理,所以这里也不处理
if (!RouterConfig.isPage(addLeadingSlash(pathPieces.pathname))) {
const res = { errMsg: `${method}:fail page ${option.url} is not found` }
fail?.(res)
complete?.(res)
Expand All @@ -70,7 +78,8 @@ async function navigate (option: Option | NavigateBackOption, method: MethodName
return reject(res)
}
}
const pathPieces = processNavigateUrl(option)
// Note: 判断是否为合法的 url 之后,再进行自定义路由和 basename 的处理
pathPieces = processNavigateUrlWithAliasAndBasename(pathPieces)
const state = { timestamp: Date.now() }
if (method === 'navigateTo') {
history.push(pathPieces, state)
Expand Down

0 comments on commit a91e4de

Please sign in to comment.