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/blue-baboons-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: include hash when using `resolve` with hash routing enabled
1 change: 1 addition & 0 deletions packages/kit/src/exports/vite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ async function kit({ svelte_config }) {
__SVELTEKIT_PATHS_BASE__: s(kit.paths.base),
__SVELTEKIT_PATHS_RELATIVE__: s(kit.paths.relative),
__SVELTEKIT_CLIENT_ROUTING__: s(kit.router.resolution === 'client'),
__SVELTEKIT_HASH_ROUTING__: s(kit.router.type === 'hash'),
__SVELTEKIT_SERVER_TRACING_ENABLED__: s(kit.experimental.tracing.server)
};

Expand Down
8 changes: 6 additions & 2 deletions packages/kit/src/runtime/app/paths/client.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @import { Asset, RouteId, Pathname, ResolvedPathname } from '$app/types' */
/** @import { ResolveArgs } from './types.js' */
import { base, assets } from './internal/client.js';
import { base, assets, hash_routing } from './internal/client.js';
import { resolve_route } from '../../../utils/routing.js';

/**
Expand All @@ -25,6 +25,8 @@ export function asset(file) {
return (assets || base) + file;
}

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

/**
* Resolve a pathname by prefixing it with the base path, if any, or resolve a route ID by populating dynamic segments with parameters.
*
Expand All @@ -51,7 +53,9 @@ export function asset(file) {
export function resolve(...args) {
// The type error is correct here, and if someone doesn't pass params when they should there's a runtime error,
// but we don't want to adjust the internal resolve_route function to accept `undefined`, hence the type cast.
return base + resolve_route(args[0], /** @type {Record<string, string>} */ (args[1]));
return (
base + pathname_prefix + resolve_route(args[0], /** @type {Record<string, string>} */ (args[1]))
);
}

export { base, assets, resolve as resolveRoute };
1 change: 1 addition & 0 deletions packages/kit/src/runtime/app/paths/internal/client.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const base = __SVELTEKIT_PAYLOAD__?.base ?? __SVELTEKIT_PATHS_BASE__;
export const assets = __SVELTEKIT_PAYLOAD__?.assets ?? base ?? __SVELTEKIT_PATHS_ASSETS__;
export const app_dir = __SVELTEKIT_APP_DIR__;
export const hash_routing = __SVELTEKIT_HASH_ROUTING__;
2 changes: 2 additions & 0 deletions packages/kit/src/types/global-private.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ declare global {
const __SVELTEKIT_EXPERIMENTAL__REMOTE_FUNCTIONS__: boolean;
/** True if `config.kit.router.resolution === 'client'` */
const __SVELTEKIT_CLIENT_ROUTING__: boolean;
/** True if `config.kit.router.type === 'hash'` */
const __SVELTEKIT_HASH_ROUTING__: boolean;
/**
* True if any node in the manifest has a server load function.
* Used for treeshaking server load code from client bundles when no server loads exist.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script lang="ts">
import { resolve } from '$app/paths';
</script>

<a href={resolve('/')}>go to home</a>
9 changes: 9 additions & 0 deletions packages/kit/test/apps/hash-based-routing/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,13 @@ test.describe('hash based navigation', () => {
await expect(page.locator('#button3')).toBeFocused();
await expect(page.locator('button[id="button3"]')).toBeFocused();
});

test('resolve works', async ({ page }) => {
await page.goto('/#/resolve');
await page.locator('a', { hasText: 'go to home' }).click();
await expect(page.locator('p')).toHaveText('home');
const url = new URL(page.url());
expect(url.pathname).toBe('/');
expect(url.hash).toBe('#/');
});
});
Loading