From dbcea25378234a8b85bf8689898fe5be9cbb8c21 Mon Sep 17 00:00:00 2001 From: ULIVZ <472590061@qq.com> Date: Tue, 22 May 2018 00:01:42 +0800 Subject: [PATCH] feat: perfect solution --- lib/app/app.js | 7 ------- lib/prepare.js | 15 ++++++++++++++- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/app/app.js b/lib/app/app.js index 406a9c6b52..ceaa7b905b 100644 --- a/lib/app/app.js +++ b/lib/app/app.js @@ -67,13 +67,6 @@ export function createApp () { // redirect /foo to /foo/ router.beforeEach((to, from, next) => { - const decodedPath = decodeURIComponent(to.path) - if (decodedPath !== to.path) { - next(Object.assign({}, to, { - fullPath: decodeURIComponent(to.fullPath), - path: decodedPath - })) - } if (!/(\/|\.html)$/.test(to.path)) { next(Object.assign({}, to, { path: to.path + '/' diff --git a/lib/prepare.js b/lib/prepare.js index 11db0c132c..d619dd0f32 100644 --- a/lib/prepare.js +++ b/lib/prepare.js @@ -199,7 +199,7 @@ async function resolveOptions (sourceDir) { const key = 'v-' + Math.random().toString(16).slice(2) const data = { key, - path: fileToPath(file) + path: encodePath(fileToPath(file)) } if (shouldResolveLastUpdated) { @@ -329,6 +329,15 @@ async function genRoutesFile ({ siteData: { pages }, sourceDir, pageFiles }) { } }` + const dncodedPath = decodeURIComponent(pagePath) + if (dncodedPath !== pagePath) { + code += `, + { + path: ${JSON.stringify(dncodedPath)}, + redirect: ${JSON.stringify(pagePath)} + }` + } + if (/\/$/.test(pagePath)) { code += `, { @@ -365,6 +374,10 @@ function sort (arr) { }) } +function encodePath (path) { + return path.split('/').map(item => encodeURIComponent(item)).join('/') +} + async function parseConfig (file) { const content = await fs.readFile(file, 'utf-8') const [extension] = /.\w+$/.exec(file)