From e601f0a0d633e8274bc48a4fa8db672c6b04348b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Mon, 9 Nov 2020 18:17:32 +0100 Subject: [PATCH] chore(server): redirect if `router.base` specified in development (#8313) --- packages/server/src/server.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/packages/server/src/server.js b/packages/server/src/server.js index e330d86510..6edc2b190e 100644 --- a/packages/server/src/server.js +++ b/packages/server/src/server.js @@ -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)