Skip to content

Commit 9e7a3dc

Browse files
committed
feat: perfect solution to support non-ASCII file name
1 parent 613ea7f commit 9e7a3dc

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

lib/app/app.js

-7
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,6 @@ export function createApp () {
6767

6868
// redirect /foo to /foo/
6969
router.beforeEach((to, from, next) => {
70-
const decodedPath = decodeURIComponent(to.path)
71-
if (decodedPath !== to.path) {
72-
next(Object.assign({}, to, {
73-
fullPath: decodeURIComponent(to.fullPath),
74-
path: decodedPath
75-
}))
76-
}
7770
if (!/(\/|\.html)$/.test(to.path)) {
7871
next(Object.assign({}, to, {
7972
path: to.path + '/'

lib/build.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ module.exports = async function build (sourceDir, cliOptions = {}) {
153153
console.error(chalk.red(`Error rendering ${pagePath}:`))
154154
throw e
155155
}
156-
const filename = pagePath.replace(/\/$/, '/index.html').replace(/^\//, '')
156+
const filename = decodeURIComponent(pagePath.replace(/\/$/, '/index.html').replace(/^\//, ''))
157157
const filePath = path.resolve(outDir, filename)
158158
await fs.ensureDir(path.dirname(filePath))
159159
await fs.writeFile(filePath, html)

lib/prepare.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ async function resolveOptions (sourceDir) {
199199
const key = 'v-' + Math.random().toString(16).slice(2)
200200
const data = {
201201
key,
202-
path: fileToPath(file)
202+
path: encodePath(fileToPath(file))
203203
}
204204

205205
if (shouldResolveLastUpdated) {
@@ -329,6 +329,15 @@ async function genRoutesFile ({ siteData: { pages }, sourceDir, pageFiles }) {
329329
}
330330
}`
331331

332+
const dncodedPath = decodeURIComponent(pagePath)
333+
if (dncodedPath !== pagePath) {
334+
code += `,
335+
{
336+
path: ${JSON.stringify(dncodedPath)},
337+
redirect: ${JSON.stringify(pagePath)}
338+
}`
339+
}
340+
332341
if (/\/$/.test(pagePath)) {
333342
code += `,
334343
{
@@ -365,6 +374,10 @@ function sort (arr) {
365374
})
366375
}
367376

377+
function encodePath (path) {
378+
return path.split('/').map(item => encodeURIComponent(item)).join('/')
379+
}
380+
368381
async function parseConfig (file) {
369382
const content = await fs.readFile(file, 'utf-8')
370383
const [extension] = /.\w+$/.exec(file)

0 commit comments

Comments
 (0)