feat(server): bind dev server to 0.0.0.0:3000 and permit all network origins with CORS

This commit is contained in:
2026-11-05 11:33:56 +05:30
parent 378f76df54
commit d20f98cb36
2 changed files with 40 additions and 8 deletions
+39 -7
View File
@@ -1,32 +1,64 @@
import os from 'os';
function getLocalDevOrigins() {
const origins = ['192.168.1.32', 'localhost', '127.0.0.1'];
function getPermittedDevOrigins() {
const origins = new Set([
'localhost',
'127.0.0.1',
'0.0.0.0',
'*.local',
'*.lan',
'*.internal',
'*.home',
'*.nip.io',
'*.sslip.io',
]);
try {
// 1. Automatically register all host network interfaces (IPv4 & IPv6)
const interfaces = os.networkInterfaces();
for (const name of Object.keys(interfaces)) {
for (const iface of interfaces[name] || []) {
if (!iface.internal && iface.family === 'IPv4') {
origins.push(iface.address);
if (iface.address) {
origins.add(iface.address);
}
}
}
} catch (e) {
// fallback gracefully
// ignore
}
return Array.from(new Set(origins));
// 2. Pre-permit entire private subnets (192.168.1.0/24, 192.168.0.0/24, 10.0.0.0/24)
for (let i = 1; i <= 254; i++) {
origins.add(`192.168.1.${i}`);
origins.add(`192.168.0.${i}`);
origins.add(`10.0.0.${i}`);
}
return Array.from(origins);
}
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
allowedDevOrigins: getLocalDevOrigins(),
allowedDevOrigins: getPermittedDevOrigins(),
images: {
unoptimized: true,
},
experimental: {
serverComponentsExternalPackages: ['sharp'],
},
async headers() {
return [
{
source: '/:path*',
headers: [
{ key: 'Access-Control-Allow-Origin', value: '*' },
{ key: 'Access-Control-Allow-Methods', value: 'GET,OPTIONS,PATCH,DELETE,POST,PUT' },
{ key: 'Access-Control-Allow-Headers', value: '*' },
],
},
];
},
async redirects() {
try {
const fs = await import('fs');
+1 -1
View File
@@ -3,7 +3,7 @@
"version": "1.0.0",
"private": true,
"scripts": {
"dev": "next dev -p 3000",
"dev": "next dev -H 0.0.0.0 -p 3000",
"build": "next build",
"start": "next start -p 3000",
"test": "vitest run"