Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/hash-resolve-document.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: keep hash-router links on the current document when resolving paths
9 changes: 4 additions & 5 deletions packages/kit/src/runtime/app/paths/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ export function asset(file) {
return (assets || base) + '/' + path;
}

const pathname_prefix = hash_routing ? '#' : '';
const pathname_prefix = hash_routing ? '#' : base;

/**
* Resolve a pathname by prefixing it with the base path, if any, or resolve a route ID by populating dynamic segments with parameters.
* In hash routing mode, the returned URL starts with `#`.
*
* During server rendering, the base path is relative and depends on the page currently being rendered.
*
Expand Down Expand Up @@ -73,12 +74,10 @@ export function resolve(...args) {
throw new Error(`Missing params for dynamic route ID ${id}`);
}

return (
/** @type {ResolvedPathname} */ (base + pathname_prefix + resolve_route(id, params ?? {}))
);
return /** @type {ResolvedPathname} */ (pathname_prefix + resolve_route(id, params ?? {}));
}

return /** @type {ResolvedPathname} */ (base + pathname_prefix + '/' + id);
return /** @type {ResolvedPathname} */ (pathname_prefix + '/' + id);
}

/**
Expand Down
16 changes: 16 additions & 0 deletions packages/kit/src/runtime/app/paths/client.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { expect, test, vi } from 'vitest';

vi.hoisted(() => {
vi.stubGlobal('__SVELTEKIT_PAYLOAD__', {});
vi.stubGlobal('__SVELTEKIT_PATHS_BASE__', '/base');
vi.stubGlobal('__SVELTEKIT_PATHS_ASSETS__', '');
vi.stubGlobal('__SVELTEKIT_APP_DIR__', '_app');
vi.stubGlobal('__SVELTEKIT_HASH_ROUTING__', true);
});

const { resolve } = await import('./client.js');

test('hash routing resolves to a fragment without the base', () => {
expect(resolve('about')).toBe('#/about');
expect(resolve('/blog/[slug]', { slug: 'hello' })).toBe('#/blog/hello');
});
1 change: 1 addition & 0 deletions packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2923,6 +2923,7 @@ declare module '$app/paths' {
export function asset(file: AssetPath): string;
/**
* Resolve a pathname by prefixing it with the base path, if any, or resolve a route ID by populating dynamic segments with parameters.
* In hash routing mode, the returned URL starts with `#`.
*
* During server rendering, the base path is relative and depends on the page currently being rendered.
*
Expand Down
Loading