Skip to content

Commit a7205f3

Browse files
committed
Builder-Vite: Fix logic related to setting allowedHosts when IP address used
1 parent 2d4280e commit a7205f3

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

code/builders/builder-vite/src/vite-server.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,17 @@ export async function createViteServer(options: Options, devServer: Server) {
2929
optimizeDeps: await getOptimizeDeps(commonCfg, options),
3030
};
3131

32-
const ipRegex = /^(?:\d{1,3}\.){3}\d{1,3}$|^(?:[a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}$/;
33-
34-
if (
35-
!(config.server.allowedHosts as string[])?.length &&
36-
options.host &&
37-
!ipRegex.test(options.host)
38-
) {
39-
config.server.allowedHosts = [options.host.toLowerCase()];
32+
if (options.host && !config.server.allowedHosts) {
33+
// Regex to match IPv4 and IPv6 addresses, simplified is good enough
34+
const ipRegex = /^(?:\d{1,3}\.){3}\d{1,3}$|^\[?([a-fA-F0-9:]+)\]?$/;
35+
36+
if (ipRegex.test(options.host)) {
37+
// If options.host is set to an IP address then allow all hosts
38+
config.server.allowedHosts = true;
39+
} else {
40+
// Otherwise, allow only the specified or default host
41+
config.server.allowedHosts = [options.host.toLowerCase()];
42+
}
4043
}
4144

4245
const finalConfig = await presets.apply('viteFinal', config, options);

0 commit comments

Comments
 (0)