Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

fix(vite): prevent overlap between vite assets and app routes #7989

Merged
merged 1 commit into from Oct 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 4 additions & 5 deletions packages/vite/src/client.ts
Expand Up @@ -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'
Expand Down Expand Up @@ -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)
Expand Down