diff --git a/CHANGELOG.md b/CHANGELOG.md index 98ab860..e0cbc66 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ All notable changes to OpenHamClock will be documented in this file. - `.env` is auto-created from `.env.example` on first run - Settings won't be overwritten by git updates - Supports: CALLSIGN, LOCATOR, PORT, HOST, UNITS, TIME_FORMAT, THEME, LAYOUT +- **Auto-build on start** - `npm start` automatically builds the React frontend if needed - **Update script** - Easy updates for local/Pi installations (`./scripts/update.sh`) - Backs up config, pulls latest, rebuilds, preserves settings - **Network access configuration** - Set `HOST=0.0.0.0` to access from other devices diff --git a/README.md b/README.md index 2013040..e81ed0a 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ A modern, modular amateur radio dashboard built with React and Vite. This is the # Install dependencies npm install -# Start the server (auto-creates .env on first run) +# Start the server (auto-builds frontend and creates .env on first run) npm start # Edit .env with your callsign and grid locator @@ -18,14 +18,17 @@ npm start # Open http://localhost:3000 ``` -**That's it!** On first run, the server automatically creates a `.env` file from `.env.example`. Just edit it with your callsign and locator. +**That's it!** On first run: +- Frontend is automatically built (React app compiled to `dist/`) +- `.env` file is created from `.env.example` +- Just edit `.env` with your callsign and locator For development with hot reload: ```bash # Terminal 1: Backend API server node server.js -# Terminal 2: Frontend dev server +# Terminal 2: Frontend dev server with hot reload npm run dev ``` diff --git a/package.json b/package.json index ed16391..d91f39a 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,13 @@ { "name": "openhamclock", - "version": "3.7.0", + "version": "3.10.0", "description": "Amateur Radio Dashboard - A modern web-based HamClock alternative", "main": "server.js", "scripts": { "dev": "vite", "build": "vite build", "preview": "vite preview", + "prestart": "node -e \"const fs=require('fs'); if(!fs.existsSync('dist/index.html')){console.log('Building frontend...'); require('child_process').execSync('npm run build',{stdio:'inherit'})}\"", "start": "node server.js", "server": "node server.js", "test": "echo \"Tests passing\" && exit 0" diff --git a/public/index.html b/public/index.html new file mode 100644 index 0000000..8d6a48c --- /dev/null +++ b/public/index.html @@ -0,0 +1,82 @@ + + + + + + OpenHamClock - Build Required + + + +
+

📻 OpenHamClock

+

The frontend needs to be built before running.

+ +
+ npm install + npm run build + npm start +
+ +

Or use the quick start:

+
+ npm install && npm start +
+ +

+ If you're seeing this page, the build step was skipped.
+ Running npm start should auto-build if needed. +

+
+ + diff --git a/server.js b/server.js index 996af8b..e6773a2 100644 --- a/server.js +++ b/server.js @@ -192,17 +192,27 @@ function logErrorOnce(category, message) { return false; } -// Serve static files - use 'dist' in production (Vite build), 'public' in development -const staticDir = process.env.NODE_ENV === 'production' - ? path.join(__dirname, 'dist') - : path.join(__dirname, 'public'); -app.use(express.static(staticDir)); - -// Also serve public folder for any additional assets -if (process.env.NODE_ENV === 'production') { - app.use(express.static(path.join(__dirname, 'public'))); +// Serve static files +// dist/ contains the built React app (from npm run build) +// public/ contains the fallback page if build hasn't run +const distDir = path.join(__dirname, 'dist'); +const publicDir = path.join(__dirname, 'public'); + +// Check if dist/ exists (has index.html from build) +const distExists = fs.existsSync(path.join(distDir, 'index.html')); + +if (distExists) { + // Serve built React app from dist/ + app.use(express.static(distDir)); + console.log('[Server] Serving React app from dist/'); +} else { + // No build found - serve placeholder from public/ + console.log('[Server] ⚠️ No build found! Run: npm run build'); } +// Always serve public folder (for fallback and assets) +app.use(express.static(publicDir)); + // ============================================ // API PROXY ENDPOINTS // ============================================ @@ -3083,9 +3093,11 @@ app.get('/api/config', (req, res) => { // ============================================ app.get('*', (req, res) => { - const indexPath = process.env.NODE_ENV === 'production' - ? path.join(__dirname, 'dist', 'index.html') - : path.join(__dirname, 'public', 'index.html'); + // Try dist first (built React app), fallback to public (monolithic) + const distIndex = path.join(__dirname, 'dist', 'index.html'); + const publicIndex = path.join(__dirname, 'public', 'index.html'); + + const indexPath = fs.existsSync(distIndex) ? distIndex : publicIndex; res.sendFile(indexPath); });