import { isWeex , isWeb } from 'universal-env' ; const TARGET_PATH_REG = /[?&]_path=([^&#]+)/i ; export default function pathRedirect ( history , routes ) { let targetPath = '' ; let targetQuery = null ; if ( isWeb && TARGET_PATH_REG . test ( window . location . search ) ) { targetQuery = window . location . search . match ( TARGET_PATH_REG ) ; } if ( isWeex && TARGET_PATH_REG . test ( window . location . href ) ) { targetQuery = window . location . href . match ( TARGET_PATH_REG ) ; } if ( ! targetQuery && TARGET_PATH_REG . test ( history . location . search ) ) { targetQuery = history . location . search . match ( TARGET_PATH_REG ) ; } let isConfirmed = false ; targetPath = targetQuery ? targetQuery [ 1 ] : '' ; for ( let i = 0 , l = routes . length ; i < l ; i ++ ) { if ( targetPath === routes [ i ] . path ) { isConfirmed = true ; break ; } } if ( targetPath && ! isConfirmed ) { console . warn ( 'Warning: url query `_path` should be an exist path in app.json, see: https://rax.js.org/docs/guide/routes ' ) ; return false ; } if ( targetPath ) { history . replace ( targetPath + history . location . search ) ; } }