fix(server): gestion erreur WebSocket au build + rewrite URL protocol-relative dans le proxy
- api/websocket: capture asynchrone EADDRINUSE pour ne pas bloquer le build - proxy: réécrit aussi les URLs protocol-relatives (//localhost) dans le corps
This commit is contained in:
@@ -111,7 +111,8 @@ async function proxyRequest(req: NextRequest) {
|
||||
});
|
||||
}
|
||||
|
||||
// For text responses, rewrite localhost/internal URLs to the public URL
|
||||
// For text responses, rewrite localhost/internal URLs to the public URL.
|
||||
// Also handle protocol-relative URLs that some WordPress plugins/themes use.
|
||||
let body = await upstreamRes.text();
|
||||
const localBase = `http://${instance.node.tailscaleIp}:${instance.port}`;
|
||||
const localBaseHttps = `https://${instance.node.tailscaleIp}:${instance.port}`;
|
||||
@@ -119,6 +120,8 @@ async function proxyRequest(req: NextRequest) {
|
||||
const localLocalhostHttps = `https://localhost:${instance.port}`;
|
||||
const localLocalhostPlainHttp = `http://localhost`;
|
||||
const localLocalhostPlainHttps = `https://localhost`;
|
||||
const localLocalhostProtocolRelative = `//localhost`;
|
||||
const localTailscaleProtocolRelative = `//${instance.node.tailscaleIp}:${instance.port}`;
|
||||
|
||||
body = body
|
||||
.replaceAll(localBase, publicUrl)
|
||||
@@ -126,7 +129,9 @@ async function proxyRequest(req: NextRequest) {
|
||||
.replaceAll(localLocalhostHttp, publicUrl)
|
||||
.replaceAll(localLocalhostHttps, publicUrl)
|
||||
.replaceAll(localLocalhostPlainHttp, publicUrl)
|
||||
.replaceAll(localLocalhostPlainHttps, publicUrl);
|
||||
.replaceAll(localLocalhostPlainHttps, publicUrl)
|
||||
.replaceAll(localTailscaleProtocolRelative, publicUrl.replace(/^https?:/, ""))
|
||||
.replaceAll(localLocalhostProtocolRelative, publicUrl.replace(/^https?:/, ""));
|
||||
|
||||
return new Response(body, {
|
||||
status: upstreamRes.status,
|
||||
|
||||
@@ -5,8 +5,14 @@ const globalWss = globalThis as typeof globalThis & { __eduboxWss?: WebSocketSer
|
||||
|
||||
if (!globalWss.__eduboxWss) {
|
||||
try {
|
||||
globalWss.__eduboxWss = new WebSocketServer({ port: 3001 });
|
||||
initWebSocketServer(globalWss.__eduboxWss);
|
||||
const wss = new WebSocketServer({ port: 3001 });
|
||||
wss.on("error", (err) => {
|
||||
// Silently ignore EADDRINUSE during build/hot reload; the existing
|
||||
// server will keep handling agent connections.
|
||||
console.warn("WebSocket server error:", (err as Error).message);
|
||||
});
|
||||
globalWss.__eduboxWss = wss;
|
||||
initWebSocketServer(wss);
|
||||
} catch {
|
||||
// Port may be in use during build or hot reload
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user