From c77cd330192a0e5fb39e63e07dde42cc5a4d26b9 Mon Sep 17 00:00:00 2001 From: Michel Roegl-Brunner Date: Wed, 26 Nov 2025 08:57:45 +0100 Subject: [PATCH] Fix Next.js HMR WebSocket and static asset handling - Add WebSocket upgrade detection to only intercept /ws/script-execution - Pass all other WebSocket upgrades (including HMR) to Next.js handler - Ensure _next routes and static assets are properly handled by Next.js - Fixes 400 errors for Next.js HMR WebSocket connections - Fixes 403 errors for static assets by ensuring proper routing --- server.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/server.js b/server.js index 0e6371a..bfa0947 100644 --- a/server.js +++ b/server.js @@ -1159,12 +1159,22 @@ app.prepare().then(() => { const parsedUrl = parse(req.url || '', true); const { pathname, query } = parsedUrl; - if (pathname === '/ws/script-execution') { + // Check if this is a WebSocket upgrade request + const isWebSocketUpgrade = req.headers.upgrade === 'websocket'; + + // Only intercept WebSocket upgrades for /ws/script-execution + // Let Next.js handle all other WebSocket upgrades (like HMR) and all HTTP requests + if (isWebSocketUpgrade && pathname === '/ws/script-execution') { // WebSocket upgrade will be handled by the WebSocket server + // Don't call handle() for this path - let WebSocketServer handle it return; } - // Let Next.js handle all other requests including HMR + // Let Next.js handle all other requests including: + // - HTTP requests to /ws/script-execution (non-WebSocket) + // - WebSocket upgrades to other paths (like /_next/webpack-hmr) + // - All static assets (_next routes) + // - All other routes await handle(req, res, parsedUrl); } catch (err) { console.error('Error occurred handling', req.url, err);