diff --git a/.changeset/great-suns-pump.md b/.changeset/great-suns-pump.md new file mode 100644 index 000000000000..0d4a2541eefe --- /dev/null +++ b/.changeset/great-suns-pump.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix for passing children to client component when the component does not render them diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index d9356335284f..c2708ab1e6cb 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -213,6 +213,7 @@ export type GetStaticPathsResultKeyed = GetStaticPathsResult & { }; export interface HydrateOptions { + name: string; value?: string; } diff --git a/packages/astro/src/runtime/client/idle.ts b/packages/astro/src/runtime/client/idle.ts index c3914cfbe224..6a5bf15e141e 100644 --- a/packages/astro/src/runtime/client/idle.ts +++ b/packages/astro/src/runtime/client/idle.ts @@ -4,10 +4,26 @@ import type { GetHydrateCallback, HydrateOptions } from '../../@types/astro'; * Hydrate this component as soon as the main thread is free * (or after a short delay, if `requestIdleCallback`) isn't supported */ -export default async function onIdle(astroId: string, _options: HydrateOptions, getHydrateCallback: GetHydrateCallback) { +export default async function onIdle(astroId: string, options: HydrateOptions, getHydrateCallback: GetHydrateCallback) { const cb = async () => { const roots = document.querySelectorAll(`astro-root[uid="${astroId}"]`); - const innerHTML = roots[0].querySelector(`astro-fragment`)?.innerHTML ?? null; + if(roots.length === 0) { + throw new Error(`Unable to find the root for the component ${options.name}`); + } + + let innerHTML: string | null = null; + let fragment = roots[0].querySelector(`astro-fragment`); + if(fragment == null && roots[0].hasAttribute('tmpl')) { + // If there is no child fragment, check to see if there is a template. + // This happens if children were passed but the client component did not render any. + let template = roots[0].querySelector(`template[data-astro-template]`); + if(template) { + innerHTML = template.innerHTML; + template.remove(); + } + } else if(fragment) { + innerHTML = fragment.innerHTML; + } const hydrate = await getHydrateCallback(); for (const root of roots) { diff --git a/packages/astro/src/runtime/client/load.ts b/packages/astro/src/runtime/client/load.ts index c3fae489e3dd..73bb441cf319 100644 --- a/packages/astro/src/runtime/client/load.ts +++ b/packages/astro/src/runtime/client/load.ts @@ -3,9 +3,27 @@ import type { GetHydrateCallback, HydrateOptions } from '../../@types/astro'; /** * Hydrate this component immediately */ -export default async function onLoad(astroId: string, _options: HydrateOptions, getHydrateCallback: GetHydrateCallback) { +export default async function onLoad(astroId: string, options: HydrateOptions, getHydrateCallback: GetHydrateCallback) { const roots = document.querySelectorAll(`astro-root[uid="${astroId}"]`); - const innerHTML = roots[0].querySelector(`astro-fragment`)?.innerHTML ?? null; + if(roots.length === 0) { + throw new Error(`Unable to find the root for the component ${options.name}`); + } + + let innerHTML: string | null = null; + let fragment = roots[0].querySelector(`astro-fragment`); + if(fragment == null && roots[0].hasAttribute('tmpl')) { + // If there is no child fragment, check to see if there is a template. + // This happens if children were passed but the client component did not render any. + let template = roots[0].querySelector(`template[data-astro-template]`); + if(template) { + innerHTML = template.innerHTML; + template.remove(); + } + } else if(fragment) { + innerHTML = fragment.innerHTML; + } + + //const innerHTML = roots[0].querySelector(`astro-fragment`)?.innerHTML ?? null; const hydrate = await getHydrateCallback(); for (const root of roots) { diff --git a/packages/astro/src/runtime/client/media.ts b/packages/astro/src/runtime/client/media.ts index ef2f65260b1d..f5ae240c132a 100644 --- a/packages/astro/src/runtime/client/media.ts +++ b/packages/astro/src/runtime/client/media.ts @@ -5,7 +5,23 @@ import type { GetHydrateCallback, HydrateOptions } from '../../@types/astro'; */ export default async function onMedia(astroId: string, options: HydrateOptions, getHydrateCallback: GetHydrateCallback) { const roots = document.querySelectorAll(`astro-root[uid="${astroId}"]`); - const innerHTML = roots[0].querySelector(`astro-fragment`)?.innerHTML ?? null; + if(roots.length === 0) { + throw new Error(`Unable to find the root for the component ${options.name}`); + } + + let innerHTML: string | null = null; + let fragment = roots[0].querySelector(`astro-fragment`); + if(fragment == null && roots[0].hasAttribute('tmpl')) { + // If there is no child fragment, check to see if there is a template. + // This happens if children were passed but the client component did not render any. + let template = roots[0].querySelector(`template[data-astro-template]`); + if(template) { + innerHTML = template.innerHTML; + template.remove(); + } + } else if(fragment) { + innerHTML = fragment.innerHTML; + } const cb = async () => { const hydrate = await getHydrateCallback(); diff --git a/packages/astro/src/runtime/client/only.ts b/packages/astro/src/runtime/client/only.ts index c3fae489e3dd..cc4efb99d6a9 100644 --- a/packages/astro/src/runtime/client/only.ts +++ b/packages/astro/src/runtime/client/only.ts @@ -3,9 +3,25 @@ import type { GetHydrateCallback, HydrateOptions } from '../../@types/astro'; /** * Hydrate this component immediately */ -export default async function onLoad(astroId: string, _options: HydrateOptions, getHydrateCallback: GetHydrateCallback) { +export default async function onLoad(astroId: string, options: HydrateOptions, getHydrateCallback: GetHydrateCallback) { const roots = document.querySelectorAll(`astro-root[uid="${astroId}"]`); - const innerHTML = roots[0].querySelector(`astro-fragment`)?.innerHTML ?? null; + if(roots.length === 0) { + throw new Error(`Unable to find the root for the component ${options.name}`); + } + + let innerHTML: string | null = null; + let fragment = roots[0].querySelector(`astro-fragment`); + if(fragment == null && roots[0].hasAttribute('tmpl')) { + // If there is no child fragment, check to see if there is a template. + // This happens if children were passed but the client component did not render any. + let template = roots[0].querySelector(`template[data-astro-template]`); + if(template) { + innerHTML = template.innerHTML; + template.remove(); + } + } else if(fragment) { + innerHTML = fragment.innerHTML; + } const hydrate = await getHydrateCallback(); for (const root of roots) { diff --git a/packages/astro/src/runtime/client/visible.ts b/packages/astro/src/runtime/client/visible.ts index e06aabab4b16..101551ec0cd1 100644 --- a/packages/astro/src/runtime/client/visible.ts +++ b/packages/astro/src/runtime/client/visible.ts @@ -5,9 +5,25 @@ import type { GetHydrateCallback, HydrateOptions } from '../../@types/astro'; * We target the children because `astro-root` is set to `display: contents` * which doesn't work with IntersectionObserver */ -export default async function onVisible(astroId: string, _options: HydrateOptions, getHydrateCallback: GetHydrateCallback) { +export default async function onVisible(astroId: string, options: HydrateOptions, getHydrateCallback: GetHydrateCallback) { const roots = document.querySelectorAll(`astro-root[uid="${astroId}"]`); - const innerHTML = roots[0].querySelector(`astro-fragment`)?.innerHTML ?? null; + if(roots.length === 0) { + throw new Error(`Unable to find the root for the component ${options.name}`); + } + + let innerHTML: string | null = null; + let fragment = roots[0].querySelector(`astro-fragment`); + if(fragment == null && roots[0].hasAttribute('tmpl')) { + // If there is no child fragment, check to see if there is a template. + // This happens if children were passed but the client component did not render any. + let template = roots[0].querySelector(`template[data-astro-template]`); + if(template) { + innerHTML = template.innerHTML; + template.remove(); + } + } else if(fragment) { + innerHTML = fragment.innerHTML; + } const cb = async () => { const hydrate = await getHydrateCallback(); diff --git a/packages/astro/src/runtime/server/hydration.ts b/packages/astro/src/runtime/server/hydration.ts index b339b934f090..2ffdc9144cfb 100644 --- a/packages/astro/src/runtime/server/hydration.ts +++ b/packages/astro/src/runtime/server/hydration.ts @@ -116,7 +116,7 @@ export async function generateHydrateScript(scriptOptions: HydrateScriptOptions, const hydrationScript = { props: { type: 'module', 'data-astro-component-hydration': true }, children: `import setup from '${await result.resolve(hydrationSpecifier(hydrate))}'; -setup("${astroId}", {${metadata.hydrateArgs ? `value: ${JSON.stringify(metadata.hydrateArgs)}` : ''}}, async () => { +setup("${astroId}", {name:"${metadata.displayName}",${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 56a6a2a499cb..2253e2b997a6 100644 --- a/packages/astro/src/runtime/server/index.ts +++ b/packages/astro/src/runtime/server/index.ts @@ -274,7 +274,10 @@ If you're still stuck, please open an issue on GitHub or join us at https://astr // INVESTIGATE: This will likely be a problem in streaming because the `
` will be gone at this point. result.scripts.add(await generateHydrateScript({ renderer, result, astroId, props }, metadata as Required