From e540b6b68a98022479f49c585a36b4afc2e651ca Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Mon, 10 Oct 2022 08:28:55 -0400 Subject: [PATCH] Try a counter approach --- packages/astro/src/runtime/server/jsx.ts | 46 +++++++++++++++++------- 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/packages/astro/src/runtime/server/jsx.ts b/packages/astro/src/runtime/server/jsx.ts index 5ae731a2b..a4ee340e8 100644 --- a/packages/astro/src/runtime/server/jsx.ts +++ b/packages/astro/src/runtime/server/jsx.ts @@ -1,6 +1,6 @@ /* eslint-disable no-console */ import { SSRResult } from '../../@types/astro.js'; -import { AstroJSX, isVNode } from '../../jsx-runtime/index.js'; +import { AstroJSX, AstroVNode, isVNode } from '../../jsx-runtime/index.js'; import { escapeHTML, HTMLString, @@ -15,7 +15,24 @@ import type { ComponentIterable } from './render/component'; const ClientOnlyPlaceholder = 'astro-client-only'; -const skipAstroJSXCheck = new WeakMap<() => any, null | Error>(); +const skipAstroJSXCheck = new WeakMap<() => any, number>(); +function addSkip(vnode: AstroVNode) { + if(typeof vnode.type === 'function') { + skipAstroJSXCheck.set(vnode.type, skipCount(vnode) + 1); + } +} +function skipCount(vnode: AstroVNode): number { + if(typeof vnode.type === 'function') { + return skipAstroJSXCheck.get(vnode.type) || 0; + } + return NaN; +} +function deleteSkips(vnode: AstroVNode) { + if(typeof vnode.type === 'function') { + skipAstroJSXCheck.delete(vnode.type); + } +} + let originalConsoleError: any; let consoleFilterRefs = 0; @@ -68,32 +85,35 @@ Did you forget to import the component or is it possible there is a typo?`); if (vnode.type) { if (typeof vnode.type === 'function' && (vnode.type as any)['astro:renderer']) { - skipAstroJSXCheck.set(vnode.type, null); + addSkip(vnode); } if (typeof vnode.type === 'function' && vnode.props['server:root']) { const output = await vnode.type(vnode.props ?? {}); return await renderJSX(result, output); } if (typeof vnode.type === 'function') { - if(skipAstroJSXCheck.has(vnode.type)) { - const error = skipAstroJSXCheck.get(vnode.type); - if(error) { - throw error; - } - } else { + if(skipCount(vnode) === 0 || skipCount(vnode) > 2) { useConsoleFilter(); try { const output = await vnode.type(vnode.props ?? {}); + let renderResult: any; if (output && output[AstroJSX]) { - return await renderJSX(result, output); + renderResult = await renderJSX(result, output); } else if (!output) { - return await renderJSX(result, output); + renderResult = await renderJSX(result, output); } + deleteSkips(vnode); + return renderResult; } catch (e: any) { - skipAstroJSXCheck.set(vnode.type, e); + if(skipCount(vnode) > 2) { + throw e; + } + addSkip(vnode); } finally { finishUsingConsoleFilter(); } + } else { + addSkip(vnode); } } @@ -158,8 +178,10 @@ Did you forget to import the component or is it possible there is a typo?`); for await (const chunk of output) { parts.append(chunk, result); } + deleteSkips(vnode); return markHTMLString(parts.toString()); } else { + deleteSkips(vnode); return markHTMLString(output); } }