Skip to content

Commit 1c77e28

Browse files
authored
fix: correctly detect internal links when hash router is used (#13296)
fixes #13287 This PR fixes the client-side external link evaluation so that when using the hash router, accessing /index.html is considered an internal link. This prevents the page from reloading infinitely because the router incorrectly considered /index.html an external link. There's also a slight doc change. Instead of suggesting all links start with /#/, suggesting links start with # allows the router to preserve the /index.html pathname. For example, links to /about should be #/about instead of /#/about so that it navigates to /index.html#/about.
1 parent 9dc5c0e commit 1c77e28

File tree

4 files changed

+8
-3
lines changed

4 files changed

+8
-3
lines changed

.changeset/green-flowers-heal.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: prevent infinite reload when using the hash router and previewing `/index.html`

packages/kit/src/exports/public.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ export interface KitConfig {
653653
* What type of client-side router to use.
654654
* - `'pathname'` is the default and means the current URL pathname determines the route
655655
* - `'hash'` means the route is determined by `location.hash`. In this case, SSR and prerendering are disabled. This is only recommended if `pathname` is not an option, for example because you don't control the webserver where your app is deployed.
656-
* It comes with some caveats: you can't use server-side rendering (or indeed any server logic), and you have to make sure that the links in your app all start with /#/, or they won't work. Beyond that, everything works exactly like a normal SvelteKit app.
656+
* It comes with some caveats: you can't use server-side rendering (or indeed any server logic), and you have to make sure that the links in your app all start with #/, or they won't work. Beyond that, everything works exactly like a normal SvelteKit app.
657657
*
658658
* @default "pathname"
659659
* @since 2.14.0

packages/kit/src/runtime/client/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ export function is_external_url(url, base, hash_routing) {
311311
}
312312

313313
if (hash_routing) {
314-
if (url.pathname === base + '/') {
314+
if (url.pathname === base + '/' || url.pathname === base + '/index.html') {
315315
return false;
316316
}
317317

packages/kit/types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ declare module '@sveltejs/kit' {
635635
* What type of client-side router to use.
636636
* - `'pathname'` is the default and means the current URL pathname determines the route
637637
* - `'hash'` means the route is determined by `location.hash`. In this case, SSR and prerendering are disabled. This is only recommended if `pathname` is not an option, for example because you don't control the webserver where your app is deployed.
638-
* It comes with some caveats: you can't use server-side rendering (or indeed any server logic), and you have to make sure that the links in your app all start with /#/, or they won't work. Beyond that, everything works exactly like a normal SvelteKit app.
638+
* It comes with some caveats: you can't use server-side rendering (or indeed any server logic), and you have to make sure that the links in your app all start with #/, or they won't work. Beyond that, everything works exactly like a normal SvelteKit app.
639639
*
640640
* @default "pathname"
641641
* @since 2.14.0

0 commit comments

Comments
 (0)