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

fix(vite): don't match other endpoints _beginning_ with _nuxt #10121

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/vite/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import viteJsxPlugin from '@vitejs/plugin-vue-jsx'
import type { ServerOptions } from 'vite'
import { logger } from '@nuxt/kit'
import { getPort } from 'get-port-please'
import { joinURL, withoutLeadingSlash, withoutTrailingSlash } from 'ufo'
import { joinURL, withoutLeadingSlash, withTrailingSlash, withoutTrailingSlash } from 'ufo'
import defu from 'defu'
import type { OutputOptions } from 'rollup'
import { defineEventHandler } from 'h3'
Expand Down Expand Up @@ -130,11 +130,12 @@ export async function buildClient (ctx: ViteBuildContext) {
next()
}
})
const base = withTrailingSlash(clientConfig.base)
const viteMiddleware = defineEventHandler(async (event) => {
// Workaround: vite devmiddleware modifies req.url
const originalURL = event.node.req.url!
// @ts-expect-error _skip_transform is a private property
event.node.req._skip_transform = !originalURL.startsWith(clientConfig.base!)
event.node.req._skip_transform = !originalURL.startsWith(base)
await new Promise((resolve, reject) => {
viteServer.middlewares.handle(event.node.req, event.node.res, (err: Error) => {
event.node.req.url = originalURL
Expand Down
10 changes: 10 additions & 0 deletions test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,16 @@ describe('component islands', () => {
})
})

describe.runIf(process.env.NUXT_TEST_DEV && !process.env.TEST_WITH_WEBPACK)('vite plugins', () => {
it('does not override vite plugins', async () => {
expect(await $fetch('/vite-plugin-without-path')).toBe('vite-plugin without path')
expect(await $fetch('/__nuxt-test')).toBe('vite-plugin with __nuxt prefix')
})
it('does not allow direct access to nuxt source folder', async () => {
expect(await $fetch('/app.config')).toContain('404')
})
})

describe.skipIf(process.env.NUXT_TEST_DEV || isWindows)('payload rendering', () => {
it('renders a payload', async () => {
const payload = await $fetch('/random/a/_payload.js', { responseType: 'text' })
Expand Down
22 changes: 22 additions & 0 deletions test/fixtures/basic/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,28 @@ export default defineNuxtConfig({
export: 'namedExport',
filePath: '~/other-components-folder/named-export'
})
},
'vite:extendConfig' (config) {
config.plugins!.push({
name: 'nuxt:server',
configureServer (server) {
server.middlewares.use((req, res, next) => {
if (req.url === '/vite-plugin-without-path') {
res.end('vite-plugin without path')
return
}
next()
})

server.middlewares.use((req, res, next) => {
if (req.url === '/__nuxt-test') {
res.end('vite-plugin with __nuxt prefix')
return
}
next()
})
}
})
}
},
experimental: {
Expand Down