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

Page-relative href="#<id>" links break when using hash-based router #13320

Closed
stephenlrandall opened this issue Jan 16, 2025 · 2 comments · Fixed by #13356
Closed

Page-relative href="#<id>" links break when using hash-based router #13320

stephenlrandall opened this issue Jan 16, 2025 · 2 comments · Fixed by #13356
Labels
bug Something isn't working

Comments

@stephenlrandall
Copy link
Contributor

Describe the bug

When using router.type === 'hash', typical page-relative section links via href="#<id>" don't work. Instead, the full path href="#/path/to/page#<id> is required. This makes working with automated page generation tools difficult, since these kinds of links all need to be modified and prepended with the path part of $page.url.hash.

Reproduction

Compare the two "jump to..." links in https://github.com/stephenlrandall/kit-inline-test.

Logs

System Info

System:
    OS: macOS 15.2
    CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 401.48 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.8.0 - /usr/local/bin/node
    npm: 10.1.0 - /usr/local/bin/npm
    pnpm: 9.15.4 - ~/Library/pnpm/pnpm
  Browsers:
    Chrome: 131.0.6778.265
    Edge: 131.0.2903.147
    Safari: 18.2
  npmPackages:
    @sveltejs/adapter-auto: ^3.3.1 => 3.3.1 
    @sveltejs/adapter-static: ^3.0.8 => 3.0.8 
    @sveltejs/kit: ^2.15.3 => 2.15.3 
    @sveltejs/vite-plugin-svelte: ^4.0.4 => 4.0.4 
    svelte: ^5.18.0 => 5.18.0

Severity

annoyance

Additional Information

No response

@stephenlrandall stephenlrandall changed the title Page-relative href="#<id> links break when using hash-based router Page-relative href="#<id>" links break when using hash-based router Jan 16, 2025
@eltigerchino
Copy link
Member

eltigerchino commented Jan 16, 2025

We could add the check below when retrieving link info so that relative links such as href="#about" resolve to href="#/pathname#about" by checking if href starts with #/

 try { 
 	url = new URL(a instanceof SVGAElement ? a.href.baseVal : a.href, document.baseURI);
+	if (uses_hash_router && url.hash.match(/^#[^\/]/)) {
+		const route = location.hash.split('#')[0] || '/';
+		url.hash = `#${route}${url.hash}`;
+	}
 } catch {} 

try {
url = new URL(a instanceof SVGAElement ? a.href.baseVal : a.href, document.baseURI);
} catch {}

Thoughts? @dummdidumm

@dummdidumm
Copy link
Member

sounds sensible

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants