Skip to content

Conversation

benmccann
Copy link
Member

@benmccann benmccann commented Oct 23, 2024

Closes #556. This works pretty well.

Is it okay that I mutated the URL from the store on line 40?
And is using goto on line 49 the appropriate way to do this?

Copy link

vercel bot commented Oct 23, 2024

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

Name Status Preview Comments Updated (UTC)
kit-svelte-dev ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 23, 2024 7:47pm
learn-svelte-dev 🛑 Canceled (Inspect) Oct 23, 2024 7:47pm
omnisite 🛑 Canceled (Inspect) Oct 23, 2024 7:47pm
svelte-5-preview ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 23, 2024 7:47pm
svelte-dev ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 23, 2024 7:47pm

for (const el of elements) {
if (el.id.toLocaleLowerCase() === hash.toLocaleLowerCase()) {
const url = $page.url;
url.hash = el.id;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it necessary to mutate it? wouldn't you be able to construct the new url and return it without touching the store?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, mostly I wanted to know if I should even be able to. It doesn't seem like I should. I filed a feature request for that: sveltejs/kit#12870. And have updated this to make a copy

Comment on lines 30 to 34
const elements = document.querySelectorAll('*[id]');
// if there's an exact match, use that. no need to redirect
// also handles the case where one appears twice with difference casing
// e.g. https://svelte.dev/docs/kit/@sveltejs-kit#redirect vs https://svelte.dev/docs/kit/@sveltejs-kit#Redirect
for (const el of elements) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we iterating? just use a case-insensitive selector

const heading = document.querySelector(`[id="${id}" i]`);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated to use that, but we still need to iterate. e.g. we have https://svelte.dev/docs/kit/@sveltejs-kit#redirect and https://svelte.dev/docs/kit/@sveltejs-kit#Redirect

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could do this!

document.querySelector(`[id="${id}"]`) ?? document.querySelector(`[id="${id}" i]`);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well it turns out that this issue isn't fixable. we need to change one of these ids so that they don't collide: #590

@Rich-Harris
Copy link
Member

So here's the thing: this won't close #556, unless you also add a handleMissingId function to svelte.config.js that allows broken hashes through. Which we obviously don't want.

I think we should just fix the links.

@benmccann
Copy link
Member Author

So here's the thing: this won't close #556, unless you also add a handleMissingId function to svelte.config.js that allows broken hashes through. Which we obviously don't want.
I think we should just fix the links.

I fixed the links in our own docs already. What I'm worried about is breaking every link on the internet, which is what I'm trying to prevent here

@Rich-Harris Rich-Harris merged commit 9fa424b into main Oct 23, 2024
7 checks passed
@Rich-Harris Rich-Harris deleted the case-insensitive-hash branch October 23, 2024 19:54
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.

lowercase hash links?

3 participants