Skip to content

Commit ea5d9f7

Browse files
jkzinghaoqunjiang
authored andcommittedFeb 19, 2019
feat(cli-service): add history api fallback for multi-page mode (#3181)
1 parent bf59e4f commit ea5d9f7

File tree

1 file changed

+18
-4
lines changed
  • packages/@vue/cli-service/lib/commands

1 file changed

+18
-4
lines changed
 

‎packages/@vue/cli-service/lib/commands/serve.js

+18-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ module.exports = (api, options) => {
3333
const isProduction = process.env.NODE_ENV === 'production'
3434

3535
const url = require('url')
36-
const path = require('path')
3736
const chalk = require('chalk')
3837
const webpack = require('webpack')
3938
const WebpackDevServer = require('webpack-dev-server')
@@ -139,9 +138,7 @@ module.exports = (api, options) => {
139138
clientLogLevel: 'none',
140139
historyApiFallback: {
141140
disableDotRule: true,
142-
rewrites: [
143-
{ from: /./, to: path.posix.join(options.publicPath, 'index.html') }
144-
]
141+
rewrites: genHistoryApiFallbackRewrites(options.publicPath, options.pages)
145142
},
146143
contentBase: api.resolve('public'),
147144
watchContentBase: !isProduction,
@@ -302,6 +299,23 @@ function checkInContainer () {
302299
}
303300
}
304301

302+
function genHistoryApiFallbackRewrites (baseUrl, pages = {}) {
303+
const path = require('path')
304+
const multiPageRewrites = Object
305+
.keys(pages)
306+
// sort by length in reversed order to avoid overrides
307+
// eg. 'page11' should appear in front of 'page1'
308+
.sort((a, b) => b.length - a.length)
309+
.map(name => ({
310+
from: new RegExp(`^/${name}`),
311+
to: path.posix.join(baseUrl, pages[name].filename || `${name}.html`)
312+
}))
313+
return [
314+
...multiPageRewrites,
315+
{ from: /./, to: path.posix.join(baseUrl, 'index.html') }
316+
]
317+
}
318+
305319
module.exports.defaultModes = {
306320
serve: 'development'
307321
}

1 commit comments

Comments
 (1)

haoqunjiang commented on Feb 20, 2019

@haoqunjiang
Member

Sidenote: though the commit message indicated it was a feature implementation, I personally thought it could be a bug fix as well. So it's likely to be included in a patch release.

Please sign in to comment.