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
Provenance
- Commit
2cf6004- Tree
3188151- Author
Taku Amano <taku@taaas.jp>- Committer
GitHub <noreply@github.com>- Parent
cf9a78d
2 files changed+150 added−11 removed
modified:src/jsx/components.test.tsx+109 added
84 unmodified lines85 86 errorBoundaryCounter--87 suspenseCounter--88 })89+90+ it('string content', async () => {91+ const html = <ErrorBoundary fallback={<Fallback />}>{'< ok >'}</ErrorBoundary>92+93+ expect((await resolveCallback(await html.toString())).toString()).toEqual('< ok >')94+95+ errorBoundaryCounter--96+ suspenseCounter--97+ })98+99+ it('error: string content', async () => {100+ const html = (101+ <ErrorBoundary fallback={'< error >'}>102+ <Component error={true} />103+ </ErrorBoundary>104+ )105+106+ expect((await resolveCallback(await html.toString())).toString()).toEqual('< error >')107+108+ errorBoundaryCounter--109+ suspenseCounter--110+ })111+112+ it('error: Promise<string> from fallback', async () => {113+ const html = (114+ <ErrorBoundary fallback={Promise.resolve('< error >')}>115+ <Component error={true} />116+ </ErrorBoundary>117+ )118+119+ expect((await resolveCallback(await html.toString())).toString()).toEqual('< error >')120+121+ errorBoundaryCounter--122+ suspenseCounter--123+ })124+125+ it('error: string content from fallbackRender', async () => {126+ const html = (127+ <ErrorBoundary fallbackRender={() => '< error >'}>128+ <Component error={true} />129+ </ErrorBoundary>130+ )131+132+ expect((await resolveCallback(await html.toString())).toString()).toEqual('< error >')133+134+ errorBoundaryCounter--135+ suspenseCounter--136+ })137+138+ it('error: Promise<string> from fallbackRender', async () => {139+ const html = (140+ <ErrorBoundary fallbackRender={() => Promise.resolve('< error >')}>141+ <Component error={true} />142+ </ErrorBoundary>143+ )144+145+ expect((await resolveCallback(await html.toString())).toString()).toEqual('< error >')146+147+ errorBoundaryCounter--148+ suspenseCounter--149+ })150 })151 152 describe('async', async () => {153 const Component = async ({ error }: { error?: boolean }) => {29 unmodified lines183 )184 185 suspenseCounter--186 })187+188+ it('error: string content', async () => {189+ const html = (190+ <ErrorBoundary fallback={'< error >'}>191+ <Component error={true} />192+ </ErrorBoundary>193+ )194+195+ expect((await resolveCallback(await html.toString())).toString()).toEqual('< error >')196+197+ suspenseCounter--198+ })199+200+ it('error: Promise<string> from fallback', async () => {201+ const html = (202+ <ErrorBoundary fallback={Promise.resolve('< error >')}>203+ <Component error={true} />204+ </ErrorBoundary>205+ )206+207+ expect((await resolveCallback(await html.toString())).toString()).toEqual('< error >')208+209+ suspenseCounter--210+ })211+212+ it('error: string content from fallbackRender', async () => {213+ const html = (214+ <ErrorBoundary fallbackRender={() => '< error >'}>215+ <Component error={true} />216+ </ErrorBoundary>217+ )218+219+ expect((await resolveCallback(await html.toString())).toString()).toEqual('< error >')220+221+ suspenseCounter--222+ })223+224+ it('error: Promise<string> from fallbackRender', async () => {225+ const html = (226+ <ErrorBoundary fallbackRender={() => Promise.resolve('< error >')}>227+ <Component error={true} />228+ </ErrorBoundary>229+ )230+231+ expect((await resolveCallback(await html.toString())).toString()).toEqual('< error >')232+233+ suspenseCounter--234+ })235 })236 237 describe('async : nested', async () => {238 const handlers: Record<number, { resolve: (value: unknown) => void; reject: () => void }> = {}453 unmodified linesmodified:src/jsx/components.ts+41 added-11 removed
1 import { raw } from '../helper/html'2 import type { HtmlEscapedCallback, HtmlEscapedString } from '../utils/html'3 import { HtmlEscapedCallbackPhase, resolveCallback } from '../utils/html'4+import { jsx, Fragment } from './base'5 import { DOM_RENDERER } from './constants'6 import { useContext } from './context'7 import { ErrorBoundary as ErrorBoundaryDomRenderer } from './dom/components'8 import type { HasRenderToDom } from './dom/render'16 unmodified lines25 }26 }27 }28 29+const 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+}43+44 export type ErrorHandler = (error: Error) => void45 export type FallbackRender = (error: Error) => Child46 47 /**18 unmodified lines66 67 const nonce = useContext(StreamingContext)?.scriptNonce68 69 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 }101 102 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 }111 84- 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 buffer105 unmodified lines224 .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