From f62aa5d5dbbcd1a157bc66496cbbd00c9bf3b61f Mon Sep 17 00:00:00 2001 From: Gurkirat Khaira <777gurkirat@gmail.com> Date: Tue, 9 Jun 2026 20:04:38 -0400 Subject: [PATCH] fix(windows): add Express path-rewriting middleware for Next.js static exports --- main/kds-server.ts | 37 +++++++++++++++++++++++++++++++++++++ main/server.ts | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/main/kds-server.ts b/main/kds-server.ts index fb3dd95..b5601d5 100644 --- a/main/kds-server.ts +++ b/main/kds-server.ts @@ -33,6 +33,27 @@ function getStaticDir(): string | null { return null; } +/** + * Helper to rewrite dotted Next.js static segment file requests to nested paths on Windows. + * E.g., /products/__next.!KGRhc2hib2FyZCk.products.__PAGE__.txt -> /products/__next.!KGRhc2hib2FyZCk/products/__PAGE__.txt + */ +function rewriteNextExportPath(reqPath: string): string { + const nextIndex = reqPath.indexOf('__next.'); + if (nextIndex === -1) return reqPath; + + const prefix = reqPath.substring(0, nextIndex + '__next.'.length); + const rest = reqPath.substring(nextIndex + '__next.'.length); + + const lastDotIndex = rest.lastIndexOf('.'); + if (lastDotIndex === -1) return reqPath; + + const namePart = rest.substring(0, lastDotIndex); + const extPart = rest.substring(lastDotIndex); + + const rewrittenName = namePart.replace(/\./g, '/'); + return prefix + rewrittenName + extPart; +} + export function startKdsServer(): Promise { return new Promise((resolve, reject) => { const app: Express = express(); @@ -171,6 +192,22 @@ export function startKdsServer(): Promise { const staticDir = getStaticDir(); if (staticDir) { console.log(`[KDS Server] Serving static files from: ${staticDir}`); + + // Middleware to patch Windows-specific Next.js static export path nesting + app.use((req: Request, res: Response, next: NextFunction) => { + if (req.path.includes('__next.')) { + const originalPath = req.path; + const rewritten = rewriteNextExportPath(originalPath); + if (rewritten !== originalPath) { + const fullPath = path.join(staticDir, rewritten); + if (fs.existsSync(fullPath)) { + req.url = rewritten; + } + } + } + next(); + }); + app.use(express.static(staticDir, { index: false })); // Redirect root to standalone KDS diff --git a/main/server.ts b/main/server.ts index 76d709c..ccf7bc7 100644 --- a/main/server.ts +++ b/main/server.ts @@ -41,6 +41,27 @@ function getFrontendDir(): string | null { return null; } +/** + * Helper to rewrite dotted Next.js static segment file requests to nested paths on Windows. + * E.g., /products/__next.!KGRhc2hib2FyZCk.products.__PAGE__.txt -> /products/__next.!KGRhc2hib2FyZCk/products/__PAGE__.txt + */ +function rewriteNextExportPath(reqPath: string): string { + const nextIndex = reqPath.indexOf('__next.'); + if (nextIndex === -1) return reqPath; + + const prefix = reqPath.substring(0, nextIndex + '__next.'.length); + const rest = reqPath.substring(nextIndex + '__next.'.length); + + const lastDotIndex = rest.lastIndexOf('.'); + if (lastDotIndex === -1) return reqPath; + + const namePart = rest.substring(0, lastDotIndex); + const extPart = rest.substring(lastDotIndex); + + const rewrittenName = namePart.replace(/\./g, '/'); + return prefix + rewrittenName + extPart; +} + export function startServer(): Promise { return new Promise((resolve, reject) => { app = express(); @@ -68,6 +89,22 @@ export function startServer(): Promise { const frontendDir = getFrontendDir(); if (frontendDir) { console.log(`[Server] Serving frontend from: ${frontendDir}`); + + // Middleware to patch Windows-specific Next.js static export path nesting + app.use((req: Request, res: Response, next: NextFunction) => { + if (req.path.includes('__next.')) { + const originalPath = req.path; + const rewritten = rewriteNextExportPath(originalPath); + if (rewritten !== originalPath) { + const fullPath = path.join(frontendDir, rewritten); + if (fs.existsSync(fullPath)) { + req.url = rewritten; + } + } + } + next(); + }); + app.use(express.static(frontendDir)); // SPA fallback: any unknown path returns index.html so Next.js