From b958088c3dbabc43f698ff6684e6d54a8ec189f4 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Thu, 11 Nov 2021 11:55:50 -0500 Subject: [PATCH] Add display: contents for hydrated components (#1794) * Add display: contents for hydrated components * Only serialize boolean attrs that are data attrs * Adds a changeset --- .changeset/dull-steaks-dance.md | 5 +++++ .../astro/src/runtime/server/hydration.ts | 2 +- packages/astro/src/runtime/server/index.ts | 22 ++++++++++++++----- .../astro/src/vite-plugin-build-html/index.ts | 6 ++--- packages/astro/test/astro-styles-ssr.test.js | 9 ++++++++ .../src/components/ReactDynamic.jsx | 5 +++++ .../astro-styles-ssr/src/pages/index.astro | 2 ++ 7 files changed, 42 insertions(+), 9 deletions(-) create mode 100644 .changeset/dull-steaks-dance.md create mode 100644 packages/astro/test/fixtures/astro-styles-ssr/src/components/ReactDynamic.jsx diff --git a/.changeset/dull-steaks-dance.md b/.changeset/dull-steaks-dance.md new file mode 100644 index 000000000..3c95d998c --- /dev/null +++ b/.changeset/dull-steaks-dance.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Make astro-root be a display: contents element diff --git a/packages/astro/src/runtime/server/hydration.ts b/packages/astro/src/runtime/server/hydration.ts index dfd48abad..4a67825a9 100644 --- a/packages/astro/src/runtime/server/hydration.ts +++ b/packages/astro/src/runtime/server/hydration.ts @@ -112,7 +112,7 @@ export async function generateHydrateScript(scriptOptions: HydrateScriptOptions, `; const hydrationScript = { - props: { type: 'module' }, + props: { type: 'module', 'data-astro-component-hydration': true }, children: `import setup from 'astro/client/${hydrate}.js'; setup("${astroId}", {${metadata.hydrateArgs ? `value: ${JSON.stringify(metadata.hydrateArgs)}` : ''}}, async () => { ${hydrationSource} diff --git a/packages/astro/src/runtime/server/index.ts b/packages/astro/src/runtime/server/index.ts index 5e191d406..a4db5c9fb 100644 --- a/packages/astro/src/runtime/server/index.ts +++ b/packages/astro/src/runtime/server/index.ts @@ -222,7 +222,12 @@ export function addAttribute(value: any, key: string) { return ` ${key.slice(0, -5)}="${toAttributeString(serializeListValue(value))}"`; } - return ` ${key}="${toAttributeString(value)}"`; + // Boolean only needs the key + if(value === true && key.startsWith('data-')) { + return ` ${key}`; + } else { + return ` ${key}="${toAttributeString(value)}"`; + } } // Adds support for ` @@ -278,14 +283,21 @@ export async function renderPage(result: SSRResult, Component: AstroComponentFac props: { ...style.props, 'astro-style': true }, }) ); + let needsHydrationStyles = false; const scripts = Array.from(result.scripts) .filter(uniqueElements) - .map((script, i) => - renderElement('script', { + .map((script, i) => { + if('data-astro-component-hydration' in script.props) { + needsHydrationStyles = true; + } + return renderElement('script', { ...script, props: { ...script.props, 'astro-script': result._metadata.pathname + '/script-' + i }, - }) - ); + }); + }); + if(needsHydrationStyles) { + styles.push(renderElement('style', { props: { 'astro-style': true },children: 'astro-root, astro-fragment { display: contents; }' })) + } return template.replace('', styles.join('\n') + scripts.join('\n') + ''); } diff --git a/packages/astro/src/vite-plugin-build-html/index.ts b/packages/astro/src/vite-plugin-build-html/index.ts index 262e00ca0..114dcbc48 100644 --- a/packages/astro/src/vite-plugin-build-html/index.ts +++ b/packages/astro/src/vite-plugin-build-html/index.ts @@ -7,7 +7,7 @@ import parse5 from 'parse5'; import srcsetParse from 'srcset-parse'; import * as npath from 'path'; import { promises as fs } from 'fs'; -import { getAttribute, getTagName, insertBefore, remove, createScript, createElement, setAttribute } from '@web/parse5-utils'; +import { getAttribute, hasAttribute, getTagName, insertBefore, remove, createScript, createElement, setAttribute } from '@web/parse5-utils'; import { addRollupInput } from './add-rollup-input.js'; import { findAssets, findInlineScripts, findInlineStyles, getTextContent, isStylesheetLink } from './extract-assets.js'; import { render as ssrRender } from '../core/ssr/index.js'; @@ -109,7 +109,7 @@ export function rollupPluginAstroBuildHTML(options: PluginOptions): VitePlugin { let styles = ''; for (const node of findInlineStyles(document)) { - if (getAttribute(node, 'astro-style')) { + if (hasAttribute(node, 'astro-style')) { styles += getTextContent(node); } } @@ -374,7 +374,7 @@ export function rollupPluginAstroBuildHTML(options: PluginOptions): VitePlugin { // Page styles for