Skip to content

fix(ssr-runtime): coerce className to string for non-string values#5766

Open
nikhil6393 wants to merge 1 commit into
salesforce:masterfrom
nikhil6393:fix/ssr-classname-non-string-coercion
Open

fix(ssr-runtime): coerce className to string for non-string values#5766
nikhil6393 wants to merge 1 commit into
salesforce:masterfrom
nikhil6393:fix/ssr-classname-non-string-coercion

Conversation

@nikhil6393

Copy link
Copy Markdown

Summary

Fixes #5093
In browsers, setting element.className to a non-string value (e.g. undefined, null, 0, false) coerces it to its string representation. The SSR v2 runtime (@lwc/ssr-runtime) was storing the raw value without coercion, diverging from browser behaviour.

Root cause: The className setter in LightningElement (SSR v2) stored newVal directly into #props.class and #attrs.class without calling String().

Fix

Add String() coercion inside the className setter in packages/@lwc/ssr-runtime/src/lightning-element.ts:

set className(newVal: any) {
    // Coerce to string to match browser behaviour:
    // e.g. `element.className = undefined` -> className is "undefined"
    const strVal = String(newVal);
    this.#props.class = strVal;
    this.#attrs.class = strVal;
    mutationTracker.add(this, 'class');
}

Tests

Added unit tests in packages/@lwc/ssr-runtime/src/__tests__/lightning-element.spec.ts covering:

  • undefined -> "undefined"
    • null -> "null"
    • 0 -> "0"
    • false -> "false"
    • numeric values
    • objects with toString()

…alesforce#5093)

In browsers, setting element.className to a non-string value (e.g. undefined,
null, 0, false) coerces it to the string representation via String(). The SSR
runtime was storing the raw value without coercion, diverging from browser
behaviour.

This fix adds String() coercion inside the className setter in LightningElement
so that the SSR runtime matches browser semantics.

Fixes salesforce#5093
@nikhil6393
nikhil6393 requested a review from a team as a code owner March 29, 2026 19:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[SSR] className reflection should handle non-strings

1 participant