Skip to content

Commit

Permalink
chore(astro): Make sure only one fallback slot renders
Browse files Browse the repository at this point in the history
  • Loading branch information
wobsoriano committed Feb 20, 2025
1 parent c5d88ad commit 4c6b1fe
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
12 changes: 10 additions & 2 deletions packages/astro/src/astro-components/control/Protect.astro
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,18 @@ type Props = ProtectProps & {
const { isStatic, ...props } = Astro.props;
const ProtectComponent = isStaticOutput(isStatic) ? ProtectCSR : ProtectSSR;
// Note: Astro server islands also use a "fallback" slot for loading states
// See: https://docs.astro.build/en/guides/server-islands/#server-island-fallback-content
// We use "protect-fallback" as our preferred slot name to avoid conflicts
const hasProtectFallback = Astro.slots.has('protect-fallback');
---

<ProtectComponent {...props}>
<slot />
<slot name="protect-fallback" slot="protect-fallback" />
<slot name="fallback" slot="fallback" />
{hasProtectFallback ? (
<slot name="protect-fallback" slot="protect-fallback" />
) : (
<slot name="fallback" slot="fallback" />
)}
</ProtectComponent>
21 changes: 13 additions & 8 deletions packages/astro/src/astro-components/control/ProtectSSR.astro
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ const isUnauthorized =
(typeof Astro.props.condition === "function" &&
!Astro.props.condition(has)) ||
((Astro.props.role || Astro.props.permission) && !has(Astro.props));
// Note: Astro server islands also use a "fallback" slot for loading states
// See: https://docs.astro.build/en/guides/server-islands/#server-island-fallback-content
// We use "protect-fallback" as our preferred slot name to avoid conflicts
const fallbackSlot = Astro.slots.has('protect-fallback')
? 'protect-fallback'
: 'fallback';
const hasProtectFallback = Astro.slots.has('protect-fallback');
---

{isUnauthorized ? <slot name={fallbackSlot} /> : <slot />}
{
isUnauthorized ? (
hasProtectFallback ? (
<slot name="protect-fallback" />
) : (
<slot name="fallback" />
)
) : (
<slot />
)
}

0 comments on commit 4c6b1fe

Please sign in to comment.