Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion packages/vite/src/node/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,27 @@ export async function preview(
app.use(proxyMiddleware(httpServer, proxy, config))
}

app.use(compression())
// @polka/compression wraps response methods before it decides whether
// to actually compress. For SSE (`text/event-stream`) this wrapping breaks
// HTTP/2 stream lifecycle when the client aborts the connection.
// Skip installing the wrapper for SSE.
const compressionMiddleware = compression()

app.use((req, res, next) => {
const contentType = (res as any).getHeader?.('Content-Type') as
| string
| undefined

const accept = req.headers['accept'] || ''

const isSSE =
contentType === 'text/event-stream' ||
(typeof accept === 'string' && accept.includes('text/event-stream'))

if (isSSE) return next()

return compressionMiddleware(req, res, next)
})
// base
if (config.base !== '/') {
app.use(baseMiddleware(config.rawBase, false))
Expand Down
Loading