From 2da8ce211db0433b36e96eb95a65ed43566939ad Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Wed, 27 Oct 2021 13:26:52 -0700 Subject: [PATCH] Next bugs (#1681) * fix(#1679): hoisted -`; +`, + }; return hydrationScript; } @@ -354,16 +355,23 @@ export function defineScriptVars(vars: Record) { return output; } -export async function renderToString(result: any, componentFactory: AstroComponentFactory, props: any, children: any) { +export async function renderToString(result: SSRResult, componentFactory: AstroComponentFactory, props: any, children: any) { const Component = await componentFactory(result, props, children); let template = await renderAstroComponent(Component); return template; } -export async function renderPage(result: any, Component: AstroComponentFactory, props: any, children: any) { +// Filter out duplicate elements in our set +const uniqueElements = (item: any, index: number, all: any[]) => { + const props = JSON.stringify(item.props); + const children = item.children; + return index === all.findIndex(i => JSON.stringify(i.props) === props && i.children == children) +} + +export async function renderPage(result: SSRResult, Component: AstroComponentFactory, props: any, children: any) { const template = await renderToString(result, Component, props, children); - const styles = Array.from(result.styles).map((style: any) => renderElement('style', style)); - const scripts = Array.from(result.scripts); + const styles = Array.from(result.styles).filter(uniqueElements).map((style) => renderElement('style', style)); + const scripts = Array.from(result.scripts).filter(uniqueElements).map((script) => renderElement('script', script)); return template.replace('', styles.join('\n') + scripts.join('\n') + ''); } @@ -379,18 +387,20 @@ export async function renderAstroComponent(component: InstanceType; children?: string }) { +function renderElement(name: string, { props: _props, children = '' }: SSRElement) { // Do not print `hoist`, `lang`, `global` - const { hoist: _0, lang: _1, global = false, 'data-astro-id': astroId, 'define:vars': defineVars, ...props } = _props; + const { lang: _, 'data-astro-id': astroId, 'define:vars': defineVars, ...props } = _props; if (defineVars) { if (name === 'style') { - if (global) { + if (props.global) { children = defineStyleVars(`:root`, defineVars) + '\n' + children; } else { children = defineStyleVars(`.astro-${astroId}`, defineVars) + '\n' + children; } + delete props.global; } if (name === 'script') { + delete props.hoist; children = defineScriptVars(defineVars) + '\n' + children; } }