Try a counter approach

This commit is contained in:
Matthew Phillips 2022-10-10 08:28:55 -04:00
parent 410cfe306c
commit e540b6b68a

View file

@ -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);
}
}