Skip to content

Commit

Permalink
fix(router): H5下 ReLaunch 没办法跳到指定的页面
Browse files Browse the repository at this point in the history
  • Loading branch information
Blet authored and Littly committed Dec 26, 2019
1 parent 325e6d2 commit 484d3b0
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 32 deletions.
7 changes: 7 additions & 0 deletions packages/taro-router/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"tabWidth": 2,
"singleQuote": true,
"jsxSingleQuote": true,
"semi": false,
"printWidth": 80
}
96 changes: 64 additions & 32 deletions packages/taro-router/src/apis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ let currentPagename = ''
const relaunchUrlKey = '__relaunchUrl'

const addHtmlExtname = (str: string) => {
return /\.html\b/.test(str)
? str
: `${str}.html`
return /\.html\b/.test(str) ? str : `${str}.html`
}

const getTargetUrl = (url: string, customRoutes: CustomRoutes) => {
Expand All @@ -55,12 +53,19 @@ const getTargetUrl = (url: string, customRoutes: CustomRoutes) => {
const search = matched[2] || ''

const targetUrl = rp(pathname, currentPagename)
const nextPagename = addHtmlExtname(stripLeadingSlash(customRoutes[targetUrl] || targetUrl))
const nextPagename = addHtmlExtname(
stripLeadingSlash(customRoutes[targetUrl] || targetUrl)
)
return `${basename}/${nextPagename}${search}`
}

const createNavigateTo = ({ customRoutes }: RouterConfig, history?: History) => {
return function ({ url }: NavigateToOption): Promise<Taro.General.CallbackResult> {
const createNavigateTo = (
{ customRoutes }: RouterConfig,
history?: History
) => {
return function({
url
}: NavigateToOption): Promise<Taro.General.CallbackResult> {
const res: Taro.General.CallbackResult = {
errMsg: ''
}
Expand All @@ -83,14 +88,22 @@ const createNavigateTo = ({ customRoutes }: RouterConfig, history?: History) =>
}
}

const createNavigateBack = ({ customRoutes }: RouterConfig, history?: History) => {
return function (opts: NavigateBackOption = {}): Promise<Taro.General.CallbackResult> {
const createNavigateBack = (
{ customRoutes }: RouterConfig,
history?: History
) => {
return function(
opts: NavigateBackOption = {}
): Promise<Taro.General.CallbackResult> {
const res: Taro.General.CallbackResult = {
errMsg: ''
}
try {
const { delta = 1 } = opts
invariant(delta >= 0, 'navigateBack must be called with a delta greater than 0')
invariant(
delta >= 0,
'navigateBack must be called with a delta greater than 0'
)
if (history) {
history.go(-delta)
} else {
Expand All @@ -106,8 +119,13 @@ const createNavigateBack = ({ customRoutes }: RouterConfig, history?: History) =
}
}

const createRedirectTo = ({ customRoutes }: RouterConfig, history?: History) => {
return function ({ url }: RedirectToOption): Promise<Taro.General.CallbackResult> {
const createRedirectTo = (
{ customRoutes }: RouterConfig,
history?: History
) => {
return function({
url
}: RedirectToOption): Promise<Taro.General.CallbackResult> {
const res: Taro.General.CallbackResult = {
errMsg: ''
}
Expand All @@ -116,8 +134,9 @@ const createRedirectTo = ({ customRoutes }: RouterConfig, history?: History) =>
invariant(url, 'redirectTo must be called with a url')

if (/^(https?:)\/\//.test(url)) {
window.location.assign(url);
} if (history) {
window.location.assign(url)
}
if (history) {
history.replace(url)
} else {
window.location.replace(getTargetUrl(url, customRoutes))
Expand All @@ -141,28 +160,41 @@ const createReLaunch = ({ customRoutes }: RouterConfig, history?: History) => {
} catch (e) {
console.log(e.message)
}
return function ({ url }): Promise<Taro.General.CallbackResult> {
const res: Taro.General.CallbackResult = {
errMsg: ''
}
try {
if (history) {
history.go(-(history.length - 1))
if (/^(https?:)\/\//.test(url)) {
window.location.assign(url);
return function({ url }): Promise<Taro.General.CallbackResult> {
return new Promise((resolve, reject) => {
const res: Taro.General.CallbackResult = {
errMsg: ''
}
try {
// setTimeout hack
// 修复 history.go 之后,后面的代码不执行的问题
setTimeout(() => {
if (history) {
if (/^(https?:)\/\//.test(url)) {
window.location.assign(url)
} else {
history.replace(url)
}
} else {
localStorage.setItem(
relaunchUrlKey,
getTargetUrl(url, customRoutes)
)
window.history.go(-(window.history.length - 1))
}
res.errMsg = 'reLaunch:ok'
resolve(res)
}, 50)
if (history) {
history.go(-(history.length - 1))
} else {
history.replace(url)
window.history.go(-(window.history.length - 1))
}
} else {
localStorage.setItem(relaunchUrlKey, getTargetUrl(url, customRoutes))
window.history.go(-(window.history.length - 1))
} catch (e) {
res.errMsg = `reLaunch:fail ${e.message}`
reject(res)
}
res.errMsg = 'reLaunch:ok'
return Promise.resolve(res)
} catch (e) {
res.errMsg = `reLaunch:fail ${e.message}`
return Promise.reject(res)
}
})
}
}

Expand Down

0 comments on commit 484d3b0

Please sign in to comment.