Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: repair href attribute mismatches #13032

Merged
merged 7 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/two-cats-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: repair `href` attribute mismatches
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ export function set_attribute(element, attribute, value, skip_warning) {
if (hydrating) {
attributes[attribute] = element.getAttribute(attribute);

if (attribute === 'src' || attribute === 'href' || attribute === 'srcset') {
if (
attribute === 'src' ||
attribute === 'srcset' ||
(attribute === 'href' && element.nodeName === 'LINK')
) {
if (!skip_warning) {
check_src_in_dev_hydration(element, attribute, value);
}
Expand Down Expand Up @@ -388,7 +392,7 @@ function check_src_in_dev_hydration(element, attribute, value) {

w.hydration_attribute_changed(
attribute,
element.outerHTML.replace(element.innerHTML, '...'),
element.outerHTML.replace(element.innerHTML, element.innerHTML && '...'),
String(value)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ export default test({
},

test(assert, target) {
assert.equal(target.querySelector('a')?.getAttribute('href'), '/bar');
assert.equal(target.querySelector('link')?.getAttribute('href'), '/bar');
},

errors: [
'The `href` attribute on `<a href="/bar">...</a>` changed its value between server and client renders. The client value, `/foo`, will be ignored in favour of the server value'
'The `href` attribute on `<link href="/bar">` changed its value between server and client renders. The client value, `/foo`, will be ignored in favour of the server value'
]
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
let { browser } = $props();
</script>

<a href={browser ? '/foo': '/bar'}>foo</a>
<link href={browser ? '/foo' : '/bar'} />
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ export default test({
assert.htmlEqual(target.innerHTML, '<img src="server.jpg" alt="">');
},
errors: [
'The `src` attribute on `...<img src="server.jpg" alt="">` changed its value between server and client renders. The client value, `client.jpg`, will be ignored in favour of the server value'
'The `src` attribute on `<img src="server.jpg" alt="">` changed its value between server and client renders. The client value, `client.jpg`, will be ignored in favour of the server value'
]
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { test } from '../../test';

export default test({
server_props: {
browser: false
},

props: {
browser: true
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!--[--><a href="/foo">foo</a><!--]-->
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
let { browser } = $props();
</script>

<a href={browser ? '/foo' : '/bar'}>foo</a>
Loading