Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

PM2 cluster mode support

Blue Mouse58c04b9963c7
diff --git a/web/ecosystem.config.cjs b/web/ecosystem.config.cjs
index da407ad73..aff00c2e5 100644
--- a/web/ecosystem.config.cjs
+++ b/web/ecosystem.config.cjs
@@ -2,8 +2,14 @@ module.exports = {
apps: [
{
name: 'Swetrix Frontend',
- script: './start.sh',
- interpreter: '/bin/bash',
+ cwd: __dirname,
+ script: './start.cjs',
+ exec_mode: 'cluster',
+ instances: 'max',
+ max_memory_restart: '1G',
+ env: {
+ NODE_ENV: 'production',
+ },
},
],
}
diff --git a/web/start.cjs b/web/start.cjs
new file mode 100644
index 000000000..4fa4527aa
--- /dev/null
+++ b/web/start.cjs
@@ -0,0 +1,32 @@
+#!/usr/bin/env node
+// Production launcher for the Swetrix frontend.
+// Loads .env into process.env, then hands off to @react-router/serve in-process.
+// Works as a standalone entry point AND with PM2 cluster mode (which only
+// supports Node.js scripts, not shell scripts).
+
+const fs = require('node:fs')
+const path = require('node:path')
+
+const envPath = path.join(__dirname, '.env')
+
+if (!fs.existsSync(envPath)) {
+ console.error('Error: .env file not found')
+ process.exit(1)
+}
+
+process.loadEnvFile(envPath)
+
+if (!process.env.NODE_ENV) {
+ process.env.NODE_ENV = 'production'
+}
+
+// @react-router/serve restricts subpath imports via its exports field,
+// so we resolve the CLI through the package.json (which is exported).
+const pkgJsonPath = require.resolve('@react-router/serve/package.json')
+const cliPath = path.join(path.dirname(pkgJsonPath), 'dist/cli.js')
+const buildPath = path.join(__dirname, 'build/server/index.js')
+
+// The CLI reads the build path from process.argv[2].
+process.argv = [process.argv[0], cliPath, buildPath]
+
+require(cliPath)
diff --git a/web/start.sh b/web/start.sh
deleted file mode 100755
index 5c801f40f..000000000
--- a/web/start.sh
+++ /dev/null
@@ -1,14 +0,0 @@
-#!/bin/bash
-# This script is used to start the production server on managed servers like Hetzner
-
-# Check if .env file exists
-if [ -f .env ]; then
- # Read .env file and format variables for inline usage
- ENV_VARS=$(cat .env | grep -v '^#' | xargs)
-
- # Run npm start with variables inline using env command
- env $ENV_VARS npm run start
-else
- echo "Error: .env file not found"
- exit 1
-fi