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)