Hydration rewrites numeric/boolean attributes that already match the server markup
Describe the bug
During hydration, set_attribute compares the server attribute (a string from getAttribute()) with the client value. When the value is a number or boolean, the two never compare equal, so attributes like tabindex={-1}, aria-rowindex={i}, colspan={1}, <svg width={24} stroke-width={1.5}> or data-x={false} are set again with the string they already have.
If the page observes the DOM, each rewrite enqueues an attribute mutation record, and it can add style and layout work. In a 29-row table with a synthetic mix of attributes, Chromium records 493 attribute mutations during hydration on main, although the markup already matches. 290 of them are eligible to be skipped: plain setAttribute writes of numbers and booleans that store exactly the string already there. The other 203 are preserved: they go through property setters (IDL setters such as canvas dimensions, spread disabled/required, and compiled property assignments), which can have side effects even when they store an identical string. The candidate removes 290 attribute mutation records in this fixture while retaining 203 writes.
set_class already skips unchanged values during hydration. I intend to submit a PR that, during hydration, skips only setAttribute calls that would store the exact string the server rendered for a number or boolean. Property setters keep running, and so do writes to URL attributes such as an SVG <image href>, because writing the same URL again can fire another load or error event, and to SVG list attributes such as <text x>, because writing one detaches list items retrieved through baseVal. Writes to autonomous custom elements are kept, and so are writes to upgraded customized built-ins, which are recognized by their prototype, so also after is is removed. One limitation: an upgraded customized built-in whose prototype was replaced with the native one is compared as native, so it gets no attributeChangedCallback for these skipped values. That is how main already treats unchanged strings on any customized built-in.
Reproduction
Reproduction repository: https://gist.github.com/xyrolle/5b9cab56f4c89e7e28d66c61b5637279
This requires an SSR/hydration harness with the observer attached beforehand. This self-contained script, which is also in the repository, provides one with only svelte and jsdom (the output below is from svelte 5.57.1 and jsdom 30.1.1):
mkdir hydration-writes && cd hydration-writes
npm init -y && npm pkg set type=module
npm i [email protected] [email protected]
# add App.svelte and repro.mjs from below
node --conditions=browser repro.mjsApp.svelte:
<script>
let { rows = ['a', 'b', 'c'] } = $props();
const rest = { disabled: true, tabindex: -1 };
</script>
<table>
<tbody>
{#each rows as row, i (row)}
<tr aria-rowindex={i + 2}>
<td tabindex={-1}>{row}</td>
<td><svg width={24} height={24} stroke-width={1.7} viewBox="0 0 24 24"></svg></td>
<td><button {...rest}>x</button></td>
</tr>
{/each}
</tbody>
</table>repro.mjs compiles the component for the server and the client, renders it with svelte/server, and hydrates the result in jsdom with a MutationObserver attached:
// node --conditions=browser repro.mjs
import fs from 'node:fs';
import { JSDOM } from 'jsdom';
import { compile, VERSION } from 'svelte/compiler';
import { render } from 'svelte/server';
const source = fs.readFileSync('App.svelte', 'utf-8');
fs.mkdirSync('out', { recursive: true });
for (const generate of ['server', 'client']) {
fs.writeFileSync(`out/${generate}.mjs`, compile(source, { generate }).js.code);
}
const { default: ServerApp } = await import('./out/server.mjs');
const { window } = new JSDOM(`<main>${render(ServerApp).body}</main>`);
Object.assign(globalThis, {
window,
document: window.document,
Node: window.Node,
Element: window.Element,
Text: window.Text
});
const { hydrate, flushSync } = await import('svelte');
const { default: App } = await import('./out/client.mjs');
const target = document.querySelector('main');
const observer = new window.MutationObserver(() => {});
observer.observe(target, { attributes: true, attributeOldValue: true, subtree: true });
hydrate(App, { target });
flushSync();
const records = observer.takeRecords();
const same = records.filter((r) => r.oldValue === r.target.getAttribute(r.attributeName));
const counts = {};
for (const r of records) {
const key = `${r.target.localName} ${r.attributeName}`;
counts[key] = (counts[key] ?? 0) + 1;
}
console.log(`svelte ${VERSION}: ${records.length} attribute mutation records during hydration`);
console.log(`${same.length} of them wrote the value the attribute already had`);
console.table(counts);Actual: 21 records, each writing the value the attribute already had. 18 of them write numbers with setAttribute (tabindex, width, height, stroke-width, aria-rowindex); the other 3 are the spread's disabled property assignments. Expected with the proposed change: the 18 attribute writes are skipped, and the 3 spread-property writes are retained. The same component hydrated in Chromium 151 also records 21 on main (38098a9).
Logs
$ node --conditions=browser repro.mjs
svelte 5.57.1: 21 attribute mutation records during hydration
21 of them wrote the value the attribute already had
┌──────────────────┬────────┐
│ (index) │ Values │
├──────────────────┼────────┤
│ td tabindex │ 3 │
│ svg width │ 3 │
│ svg height │ 3 │
│ svg stroke-width │ 3 │
│ button disabled │ 3 │
│ button tabindex │ 3 │
│ tr aria-rowindex │ 3 │
└──────────────────┴────────┘System Info
System:
OS: macOS 27.0
CPU: (18) arm64 Apple M5 Pro
Memory: 2.68 GB / 64.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 24.16.0 - ~/.nvm/versions/node/v24.16.0/bin/node
Yarn: 1.22.22 - ~/.nvm/versions/node/v24.16.0/bin/yarn
npm: 11.13.0 - ~/.nvm/versions/node/v24.16.0/bin/npm
pnpm: 11.12.0 - ~/.nvm/versions/node/v24.16.0/bin/pnpm
Browsers:
Chrome: 153.0.8010.54
Safari: 27.0
npmPackages:
svelte: ^5.57.1 => 5.57.1The Chromium counts above come from Playwright's Chromium 151.0.7922.34 (headless). The reproduction script ran with jsdom 30.1.1.
Severity
annoyance
Source: sveltejs/svelte