Skip to content

feat: Make ACCEPTS header parsing follow specification more closely#36

Merged
kibertoad merged 6 commits into
mainfrom
feat/spec-compliant-header-handling
Jun 9, 2026
Merged

feat: Make ACCEPTS header parsing follow specification more closely#36
kibertoad merged 6 commits into
mainfrom
feat/spec-compliant-header-handling

Conversation

@kibertoad

Copy link
Copy Markdown
Member

Per RFC 9110 §12.5.1 (https://www.rfc-editor.org/rfc/rfc9110#name-accept), a wildcard Accept header (/, text/*) or a missing Accept header means any media type is acceptable, including text/event-stream. The server gets to choose. @fastify/sse currently treats those headers as "not SSE" and falls through to the route's regular handler. For SSE-only routes the regular handler is the same handler that uses reply.sse, and reply.sse is undefined on the fallback path, so the request 500s with a cryptic TypeError: Cannot read properties of undefined (reading 'send').

That hits anything that doesn't send an explicit text/event-stream header: Postman defaults, curl defaults, fetch() defaults, plain navigations.

After this PR, routes can now declare their intent up front:

  • sse: 'only' - SSE-only route. Lenient gate per RFC: any spec-compliant Accept (text/event-stream, text/*, */*, missing) admits SSE. Clients that explicitly refuse SSE (Accept: application/json, or text/event-stream;q=0) get 406 Not Acceptable.
  • sse: 'dual' - Same handler serves both SSE and non-SSE clients. Strict gate: only an explicit text/event-stream token routes to SSE; everything else (including /) reaches the handler with reply.sse undefined, so the handler can serve a non-SSE response. This preserves the convention that wildcards prefer the richer non-SSE representation when the route offers a choice.
  • sse: true - Unchanged routing (back-compat). On the fallback path the plugin now rethrows with a clearer error if the handler trips on undefined reply.sse, naming sse: 'only' as the fix.

sse: true routes behave exactly as before - the existing fallback pattern, the existing tests, and the documented examples all keep working. New routes can opt into the explicit kinds without affecting old ones.

Safe for a minor bump pre-1.0: no behavioral change for any existing sse: true registration; only additive surface ('only', 'dual', the object form's kind field).

Checklist

@kibertoad kibertoad requested a review from a team May 11, 2026 23:27
Comment thread index.js Outdated
Comment thread index.js
@kibertoad kibertoad requested a review from mcollina May 12, 2026 11:50
@kibertoad

Copy link
Copy Markdown
Member Author

@mcollina do you think optimization was sufficient?

@kibertoad kibertoad requested a review from a team May 13, 2026 15:28
@kibertoad

Copy link
Copy Markdown
Member Author

@mcollina could you please rereview? hot path is now faster than it was before the PR

@kibertoad

Copy link
Copy Markdown
Member Author

@mcollina how can we proceed with that? or you prefer not to do this change altogether?

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Sorry, I lost all the notifications

@kibertoad kibertoad merged commit 96c6da5 into main Jun 9, 2026
23 of 32 checks passed
@kibertoad kibertoad deleted the feat/spec-compliant-header-handling branch June 9, 2026 12:33
@andokai

andokai commented Jun 19, 2026

Copy link
Copy Markdown

Hi, any sign of a release with these changes included?

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.

3 participants