Skip to content
Myr

fix(jsx): replace Suspense and ErrorBoundary content across newlines (#5380)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix(jsx): replace Suspense and ErrorBoundary content across newlines The buffer-phase replacement regexes for Suspense and ErrorBoundary used '.*?', which cannot match across newlines. When a Suspense fallback or an ErrorBoundary region contained a newline (e.g. in the writeSSE Stringify phase), the replacement was a silent no-op: the resolved content was dropped and the template markers stayed in the output. Use [\s\S] so the regions match regardless of line breaks. * test(sse): cover sibling and nested multiline Suspense boundaries Two controls for the buffer-phase replacement: sibling Suspense boundaries with multiline fallbacks must each replace only their own region, and nested Suspense with multiline fallback and content must resolve through both levels. Both read the stream to completion. * refactor: use the `s` flag in boundary regexes and drop the no-op `.*?` from ErrorBoundary ---------

authored by Neil Daquioag · committed by GitHub · parent 098e119 · Browse files · 3 files+129 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: ndycode <ndycode@users.noreply.github.com>
  • Co-authored-by: Yusuke Wada <yusuke@kamawada.com>

Diff stats

3 files+129 added−3 removed

  • tests:+127 added−1 removed
  • other:+2 added−2 removed

3 files+129 added−3 removed

modified:src/helper/streaming/sse.test.tsx+127 added-1 removed
1/** @jsxImportSource ../../jsx */2import { Context } from '../../context'3import { ErrorBoundary } from '../../jsx'3import { ErrorBoundary, Suspense } from '../../jsx'4import { streamSSE } from '.'56describe('SSE Streaming helper', () => {7 const req = new Request('http://localhost/')
263 unmodified lines
271 const decodedValue = decoder.decode(value)272 expect(decodedValue).toBe('data: <div>Error</div>\n\n')273 })274275 it('Check streamSSE Response via Suspense with a multi-line fallback', async () => {276 const AsyncComponent = async () => Promise.resolve(<div>Async Hello</div>)277 const res = streamSSE(c, async (stream) => {278 await stream.writeSSE({279 data: (280 <Suspense fallback={<div>{'Loading...\nPlease wait'}</div>}>281 <AsyncComponent />282 </Suspense>283 ),284 })285 })286287 expect(res).not.toBeNull()288 expect(res.status).toBe(200)289290 if (!res.body) {291 throw new Error('Body is null')292 }293 const reader = res.body.getReader()294 const decoder = new TextDecoder()295 const { value } = await reader.read()296 const decodedValue = decoder.decode(value)297 expect(decodedValue).toBe('data: <div>Async Hello</div>\n\n')298 })299300 it('Check streamSSE Response via ErrorBoundary with multi-line content', async () => {301 const AsyncComponent = async () => Promise.resolve(<div>{'Async\nHello'}</div>)302 const res = streamSSE(c, async (stream) => {303 await stream.writeSSE({304 data: (305 <ErrorBoundary fallback={<div>Error</div>}>306 <Suspense fallback={<div>Loading...</div>}>307 <AsyncComponent />308 </Suspense>309 </ErrorBoundary>310 ),311 })312 })313314 expect(res).not.toBeNull()315 expect(res.status).toBe(200)316317 if (!res.body) {318 throw new Error('Body is null')319 }320 const reader = res.body.getReader()321 const decoder = new TextDecoder()322 const { value } = await reader.read()323 const decodedValue = decoder.decode(value)324 expect(decodedValue).toBe('data: <div>Async\ndata: Hello</div>\n\n')325 })326327 it('Check streamSSE Response via sibling Suspense boundaries with multi-line fallbacks', async () => {328 const One = async () => Promise.resolve(<div>One</div>)329 const Two = async () => Promise.resolve(<div>Two</div>)330 const res = streamSSE(c, async (stream) => {331 await stream.writeSSE({332 data: (333 <div>334 <Suspense fallback={<div>{'Loading1...\nwait'}</div>}>335 <One />336 </Suspense>337 <Suspense fallback={<div>{'Loading2...\nwait'}</div>}>338 <Two />339 </Suspense>340 </div>341 ),342 })343 })344345 expect(res).not.toBeNull()346 expect(res.status).toBe(200)347348 if (!res.body) {349 throw new Error('Body is null')350 }351 const reader = res.body.getReader()352 const decoder = new TextDecoder()353 let decodedValue = ''354 for (;;) {355 const { done, value } = await reader.read()356 if (done) {357 break358 }359 decodedValue += decoder.decode(value)360 }361 expect(decodedValue).toBe('data: <div><div>One</div><div>Two</div></div>\n\n')362 })363364 it('Check streamSSE Response via nested Suspense boundaries with multi-line fallback and content', async () => {365 const Inner = async () => Promise.resolve(<div>{'Inner\nContent'}</div>)366 const Outer = async () =>367 Promise.resolve(368 <Suspense fallback={<div>{'B\nb'}</div>}>369 <Inner />370 </Suspense>371 )372 const res = streamSSE(c, async (stream) => {373 await stream.writeSSE({374 data: (375 <Suspense fallback={<div>{'A\na'}</div>}>376 <Outer />377 </Suspense>378 ),379 })380 })381382 expect(res).not.toBeNull()383 expect(res.status).toBe(200)384385 if (!res.body) {386 throw new Error('Body is null')387 }388 const reader = res.body.getReader()389 const decoder = new TextDecoder()390 let decodedValue = ''391 for (;;) {392 const { done, value } = await reader.read()393 if (done) {394 break395 }396 decodedValue += decoder.decode(value)397 }398 expect(decodedValue).toBe('data: <div>Inner\ndata: Content</div>\n\n')399 })400401 it('Check streamSSE handles \\r (CR) line ending correctly', async () => {402 const res = streamSSE(c, async (stream) => {403 await stream.writeSSE({404 data: 'Line1\rLine2',
153 unmodified lines
modified:src/jsx/components.ts+1 added-1 removed
124 unmodified lines
125 // a Promise from `resolveChildEarly` skipped the `catch`, so the deferred126 // `catchCallback` would otherwise capture too late.127 getResume()128 const index = errorBoundaryCounter++129 const replaceRe = RegExp(`(<template id="E:${index}"></template>.*?)(.*?)(<!--E:${index}-->)`)129 const replaceRe = RegExp(`(<template id="E:${index}"></template>)(.*?)(<!--E:${index}-->)`, 's')130 let caught = false131 const catchCallback = async ({ error, buffer }: { error: Error; buffer?: [string] }) => {132 if (caught) {133 return ''
124 unmodified lines
modified:src/jsx/streaming.ts+1 added-1 removed
107 unmodified lines
108 htmlArray = htmlArray.flat()109 const content = htmlArray.join('')110 if (buffer) {111 buffer[0] = buffer[0].replace(112 new RegExp(`<template id="H:${index}"></template>.*?<!--/\\$-->`),112 new RegExp(`<template id="H:${index}"></template>.*?<!--/\\$-->`, 's'),113 () => content114 )115 }116 let html = buffer
115 unmodified lines