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

fix: repair href attribute mismatches #13032

merged 7 commits into from
Aug 26, 2024

Conversation

dummdidumm
Copy link
Member

@dummdidumm dummdidumm commented Aug 26, 2024

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 (and the link stays around, for example because it's in a layout), the relative path would now point to the wrong location. Therefore we need to repair href hydration mismatches

fixes sveltejs/kit#12254

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

Copy link

changeset-bot bot commented Aug 26, 2024

🦋 Changeset detected

Latest commit: fbd234a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

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
@Rich-Harris
Copy link
Member

I'm not following — if you're navigating client-side then you're not hydrating, so you wouldn't reach this code

@dummdidumm
Copy link
Member Author

Imagine you have a layout like

<!-- +layout.svelte -->
<script>
  import { base } from "$app/paths";
</script>

<a href="{base}/">home</a>
<a href="{base}/foo">foo</a>

<slot />

You start at /base-path/, so the href for foo looks like ./foo. Now you click on that link, so you're on /base-path/foo. Since this is the root layout, it will stay as-is between navigations, so the href still links to ./foo. That means, when you click on it again, you end up on /base-path/foo/foo -> wrong.

@Rich-Harris
Copy link
Member

Ah, I see. We still need to ignore mismatches for <link> elements, otherwise changing ./foo to /foo will trigger another network request (the avoidance of which was the purpose of ignoring href mismatches in the first place, just like for src and srcset), so we should reinstate the test but change the <a> to a <link>.

The other cases where an href attribute applies are <base> and <area>, neither of which eagerly trigger requests, so it's just <link> that this applies to AFAICT.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants