add router base to all middleware

This commit is contained in:
Pooya Parsa 2017-08-25 16:31:16 +04:30
parent 7bc1b35b21
commit 1ca5739a2f
4 changed files with 16 additions and 13 deletions

View File

@ -518,7 +518,7 @@ export default class Builder extends Tapable {
this.webpackHotMiddleware = pify(webpackHotMiddleware(this.compiler.client, Object.assign({
log: false,
heartbeat: 2500
heartbeat: 1000
}, this.options.build.hotMiddleware)))
// Inject to renderer instance

View File

@ -151,7 +151,11 @@ export default function webpackClientConfig () {
config.plugins.push(new webpack.NamedModulesPlugin())
// Add HMR support
config.entry.app = ['webpack-hot-middleware/client?name=client&reload=true', config.entry.app]
config.entry.app = [
// https://github.com/glenjamin/webpack-hot-middleware#config
`webpack-hot-middleware/client?name=client&reload=true&timeout=3000&path=${this.options.router.base}/__webpack_hmr`.replace(/\/\//g, '/'),
config.entry.app
]
config.plugins.push(
new webpack.HotModuleReplacementPlugin(),
new webpack.NoEmitOnErrorsPlugin()

View File

@ -179,11 +179,9 @@ export default class Renderer extends Tapable {
m.handler = require(this.nuxt.resolvePath(m.handler))
}
// Use middleware
if (m instanceof Function) {
this.app.use(m)
} else if (m && m.path && m.handler) {
this.app.use(m.path, m.handler)
}
const handler = m.handler || m
const path = (this.options.router.base + (m.path ? m.path : '')).replace(/\/\//g, '/')
this.app.use(path, handler)
}
async setupMiddleware () {
@ -197,10 +195,6 @@ export default class Renderer extends Tapable {
// Common URL checks
this.useMiddleware((req, res, next) => {
// If base in req.url, remove it for the middleware and vue-router
if (this.options.router.base !== '/' && req.url.indexOf(this.options.router.base) === 0) {
req.url = req.url.replace(this.options.router.base, '/')
}
// Prevent access to SSR resources
if (ssrResourceRegex.test(req.url)) {
res.statusCode = 404

View File

@ -90,11 +90,16 @@ test('Check stats.json generated by build.analyze', t => {
t.is(stats.assets.length, 28)
})
test('Check /test.txt with custom serve-static options', async t => {
const { headers } = await rp(url('/test.txt'), { resolveWithFullResponse: true })
test('Check /test/test.txt with custom serve-static options', async t => {
const { headers } = await rp(url('/test/test.txt'), { resolveWithFullResponse: true })
t.is(headers['cache-control'], 'public, max-age=31536000')
})
test('Check /test.txt should return 404', async t => {
const err = await t.throws(rp(url('/test.txt')))
t.is(err.response.statusCode, 404)
})
// Close server and ask nuxt to stop listening to file changes
test.after('Closing server and nuxt.js', t => {
nuxt.close()