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'; import os from 'os';
function getLocalDevOrigins() { function getPermittedDevOrigins() {
const origins = ['192.168.1.32', 'localhost', '127.0.0.1']; const origins = new Set([
'localhost',
'127.0.0.1',
'0.0.0.0',
'*.local',
'*.lan',
'*.internal',
'*.home',
'*.nip.io',
'*.sslip.io',
]);
try { try {
// 1. Automatically register all host network interfaces (IPv4 & IPv6)
const interfaces = os.networkInterfaces(); const interfaces = os.networkInterfaces();
for (const name of Object.keys(interfaces)) { for (const name of Object.keys(interfaces)) {
for (const iface of interfaces[name] || []) { for (const iface of interfaces[name] || []) {
if (!iface.internal && iface.family === 'IPv4') { if (iface.address) {
origins.push(iface.address); origins.add(iface.address);
} }
} }
} }
} catch (e) { } 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} */ /** @type {import('next').NextConfig} */
const nextConfig = { const nextConfig = {
reactStrictMode: true, reactStrictMode: true,
allowedDevOrigins: getLocalDevOrigins(), allowedDevOrigins: getPermittedDevOrigins(),
images: { images: {
unoptimized: true, unoptimized: true,
}, },
experimental: { experimental: {
serverComponentsExternalPackages: ['sharp'], 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() { async redirects() {
try { try {
const fs = await import('fs'); const fs = await import('fs');
+1 -1
View File
@@ -3,7 +3,7 @@
"version": "1.0.0", "version": "1.0.0",
"private": true, "private": true,
"scripts": { "scripts": {
"dev": "next dev -p 3000", "dev": "next dev -H 0.0.0.0 -p 3000",
"build": "next build", "build": "next build",
"start": "next start -p 3000", "start": "next start -p 3000",
"test": "vitest run" "test": "vitest run"