Skip to content

Commit 23932db

Browse files
committed
docs: add workaround for strict content-type checking to migration guide
1 parent 81f8cff commit 23932db

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

MIGRATION.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,33 @@ The default reconnect timeout is now 3 seconds - up from 1 second in v1/v2. This
8585
Redirect handling now matches Chrome/Safari. On disconnects, we will always reconnect to the _original_ URL. In v1/v2, only HTTP 307 would reconnect to the original, while 301 and 302 would both redirect to the _destination_.
8686

8787
While the _ideal_ behavior would be for 301 and 308 to reconnect to the redirect _destination_, and 302/307 to reconnect to the _original_ URL, this is not possible to do cross-platform (cross-origin requests in browsers do not allow reading location headers, and redirect handling will have to be done manually).
88+
89+
#### Strict checking of Content-Type header
90+
91+
The `Content-Type` header is now checked. It's value must be `text/event-stream` (or
92+
`text/event-stream; charset=utf-8`), and the connection will be failed otherwise.
93+
94+
To maintain the previous behaviour, you can use the `fetch` option to override the
95+
returned `Content-Type` header if your server does not send the required header:
96+
97+
```ts
98+
const es = new EventSource('https://my-server.com/sse', {
99+
fetch: async (input, init) => {
100+
const response = await fetch(input, init)
101+
102+
if (response.headers.get('content-type').startsWith('text/event-stream')) {
103+
// Valid header, forward response
104+
return response
105+
}
106+
107+
// Server did not respond with the correct content-type - override it
108+
const newHeaders = new Headers(response.headers)
109+
newHeaders.set('content-type', 'text/event-stream')
110+
return new Response(response.body, {
111+
status: response.status,
112+
statusText: response.statusText,
113+
headers: newHeaders,
114+
})
115+
},
116+
})
117+
```

0 commit comments

Comments
 (0)