chore(server): redirect if router.base specified in development (#8313)

This commit is contained in:
Sébastien Chopin 2020-11-09 18:17:32 +01:00 committed by GitHub
parent 63f5a0ed8c
commit e601f0a0d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,7 +4,7 @@ import launchMiddleware from 'launch-editor-middleware'
import serveStatic from 'serve-static'
import servePlaceholder from 'serve-placeholder'
import connect from 'connect'
import { determineGlobals, isUrl } from '@nuxt/utils'
import { determineGlobals, isUrl, urlJoin } from '@nuxt/utils'
import ServerContext from './context'
import renderAndGetWindow from './jsdom'
@ -161,6 +161,21 @@ export default class Server {
resources: this.resources
}))
// DX: redirect if router.base in development
if (this.options.dev && this.nuxt.options.router.base !== '/') {
this.useMiddleware({
prefix: false,
handler: (req, res) => {
const to = urlJoin(this.nuxt.options.router.base, req.url)
consola.info(`[Development] Redirecting from \`${req.url}\` to \`${to}\` (router.base specified).`)
res.writeHead(302, {
Location: to
})
res.end()
}
})
}
// Apply errorMiddleware from modules first
await this.nuxt.callHook('render:errorMiddleware', this.app)