Repository navigation
Description
Provide environment information
System: Windows 11
Node: 24.14.0
npmPackages:
@trpc/server: 11.29.5
@trpc/client: 11.29.5
eventsource: 3.0.7 (passed to httpSubscriptionLink)
Describe the bug
A subscription that yields undefined ends with an error when it is used over httpSubscriptionLink (SSE) without a transformer.
The server writes the event as the literal text data: undefined, because JSON.stringify(undefined) returns undefined. The client then calls JSON.parse('undefined'), which throws. The client closes the connection and the subscription ends with onError "undefined" is not valid JSON.
Raw event stream from the server for yield 1; yield undefined; yield 3:
event: connected
data: {}
data: 1
data: undefined
data: 3
event: return
data:
Why I think this is a bug and not a limit:
- The procedure types allow it:
subscription<$Output extends AsyncIterable<any, void, any>>. - With a transformer such as
superjsonthe same subscription works. The client getsonData 1,onData undefined,onData 3,onComplete. - The JSONL encoder in
jsonl.tshandlesundefinedon purpose (if (value === undefined) return [[]]). - A payload-less "something changed" signal (
yield;) is a natural thing to write.
Expected: onData(undefined), as with a transformer. If undefined is not meant to be supported, a clear error on the server would be better than a broken stream.
Link to reproduction
To reproduce
git clone https://github.com/Santhosh-x23/trpc-sse-undefined-repro
cd trpc-sse-undefined-repro
npm install
npm start # fails: onError "undefined" is not valid JSON
npm run start:superjson # worksThe core of it:
const appRouter = t.router({
signal: t.procedure.subscription(async function* () {
yield 1;
yield undefined;
yield 3;
}),
});Additional information
Where it happens (packages/server/src/unstable-core-do-not-import/stream/sse.ts):
- Producer:
chunk.data = JSON.stringify(serialize(chunk.data)). With the default identityserialize,undefinedbecomesundefinedand is written asdata: undefined. - Consumer:
deserialize(JSON.parse(msg.data))in the'message'listener. The thrown error is caught,eventSource.close()runs and the stream errors.
Possible fixes, for you to choose between:
- Write an empty
data:forundefinedand have the consumer map an emptydatatoundefined. This matches thesuperjsonresult, but an old client talking to a new server still fails. - Write
data: null. Old clients work, but the client seesnullinstead ofundefined.
I'm happy to send a PR with a regression test once you say which behavior you want.
AI disclosure: I used Claude (Anthropic) to help investigate this and draft the report. I ran the reproduction myself and the output above is real.
👨👧👦 Contributing
- 🙋♂️ Yes, I'd be down to file a PR fixing this bug!
Activity
fix(server): encode `undefined` subscription values in SSE stream (tr…
github-actions commentedon Nov 24, 2026
This issue has been locked because we are very unlikely to see comments on closed issues. If you are running into a similar issue, please create a new issue. Thank you.