Skip to content

fix(svelte): prevent empty class attribute with null defaults#15604

Merged
ematipico merged 9 commits into
withastro:mainfrom
pierreeurope:fix-svelte-empty-class-15576
Mar 26, 2026
Merged

fix(svelte): prevent empty class attribute with null defaults#15604
ematipico merged 9 commits into
withastro:mainfrom
pierreeurope:fix-svelte-empty-class-15576

Conversation

@pierreeurope
Copy link
Copy Markdown
Contributor

@pierreeurope pierreeurope commented Feb 21, 2026

Fixes #15576

This adds a temporary workaround for a Svelte bug where components extract the class property with a null default (like let { class: className = null } = $props();). The SSR output incorrectly renders class="" instead of omitting the attribute entirely.

The fix post-processes the SSR HTML output to remove empty class attributes, matching native Svelte behavior until the upstream issue is resolved.

Tests verify the fix works correctly in both build and dev modes.

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Feb 21, 2026

🦋 Changeset detected

Latest commit: 0e1d9ad

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions Bot added pkg: svelte Related to Svelte (scope) pkg: integration Related to any renderer integration (scope) labels Feb 21, 2026

// Remove empty class="" attributes from SSR output to match native Svelte behavior.
// This happens when components extract class with null defaults: let { class: className = null }
// Svelte 5 SSR renders null class values as class="", but standalone Svelte omits them.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a svelte bug, which is worth tracking. You might want rewording you comment and add the issue #15576

Comment thread .changeset/fix-svelte-empty-class.md Outdated
"@astrojs/svelte": patch
---

fix(svelte): prevent empty class attribute with null defaults
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fix(svelte): prevent empty class attribute with null defaults
Adds a temporary workaround caused by empty class attributes.

@ematipico ematipico self-assigned this Feb 23, 2026
Jarvis and others added 9 commits February 25, 2026 21:45
Fixes withastro#15576

When Svelte components extract the class property with a null default
(e.g., `let { class: className = null } = $props();`), Svelte 5's
SSR render function outputs `class=""` instead of omitting the
attribute entirely.

This fix post-processes the SSR HTML output to remove empty class
attributes, matching native Svelte behavior where null/undefined
class values don't render the attribute.

The fix uses a simple regex to strip ` class=""` from the rendered
HTML. This approach is safe because:
- It only matches the exact pattern of a space followed by class=""
- It doesn't affect non-empty class attributes
- It runs after Svelte's render function completes

Added comprehensive tests covering both build and dev modes.
@pierreeurope pierreeurope force-pushed the fix-svelte-empty-class-15576 branch from b5d08cf to 0e1d9ad Compare February 25, 2026 20:47
@Princesseuh Princesseuh requested a review from ematipico March 25, 2026 01:37
@ematipico ematipico merged commit 3e1ac66 into withastro:main Mar 26, 2026
22 checks passed
@astrobot-houston astrobot-houston mentioned this pull request Mar 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: integration Related to any renderer integration (scope) pkg: svelte Related to Svelte (scope)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Empty class attribute when combining class extraction with spread props in Svelte components

2 participants