Skip to content
Myr

Merge commit from fork

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

authored by Taku Amano · committed by GitHub · parent cf9a78d · Browse files · 2 files+150 added−11 removed
This commit

State

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

Diff stats

2 files+150 added−11 removed

  • tests:+109 added−0 removed
  • other:+41 added−11 removed

2 files+150 added−11 removed

modified:src/jsx/components.test.tsx+109 added
84 unmodified lines
8586 errorBoundaryCounter--87 suspenseCounter--88 })8990 it('string content', async () => {91 const html = <ErrorBoundary fallback={<Fallback />}>{'< ok >'}</ErrorBoundary>9293 expect((await resolveCallback(await html.toString())).toString()).toEqual('&lt; ok &gt;')9495 errorBoundaryCounter--96 suspenseCounter--97 })9899 it('error: string content', async () => {100 const html = (101 <ErrorBoundary fallback={'< error >'}>102 <Component error={true} />103 </ErrorBoundary>104 )105106 expect((await resolveCallback(await html.toString())).toString()).toEqual('&lt; error &gt;')107108 errorBoundaryCounter--109 suspenseCounter--110 })111112 it('error: Promise<string> from fallback', async () => {113 const html = (114 <ErrorBoundary fallback={Promise.resolve('< error >')}>115 <Component error={true} />116 </ErrorBoundary>117 )118119 expect((await resolveCallback(await html.toString())).toString()).toEqual('&lt; error &gt;')120121 errorBoundaryCounter--122 suspenseCounter--123 })124125 it('error: string content from fallbackRender', async () => {126 const html = (127 <ErrorBoundary fallbackRender={() => '< error >'}>128 <Component error={true} />129 </ErrorBoundary>130 )131132 expect((await resolveCallback(await html.toString())).toString()).toEqual('&lt; error &gt;')133134 errorBoundaryCounter--135 suspenseCounter--136 })137138 it('error: Promise<string> from fallbackRender', async () => {139 const html = (140 <ErrorBoundary fallbackRender={() => Promise.resolve('< error >')}>141 <Component error={true} />142 </ErrorBoundary>143 )144145 expect((await resolveCallback(await html.toString())).toString()).toEqual('&lt; error &gt;')146147 errorBoundaryCounter--148 suspenseCounter--149 })150 })151152 describe('async', async () => {153 const Component = async ({ error }: { error?: boolean }) => {
29 unmodified lines
183 )184185 suspenseCounter--186 })187188 it('error: string content', async () => {189 const html = (190 <ErrorBoundary fallback={'< error >'}>191 <Component error={true} />192 </ErrorBoundary>193 )194195 expect((await resolveCallback(await html.toString())).toString()).toEqual('&lt; error &gt;')196197 suspenseCounter--198 })199200 it('error: Promise<string> from fallback', async () => {201 const html = (202 <ErrorBoundary fallback={Promise.resolve('< error >')}>203 <Component error={true} />204 </ErrorBoundary>205 )206207 expect((await resolveCallback(await html.toString())).toString()).toEqual('&lt; error &gt;')208209 suspenseCounter--210 })211212 it('error: string content from fallbackRender', async () => {213 const html = (214 <ErrorBoundary fallbackRender={() => '< error >'}>215 <Component error={true} />216 </ErrorBoundary>217 )218219 expect((await resolveCallback(await html.toString())).toString()).toEqual('&lt; error &gt;')220221 suspenseCounter--222 })223224 it('error: Promise<string> from fallbackRender', async () => {225 const html = (226 <ErrorBoundary fallbackRender={() => Promise.resolve('< error >')}>227 <Component error={true} />228 </ErrorBoundary>229 )230231 expect((await resolveCallback(await html.toString())).toString()).toEqual('&lt; error &gt;')232233 suspenseCounter--234 })235 })236237 describe('async : nested', async () => {238 const handlers: Record<number, { resolve: (value: unknown) => void; reject: () => void }> = {}
453 unmodified lines
modified:src/jsx/components.ts+41 added-11 removed
1import { raw } from '../helper/html'2import type { HtmlEscapedCallback, HtmlEscapedString } from '../utils/html'3import { HtmlEscapedCallbackPhase, resolveCallback } from '../utils/html'4import { jsx, Fragment } from './base'5import { DOM_RENDERER } from './constants'6import { useContext } from './context'7import { ErrorBoundary as ErrorBoundaryDomRenderer } from './dom/components'8import type { HasRenderToDom } from './dom/render'
16 unmodified lines
25 }26 }27}2829const resolveChildEarly = (c: Child): HtmlEscapedString | Promise<HtmlEscapedString> => {30 if (c == null || typeof c === 'boolean') {31 return '' as HtmlEscapedString32 } else if (typeof c === 'string') {33 return c as HtmlEscapedString34 } else {35 const str = c.toString()36 if (!(str instanceof Promise)) {37 return raw(str)38 } else {39 return str as Promise<HtmlEscapedString>40 }41 }42}4344export type ErrorHandler = (error: Error) => void45export type FallbackRender = (error: Error) => Child4647/**
18 unmodified lines
6667 const nonce = useContext(StreamingContext)?.scriptNonce6869 let fallbackStr: string | undefined54 const fallbackRes = (error: Error): HtmlEscapedString => {70 const resolveFallbackStr = async () => {71 const awaitedFallback = await fallback72 if (typeof awaitedFallback === 'string') {73 fallbackStr = awaitedFallback74 } else {75 fallbackStr = await awaitedFallback?.toString()76 if (typeof fallbackStr === 'string') {77 // should not apply `raw` if fallbackStr is undefined, null, or boolean78 fallbackStr = raw(fallbackStr)79 }80 }81 }82 const fallbackRes = (error: Error): HtmlEscapedString | Promise<HtmlEscapedString> => {83 onError?.(error)56 return (fallbackStr || fallbackRender?.(error) || '').toString() as HtmlEscapedString84 return (fallbackStr ||85 (fallbackRender && jsx(Fragment, {}, fallbackRender(error) as HtmlEscapedString)) ||86 '') as HtmlEscapedString87 }88 let resArray: HtmlEscapedString[] | Promise<HtmlEscapedString[]>[] = []89 try {60 resArray = children.map((c) =>61 c == null || typeof c === 'boolean' ? '' : c.toString()62 ) as HtmlEscapedString[]90 resArray = children.map(resolveChildEarly) as unknown as HtmlEscapedString[]91 } catch (e) {64 fallbackStr = await fallback?.toString()92 await resolveFallbackStr()93 if (e instanceof Promise) {94 resArray = [95 e.then(() => childrenToString(children as Child[])).catch((e) => fallbackRes(e)),96 ] as Promise<HtmlEscapedString[]>[]97 } else {70 resArray = [fallbackRes(e as Error)]98 resArray = [fallbackRes(e as Error) as HtmlEscapedString]99 }100 }101102 if (resArray.some((res) => (res as {}) instanceof Promise)) {75 fallbackStr ||= await fallback?.toString()103 await resolveFallbackStr()104 const index = errorBoundaryCounter++105 const replaceRe = RegExp(`(<template id="E:${index}"></template>.*?)(.*?)(<!--E:${index}-->)`)106 const caught = false79 const catchCallback = ({ error, buffer }: { error: Error; buffer?: [string] }) => {107 const catchCallback = async ({ error, buffer }: { error: Error; buffer?: [string] }) => {108 if (caught) {109 return ''110 }11184 const fallbackResString = fallbackRes(error)112 const fallbackResString = await Fragment({113 children: fallbackRes(error),114 }).toString()115 if (buffer) {116 buffer[0] = buffer[0].replace(replaceRe, fallbackResString)117 }118 return buffer
105 unmodified lines
224 .catch((error) => catchCallback({ error, buffer }))225 },226 ])227 } else {198 return raw(resArray.join(''))228 return Fragment({ children: resArray as Child[] })229 }230}231;(ErrorBoundary as HasRenderToDom)[DOM_RENDERER] = ErrorBoundaryDomRenderer