Read from ReadableStream on page load
Potentially stupid question but suppose I'm trying to do something like Claude where you ask a question from the home page then redirect to the chat page with a response immediately streaming in. How would I properly read from the stream following the rules of react?
My current setup is using tRPC to send a mutation on the home page and get back a
ReadableStream
for the AI response, then passing that stream to the chat page with router context. In the chat page I'll start reading from the stream in a useEffect
, but I don't think it's possible to make that effect pure? Since when you attach a Reader to the stream and start reading, you can't abort the read so a cleanup function that detaches the reader could cause an error where the reader tries to read from the stream when it's already detached.
I suspect my general approach to this could just be wrong, would love any suggestions.1 Reply
fair-rose•13mo ago