Skip to content

Events never show to the browser when using a ReadableStream with the text/event-stream content type #3232

@hugo

Description

@hugo

What version of Remix are you using?

1.5.0

Steps to Reproduce

Create an app using @remix-run/serve that returns a ReadableStream body in a loader, with the content-type text/event-stream. Send events.

On the client side connect to this endpoint using an EventSource.

See this reproduction repository for a full example.

The issue appears to be compression in the Express server. If you cURL the endpoint you'll see events streaming as expected:

curl -N http://localhost:3000/api/events
data: asdfghjkl

data: asdfghjkl

This pointed me to the underlying problem. cURL doesn't do compression by default. If you enable it, with curl --compressed you'll also have a connection that hangs. I'm not sure why but the compression middleware in Express borks streaming responses somehow.

If you edit the "@remix-run/serve" server to disable compression for "text/event-stream" content types, things work as expected:

app.use(compression({filter: (req) => req.headers['content-type']?.startsWith('text/event-stream')}))

(n.b. there may be a preferable way to do this, I'm not sure.)


While I'm here I should just add that this new streaming functionality is delightful.

Expected Behavior

Events would start to appear via the EventSource connection.

Actual Behavior

No events appear.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions