Skip to content

Commit 4509643

Browse files
committed
fix(dev): skip sending headers if they are already sent
1 parent e15ea22 commit 4509643

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

packages/nuxi/src/commands/dev.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,13 @@ async function createDevHandler(cwd: string, nuxtOptions: NuxtOptions, listenOpt
229229
},
230230
// Loading handler
231231
async (req, res) => {
232+
if (res.headersSent) {
233+
if (!res.writableEnded) {
234+
res.end()
235+
}
236+
return
237+
}
238+
232239
res.statusCode = 503
233240
res.setHeader('Content-Type', 'text/html')
234241
res.setHeader('Cache-Control', 'no-store')

packages/nuxi/src/dev/error.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ import type { IncomingMessage, ServerResponse } from 'node:http'
22
import { Youch } from 'youch'
33

44
export async function renderError(req: IncomingMessage, res: ServerResponse, error: unknown) {
5+
if (res.headersSent) {
6+
if (!res.writableEnded) {
7+
res.end()
8+
}
9+
return
10+
}
11+
512
const youch = new Youch()
613
res.statusCode = 500
714
res.setHeader('Content-Type', 'text/html')

packages/nuxi/src/dev/fetch.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,15 @@ async function sendWebResponse(res: ServerResponse, webResponse: Response | Node
185185
if (webResponse.body) {
186186
// handle node readable stream (from Windows named pipe fetch)
187187
if (webResponse.body instanceof Readable) {
188-
await pipeline(webResponse.body, res, { end: true })
188+
try {
189+
await pipeline(webResponse.body, res, { end: true })
190+
}
191+
catch (error) {
192+
if (!res.writableEnded) {
193+
res.end()
194+
}
195+
throw error
196+
}
189197
return
190198
}
191199

@@ -202,6 +210,14 @@ async function sendWebResponse(res: ServerResponse, webResponse: Response | Node
202210
}
203211
}
204212
}
213+
catch (error) {
214+
// If streaming fails, clean up and end the response
215+
reader.releaseLock()
216+
if (!res.writableEnded) {
217+
res.end()
218+
}
219+
throw error
220+
}
205221
finally {
206222
reader.releaseLock()
207223
}

packages/nuxi/src/dev/utils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,13 @@ export class NuxtDevServer extends EventEmitter<DevServerEventMap> {
159159
}
160160

161161
async _renderLoadingScreen(req: IncomingMessage, res: ServerResponse) {
162+
if (res.headersSent) {
163+
if (!res.writableEnded) {
164+
res.end()
165+
}
166+
return
167+
}
168+
162169
res.statusCode = 503
163170
res.setHeader('Content-Type', 'text/html')
164171
const loadingTemplate = this.options.loadingTemplate

0 commit comments

Comments
 (0)