Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add compatibility mode option to disable Content-Type header checking #339

Closed

Conversation

alh-solidatus
Copy link

This was a breaking change with v3 that's hard to work around when you have broken APIs. I've added a config option to allow use of this library where it was possible to do so before.

Add relevant information to migration guide so users are aware of the breaking change.

…cking

Add relevant information to migration guide so users are aware of the breaking change.
@alh-solidatus
Copy link
Author

@rexxars Is this something you would consider merging?

@rexxars
Copy link
Member

rexxars commented Feb 20, 2025

Hi, apologies for the late response.

No, unfortunately I am not willing to add this to the core module since it does not align with the spec, and the implementations in browsers. Both Chrome and Firefox will reject the request if it has the wrong Content-Type, for example.

However, you should be able to work around this by specifying a custom fetch function that adds/modifies the Content-Type before returning (untested code, but think this should work):

const es = new EventSource("/my-non-spec-sse-endpoint", {
  fetch: async (input, init) => {
    const response = await fetch(input, init);
    // Clone the original response, overriding the content-type with a spec-conforming one
    const newHeaders = new Headers(response.headers);
    newHeaders.set("Content-Type", "text/event-stream");
    return new Response(response.body, {
      status: response.status,
      statusText: response.statusText,
      headers: newHeaders,
    });
  },
});

es.addEventListener("open", () => {
  /* … */
});

@rexxars rexxars closed this Feb 20, 2025
@rexxars
Copy link
Member

rexxars commented Feb 20, 2025

Update: I tested the above code to ensure it works, and it does :)

Added it to the migration guide for future reference - thank you for raising!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants