#!/usr/bin/env node
* Server wrapper for Next.js standalone mode
* This script generates a runtime config file from environment variables
* and injects them before starting the Next.js server.
*/
const fs = require('fs');
const path = require('path');
const env = process.env;
const runtimeConfig = {};
const EXTRA_CLIENT_VARS = ['LEARNHOUSE_PLATFORM_URL'];
Object.keys(env).forEach((key) => {
if (key.startsWith('NEXT_PUBLIC_') || EXTRA_CLIENT_VARS.includes(key)) {
runtimeConfig[key] = env[key];
process.env[key] = env[key];
}
});
const configPath = path.join(__dirname, 'runtime-config.json');
fs.writeFileSync(configPath, JSON.stringify(runtimeConfig, null, 2), 'utf8');
const publicDir = path.join(__dirname, 'public');
try {
if (!fs.existsSync(publicDir)) {
fs.mkdirSync(publicDir, { recursive: true });
}
const scriptPath = path.join(publicDir, 'runtime-config.js');
fs.writeFileSync(
scriptPath,
`window.__RUNTIME_CONFIG__ = ${JSON.stringify(runtimeConfig)};`,
'utf8'
);
} catch {
}
if (!process.env.HOSTNAME) {
process.env.HOSTNAME = '0.0.0.0';
}
if (!process.env.PORT) {
process.env.PORT = '3000';
}
require('./server.js');