Skip to content

Instantly share code, notes, and snippets.

@masterkain
Created May 10, 2023 17:30
Show Gist options
  • Save masterkain/a3edb77d932fceae51a449082ede65d7 to your computer and use it in GitHub Desktop.
Save masterkain/a3edb77d932fceae51a449082ede65d7 to your computer and use it in GitHub Desktop.
const http = require('http');
const path = require('path');
const { createServerHandler } = require('next/dist/server/lib/render-server-standalone');
const net = require('net');
const httpProxy = require('next/dist/compiled/http-proxy');
// Intercept http-proxy creation
const originalCreateProxy = httpProxy.createProxy;
httpProxy.createProxy = function (options, ...args) {
console.log('Proxy target:', options.target);
return originalCreateProxy.call(this, options, ...args);
};
const dir = path.join(__dirname);
process.env.NODE_ENV = 'production';
process.chdir(__dirname);
// Make sure commands gracefully respect termination signals (e.g. from Docker)
// Allow the graceful termination to be manually configurable
if (!process.env.NEXT_MANUAL_SIG_HANDLE) {
process.on('SIGTERM', () => process.exit(0));
process.on('SIGINT', () => process.exit(0));
}
const currentPort = parseInt(process.env.PORT, 10) || 3000;
// const hostname = process.env.HOSTNAME || 'localhost';
const hostname = 'localhost';
const keepAliveTimeout = parseInt(process.env.KEEP_ALIVE_TIMEOUT, 10);
const nextConfig = {
env: {},
webpack: null,
eslint: { ignoreDuringBuilds: false },
typescript: { ignoreBuildErrors: false, tsconfigPath: 'tsconfig.json' },
distDir: './.next',
cleanDistDir: true,
assetPrefix: '',
configOrigin: 'next.config.js',
useFileSystemPublicRoutes: true,
generateEtags: true,
pageExtensions: ['tsx', 'ts', 'jsx', 'js'],
poweredByHeader: true,
compress: true,
analyticsId: '',
images: {
deviceSizes: [640, 750, 828, 1080, 1200, 1920, 2048, 3840],
imageSizes: [16, 32, 48, 64, 96, 128, 256, 384],
path: '/_next/image',
loader: 'default',
loaderFile: '',
domains: [],
disableStaticImages: false,
minimumCacheTTL: 60,
formats: ['image/webp'],
dangerouslyAllowSVG: false,
contentSecurityPolicy: "script-src 'none'; frame-src 'none'; sandbox;",
contentDispositionType: 'inline',
remotePatterns: [],
unoptimized: false,
},
devIndicators: { buildActivity: true, buildActivityPosition: 'bottom-right' },
onDemandEntries: { maxInactiveAge: 15000, pagesBufferLength: 2 },
amp: { canonicalBase: '' },
basePath: '',
sassOptions: {},
trailingSlash: false,
i18n: null,
productionBrowserSourceMaps: false,
optimizeFonts: true,
excludeDefaultMomentLocales: true,
serverRuntimeConfig: {},
publicRuntimeConfig: {},
reactStrictMode: true,
httpAgentOptions: { keepAlive: true },
outputFileTracing: true,
staticPageGenerationTimeout: 60,
swcMinify: true,
output: 'standalone',
experimental: {
appDocumentPreloading: true,
clientRouterFilter: true,
clientRouterFilterRedirects: false,
fetchCacheKeyPrefix: '',
middlewarePrefetch: 'flexible',
optimisticClientCache: true,
manualClientBasePath: false,
legacyBrowsers: false,
newNextLinkBehavior: true,
cpus: 7,
sharedPool: true,
isrFlushToDisk: true,
workerThreads: false,
pageEnv: false,
optimizeCss: false,
nextScriptWorkers: false,
scrollRestoration: false,
externalDir: false,
disableOptimizedLoading: false,
gzipSize: true,
swcFileReading: true,
craCompat: false,
esmExternals: true,
appDir: true,
isrMemoryCacheSize: 52428800,
fullySpecified: false,
outputFileTracingRoot: '/app',
swcTraceProfiling: false,
forceSwcTransforms: false,
largePageDataBytes: 128000,
adjustFontFallbacks: false,
adjustFontFallbacksWithSizeAdjust: false,
typedRoutes: false,
instrumentationHook: false,
serverComponentsExternalPackages: ['@tremor/react'],
trustHostHeader: false,
},
configFileName: 'next.config.js',
};
process.env.__NEXT_PRIVATE_STANDALONE_CONFIG = JSON.stringify(nextConfig);
createServerHandler({
port: currentPort,
hostname,
dir,
conf: nextConfig,
})
.then((nextHandler) => {
console.log('Creating http server');
const server = http.createServer(async (req, res) => {
try {
console.log('Handling request', req.method, req.url);
// console.log('Request headers:', req.headers);
await nextHandler(req, res);
} catch (err) {
console.error('Failed to call handler', err);
res.statusCode = 500;
res.end('Internal Server Error');
}
});
if (!Number.isNaN(keepAliveTimeout) && Number.isFinite(keepAliveTimeout) && keepAliveTimeout >= 0) {
console.log('Setting keep alive timeout', keepAliveTimeout);
server.keepAliveTimeout = keepAliveTimeout;
}
server.listen(currentPort, async (err) => {
if (err) {
console.error('Failed to start server', err);
process.exit(1);
}
console.log('Listening on port', currentPort, 'url: http://' + hostname + ':' + currentPort);
});
})
.catch((err) => {
console.error('Failed to create next server handler', err);
process.exit(1);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment