Skip to content

Commit

Permalink
feat(cli-service): add history api fallback for multi-page mode (#3181)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkzing authored and haoqunjiang committed Feb 19, 2019
1 parent bf59e4f commit ea5d9f7
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions packages/@vue/cli-service/lib/commands/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ module.exports = (api, options) => {
const isProduction = process.env.NODE_ENV === 'production'

const url = require('url')
const path = require('path')
const chalk = require('chalk')
const webpack = require('webpack')
const WebpackDevServer = require('webpack-dev-server')
Expand Down Expand Up @@ -139,9 +138,7 @@ module.exports = (api, options) => {
clientLogLevel: 'none',
historyApiFallback: {
disableDotRule: true,
rewrites: [
{ from: /./, to: path.posix.join(options.publicPath, 'index.html') }
]
rewrites: genHistoryApiFallbackRewrites(options.publicPath, options.pages)
},
contentBase: api.resolve('public'),
watchContentBase: !isProduction,
Expand Down Expand Up @@ -302,6 +299,23 @@ function checkInContainer () {
}
}

function genHistoryApiFallbackRewrites (baseUrl, pages = {}) {
const path = require('path')
const multiPageRewrites = Object
.keys(pages)
// sort by length in reversed order to avoid overrides
// eg. 'page11' should appear in front of 'page1'
.sort((a, b) => b.length - a.length)
.map(name => ({
from: new RegExp(`^/${name}`),
to: path.posix.join(baseUrl, pages[name].filename || `${name}.html`)
}))
return [
...multiPageRewrites,
{ from: /./, to: path.posix.join(baseUrl, 'index.html') }
]
}

module.exports.defaultModes = {
serve: 'development'
}

1 comment on commit ea5d9f7

@haoqunjiang
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.