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

feat: ignore href when hydrating #9662

Merged
merged 2 commits into from
Nov 27, 2023
Merged

feat: ignore href when hydrating #9662

merged 2 commits into from
Nov 27, 2023

Conversation

Rich-Harris
Copy link
Member

Putting this up for discussion. Currently, a SvelteKit app will often render HTML with relative paths, which are replaced with absolute paths upon hydration. (This behaviour is intentional — it means that the HTML is more portable and resilient, for example if it ends up on the Internet Archive or on IPFS. It will likely be the default for all base-relative paths in SvelteKit 2.0.)

The cost is that something like <a href="{paths.base}/whatever"> will differ between SSR and CSR:

  • <a href="../whatever">
  • <a href="https://example.com/foo/bar/whatever">

As a result, Svelte updates the attribute during hydration, even though it resolves to the same URL.

For src and srcset attributes, we always skip hydration, on the assumption that the resolved value will be unchanged. (In dev, we check that this is in fact the case.) We could do the same thing for href. The stakes are lower — whereas updating src can cause unnecessary network requests, or cause iframes to be reloaded, this is only about avoiding unnecessary work — but it's probably worthwhile anyway.

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 Nov 27, 2023

🦋 Changeset detected

Latest commit: fb6c2b8

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

@benmccann benmccann mentioned this pull request Nov 27, 2023
@dummdidumm
Copy link
Member

Agree with the reasoning 👍

Copy link

vercel bot commented Nov 27, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
svelte-5-preview ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 27, 2023 1:54pm

@Rich-Harris Rich-Harris merged commit 9c44fd7 into main Nov 27, 2023
7 of 9 checks passed
@Rich-Harris Rich-Harris deleted the ignore-href branch November 27, 2023 13:49
dummdidumm added a commit that referenced this pull request 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, the relative path would now point to the wrong location. Therefore we need to repair href hydration mismatches

fixes sveltejs/kit#12254
dummdidumm added a commit that referenced this pull request 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, the relative path would now point to the wrong location. Therefore we need to repair href hydration mismatches

fixes sveltejs/kit#12254
Rich-Harris added a commit that referenced this pull request Aug 26, 2024
* 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

* remove test

* Revert "remove test"

This reverts commit fa43304.

* fix test

* remove comment, since the rationale for skipping the attributes in question is covered by the one immediately below

* fix

* add test for <a> specifically

---------

Co-authored-by: Rich Harris <rich.harris@vercel.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants