Skip to content

Commit 3e1c796

Browse files
committed
fix(schema,nitro,vite): add devServer.allowedHosts
1 parent 084396b commit 3e1c796

6 files changed

Lines changed: 111 additions & 2 deletions

File tree

‎packages/nitro-server/src/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1314,7 +1314,7 @@ async function spaLoadingTemplatePath (nuxt: Nuxt) {
13141314
}
13151315

13161316
function getDevHandlerAllowedHosts (nuxt: Nuxt): ReadonlySet<string> | true {
1317-
const allowedHosts = nuxt.options.vite?.server?.allowedHosts
1317+
const allowedHosts = nuxt.options.devServer.allowedHosts
13181318
if (allowedHosts === true) {
13191319
return true
13201320
}

‎packages/schema/src/config/dev.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,23 @@ export default defineResolvers({
66
devServer: {
77
https: false,
88
port: Number(process.env.NUXT_PORT || process.env.NITRO_PORT || process.env.PORT || 3000),
9-
host: process.env.NUXT_HOST || process.env.NITRO_HOST || process.env.HOST || undefined,
9+
host: {
10+
$resolve: val => typeof val === 'string' && val ? val : process.env.NUXT_HOST || process.env.NITRO_HOST || process.env.HOST || undefined,
11+
},
12+
allowedHosts: {
13+
$resolve: async (val, get) => {
14+
const [host, viteAllowedHosts] = await Promise.all([get('devServer.host'), get('vite.server').then(r => r?.allowedHosts)])
15+
if (val === true || viteAllowedHosts === true) {
16+
return true
17+
}
18+
const hosts = [
19+
...Array.isArray(val) ? val : [],
20+
...Array.isArray(viteAllowedHosts) ? viteAllowedHosts : [],
21+
host,
22+
]
23+
return [...new Set(hosts.filter((h): h is string => typeof h === 'string' && !!h))]
24+
},
25+
},
1026
url: 'http://localhost:3000',
1127
loadingTemplate,
1228
errorChannel: '/__nuxt_dev__/error',

‎packages/schema/src/types/schema.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,15 @@ export interface ConfigSchema {
10321032
*/
10331033
host: string | undefined
10341034

1035+
/**
1036+
* Hostnames the dev server accepts in the `Host` header, in addition to `localhost`, `*.localhost` and IP addresses.
1037+
*
1038+
* An entry with a leading `.` also allows its subdomains. Set to `true` to allow any host.
1039+
*
1040+
* Includes `devServer.host` and `vite.server.allowedHosts`.
1041+
*/
1042+
allowedHosts: string[] | true
1043+
10351044
/**
10361045
* Listening dev server URL.
10371046
*
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { applyDefaults } from 'untyped'
3+
4+
import { NuxtConfigSchema } from '../src/index.ts'
5+
import type { NuxtOptions } from '../src/index.ts'
6+
7+
async function resolveAllowedHosts (config: { devServer?: { host?: string, allowedHosts?: string[] | true }, vite?: { server?: { allowedHosts?: string[] | true } } }) {
8+
const result = await applyDefaults(NuxtConfigSchema, config) as unknown as NuxtOptions
9+
return result.devServer.allowedHosts
10+
}
11+
12+
describe('devServer.allowedHosts', () => {
13+
it('defaults to an empty list', async () => {
14+
expect(await resolveAllowedHosts({})).toEqual([])
15+
})
16+
17+
it('includes devServer.host and vite.server.allowedHosts', async () => {
18+
expect(await resolveAllowedHosts({
19+
devServer: { host: 'my-machine.lan', allowedHosts: ['.example.com'] },
20+
vite: { server: { allowedHosts: ['proxy.test', 'my-machine.lan'] } },
21+
})).toEqual(['.example.com', 'proxy.test', 'my-machine.lan'])
22+
})
23+
24+
it('includes devServer.host when no other option lists it', async () => {
25+
expect(await resolveAllowedHosts({ devServer: { host: 'my-machine.lan' } })).toEqual(['my-machine.lan'])
26+
})
27+
28+
it('allows any host when either option is true', async () => {
29+
expect(await resolveAllowedHosts({ devServer: { allowedHosts: true } })).toBe(true)
30+
expect(await resolveAllowedHosts({ vite: { server: { allowedHosts: true } } })).toBe(true)
31+
})
32+
})

‎packages/vite/src/vite.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ export const bundle: NuxtBuilder['bundle'] = async (nuxt) => {
269269
appType: 'custom',
270270
server: {
271271
middlewareMode: true,
272+
allowedHosts: nuxt.options.devServer.allowedHosts,
272273
watch: { ...nuxt.options.watchers.chokidar, ignored: [isIgnored, /[\\/]node_modules[\\/]/] },
273274
fs: {
274275
allow: allowDirs,
@@ -287,6 +288,10 @@ export const bundle: NuxtBuilder['bundle'] = async (nuxt) => {
287288
: viteConfig,
288289
)
289290

291+
if (Array.isArray(config.server?.allowedHosts)) {
292+
config.server.allowedHosts = [...new Set(config.server.allowedHosts)]
293+
}
294+
290295
// In build mode we explicitly override any vite options that vite is relying on
291296
// to detect whether to inject production or development code (such as HMR code)
292297
if (!nuxt.options.dev) {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { fileURLToPath } from 'node:url'
2+
import { mkdir, mkdtemp, realpath, rm, symlink, writeFile } from 'node:fs/promises'
3+
import { tmpdir } from 'node:os'
4+
import { join } from 'pathe'
5+
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
6+
import { buildNuxt, loadNuxt } from '@nuxt/kit'
7+
8+
const nuxtDir = fileURLToPath(new URL('../../nuxt', import.meta.url))
9+
10+
describe('vite dev server allowedHosts', () => {
11+
let rootDir: string
12+
13+
beforeAll(async () => {
14+
rootDir = await realpath(await mkdtemp(join(tmpdir(), 'nuxt-allowed-hosts-')))
15+
await mkdir(join(rootDir, 'app'), { recursive: true })
16+
await mkdir(join(rootDir, 'node_modules'), { recursive: true })
17+
await symlink(nuxtDir, join(rootDir, 'node_modules/nuxt'), 'dir')
18+
await writeFile(join(rootDir, 'app/app.vue'), '<template><div/></template>')
19+
await writeFile(join(rootDir, 'package.json'), '{}')
20+
}, 30000)
21+
22+
afterAll(async () => {
23+
await rm(rootDir, { recursive: true, force: true })
24+
})
25+
26+
it('should list each allowed host once', async () => {
27+
const nuxt = await loadNuxt({
28+
cwd: rootDir,
29+
dev: true,
30+
overrides: {
31+
devServer: { host: 'my-machine.lan' },
32+
vite: { server: { allowedHosts: ['proxy.test'] } },
33+
},
34+
})
35+
let allowedHosts: unknown
36+
nuxt.hook('vite:extend', (ctx) => {
37+
allowedHosts = ctx.config.server?.allowedHosts
38+
})
39+
40+
try {
41+
await buildNuxt(nuxt)
42+
expect(allowedHosts).toEqual(['proxy.test', 'my-machine.lan'])
43+
} finally {
44+
await nuxt.close()
45+
}
46+
}, 60000)
47+
})

0 commit comments

Comments
 (0)