fix(svelte): prevent empty class attribute with null defaults#15604
Merged
ematipico merged 9 commits intoMar 26, 2026
Conversation
🦋 Changeset detectedLatest 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 |
ematipico
reviewed
Feb 23, 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. |
Member
There was a problem hiding this comment.
This is a svelte bug, which is worth tracking. You might want rewording you comment and add the issue #15576
ematipico
reviewed
Feb 23, 2026
| "@astrojs/svelte": patch | ||
| --- | ||
|
|
||
| fix(svelte): prevent empty class attribute with null defaults |
Member
There was a problem hiding this comment.
Suggested change
| fix(svelte): prevent empty class attribute with null defaults | |
| Adds a temporary workaround caused by empty class attributes. |
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.
b5d08cf to
0e1d9ad
Compare
ematipico
approved these changes
Mar 26, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 rendersclass=""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.