From ed1c704835c280cd2d7ad795452f2da9c8401e2a Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 11 Oct 2022 06:26:38 +0100 Subject: [PATCH] fix(vite): prevent overlap between vite assets and app routes (#7989) --- packages/vite/src/client.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/vite/src/client.ts b/packages/vite/src/client.ts index 9f7df783b4..76e71183c9 100644 --- a/packages/vite/src/client.ts +++ b/packages/vite/src/client.ts @@ -5,8 +5,7 @@ import viteJsxPlugin from '@vitejs/plugin-vue-jsx' import type { Connect, ServerOptions } from 'vite' import { logger } from '@nuxt/kit' import { getPort } from 'get-port-please' -import { joinURL, withLeadingSlash, withoutLeadingSlash, withTrailingSlash } from 'ufo' -import escapeRE from 'escape-string-regexp' +import { joinURL, withoutLeadingSlash } from 'ufo' import defu from 'defu' import type { OutputOptions } from 'rollup' import { cacheDirPlugin } from './plugins/cache-dir' @@ -110,12 +109,12 @@ export async function buildClient (ctx: ViteBuildContext) { const viteServer = await vite.createServer(clientConfig) ctx.clientServer = viteServer await ctx.nuxt.callHook('vite:serverCreated', viteServer, { isClient: true, isServer: false }) - const baseURL = joinURL(ctx.nuxt.options.app.baseURL.replace(/^\./, '') || '/', ctx.nuxt.options.app.buildAssetsDir) - const BASE_RE = new RegExp(`^${escapeRE(withTrailingSlash(withLeadingSlash(baseURL)))}`) const viteMiddleware: Connect.NextHandleFunction = (req, res, next) => { // Workaround: vite devmiddleware modifies req.url const originalURL = req.url! - req.url = originalURL.replace(BASE_RE, '/') + if (!originalURL.startsWith('/__nuxt_vite_node__') && !originalURL.startsWith(clientConfig.base!)) { + req.url = joinURL('/__url', originalURL) + } viteServer.middlewares.handle(req, res, (err: unknown) => { req.url = originalURL next(err)