Explorar o código

fix: SPA path resolution works in both dev and dist bundle

- Server tries client/dist/ first (dev), falls back to client/ (dist bundle)
- Fixes ENOENT error when serving from deployed dist/

Co-Authored-By: Claude <noreply@anthropic.com>
Oleg Panashchenko hai 2 semanas
pai
achega
b2525ea883
Modificáronse 1 ficheiros con 6 adicións e 4 borrados
  1. 6 4
      server/src/index.ts

+ 6 - 4
server/src/index.ts

@@ -1,5 +1,5 @@
 import express from 'express';
-import { readFileSync } from 'fs';
+import { readFileSync, existsSync } from 'fs';
 import { resolve, dirname } from 'path';
 import { fileURLToPath } from 'url';
 import type { WordBank } from '@wordle/shared';
@@ -34,9 +34,11 @@ for (const [path, router] of [...mountApi('/api', enBank), ...mountApi('/ru/api'
 }
 
 // Serve SPA
-const clientDist = process.env.STATIC_DIR
-  ? resolve(process.env.STATIC_DIR)
-  : resolve(__dirname, '../../client/dist');
+const clientDist = (() => {
+  if (process.env.STATIC_DIR) return resolve(process.env.STATIC_DIR);
+  const devPath = resolve(__dirname, '../../client/dist');
+  return existsSync(devPath) ? devPath : resolve(__dirname, '../../client');
+})();
 app.use(express.static(clientDist));
 app.get('/{*path}', (_req, res) => {
   res.sendFile(resolve(clientDist, 'index.html'));