Skip to content

Commit 728ee3a

Browse files
committed
fix: repair href attribute mismatches
The reasoning in #9662 turns out to be false: While it's true that the href would resolve to the same URL on the initial page, once you're doing a client-side navigation to a URL at a different depth, the relative path would now point to the wrong location. Therefore we need to repair href hydration mismatches fixes sveltejs/kit#12254
1 parent e366c49 commit 728ee3a

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

.changeset/two-cats-approve.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: repair `href` attribute mismatches

packages/svelte/src/internal/client/dom/elements/attributes.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,12 @@ export function set_attribute(element, attribute, value, skip_warning) {
8989
if (hydrating) {
9090
attributes[attribute] = element.getAttribute(attribute);
9191

92-
if (attribute === 'src' || attribute === 'href' || attribute === 'srcset') {
92+
// Don't include href: SvelteKit will by default render relative URLs but on the client they
93+
// need to be hydrated to root-relative URLs in case you have a base path set. Else they
94+
// could point to the wrong location once you click a link in a layout (if the links
95+
// in the layout don't update, which they won't if they don't depend on page data, then they
96+
// could be relative to a different URL and therefore wrong).
97+
if (attribute === 'src' || attribute === 'srcset') {
9398
if (!skip_warning) {
9499
check_src_in_dev_hydration(element, attribute, value);
95100
}

0 commit comments

Comments
 (0)