Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions packages/router-core/src/ssr/transformStreamWithRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,17 @@ type ReadablePassthrough = {
destroyed: boolean
}

function createPassthrough() {
function createPassthrough(onCancel?: () => void) {
let controller: ReadableStreamDefaultController<any>
const encoder = new TextEncoder()
const stream = new ReadableStream({
start(c) {
controller = c
},
cancel() {
res.destroyed = true
onCancel?.()
},
})

const res: ReadablePassthrough = {
Expand Down Expand Up @@ -96,7 +100,11 @@ export function transformStreamWithRouter(
timeoutMs?: number
},
) {
const finalPassThrough = createPassthrough()
let stopListeningToInjectedHtml: (() => void) | undefined = undefined

const finalPassThrough = createPassthrough(() => {
stopListeningToInjectedHtml?.()
})
Comment thread
coderabbitai[bot] marked this conversation as resolved.
const textDecoder = new TextDecoder()

let isAppRendering = true as boolean
Expand Down Expand Up @@ -130,12 +138,9 @@ export function transformStreamWithRouter(
})

// Listen for any new injected HTML
const stopListeningToInjectedHtml = router.subscribe(
'onInjectedHtml',
(e) => {
handleInjectedHtml(e.promise)
},
)
stopListeningToInjectedHtml = router.subscribe('onInjectedHtml', (e) => {
handleInjectedHtml(e.promise)
})

function handleInjectedHtml(promise: Promise<string>) {
processingCount++
Expand Down Expand Up @@ -170,7 +175,7 @@ export function transformStreamWithRouter(
console.error('Error reading routerStream:', err)
finalPassThrough.destroy(err)
})
.finally(stopListeningToInjectedHtml)
.finally(() => stopListeningToInjectedHtml?.())

// Transform the appStream
readStream(appStream, {
Expand Down
Loading
Loading