1
- import { render_effect , teardown } from '../../reactivity/effects.js' ;
1
+ import { effect , render_effect , teardown } from '../../reactivity/effects.js' ;
2
+ import { untrack } from '../../runtime.js' ;
2
3
import { set_attribute } from '../elements/attributes.js' ;
3
4
import { set_class } from '../elements/class.js' ;
4
5
import { hydrating } from '../hydration.js' ;
@@ -15,10 +16,10 @@ export function svelte_html(get_attributes) {
15
16
// @ts -expect-error
16
17
const current_setters = ( node . __attributes_setters ??= { } ) ;
17
18
18
- /** @type {Record<string, any> } */
19
+ /** @type {Record<string, any> } Does _not_ contain event listeners, those are handled separately */
19
20
let attributes ;
20
21
21
- render_effect ( ( ) => {
22
+ const set_html_attributes = ( ) => {
22
23
attributes = get_attributes ( ) ;
23
24
24
25
for ( const name in attributes ) {
@@ -29,9 +30,16 @@ export function svelte_html(get_attributes) {
29
30
let value = attributes [ name ] ;
30
31
current . push ( { owner : self , value } ) ;
31
32
32
- // Do nothing on initial render during hydration: If there are attribute duplicates, the last value
33
- // wins, which could result in needless hydration repairs from earlier values.
34
- if ( hydrating ) continue ;
33
+ // Defer hydration on initial render during hydration: If there are attribute duplicates, the last value
34
+ // wins, so we wait until all values have been set to see if we're actually the last one that sets the value.
35
+ if ( hydrating ) {
36
+ effect ( ( ) => {
37
+ if ( current [ current . length - 1 ] . owner === self ) {
38
+ untrack ( set_html_attributes ) ;
39
+ }
40
+ } ) ;
41
+ return ;
42
+ }
35
43
36
44
if ( name === 'class' ) {
37
45
// Avoid unrelated attribute changes from triggering class changes
@@ -42,7 +50,9 @@ export function svelte_html(get_attributes) {
42
50
set_attribute ( node , name , value ) ;
43
51
}
44
52
}
45
- } ) ;
53
+ } ;
54
+
55
+ render_effect ( set_html_attributes ) ;
46
56
47
57
teardown ( ( ) => {
48
58
for ( const name in attributes ) {
0 commit comments