Skip to content

Commit d02f58d

Browse files
committed
use get_navigation_intent and reroute hook
1 parent cf61978 commit d02f58d

File tree

5 files changed

+33
-24
lines changed

5 files changed

+33
-24
lines changed

packages/kit/src/runtime/app/paths/client.js

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
/** @import { ResolveArgs } from './types.js' */
33
import { base, assets, hash_routing } from './internal/client.js';
44
import { resolve_route } from '../../../utils/routing.js';
5-
import { decode_params } from '../../../utils/url.js';
6-
import { get_routes } from '../../client/client.js';
5+
import { get_navigation_intent } from '../../client/client.js';
76

87
/**
98
* Resolve the URL of an asset in your `static` directory, by prefixing it with [`config.kit.paths.assets`](https://svelte.dev/docs/kit/configuration#paths) if configured, or otherwise by prefixing it with the base path.
@@ -75,22 +74,14 @@ export function resolve(...args) {
7574
* @returns {Promise<{ id: RouteId, params: Record<string, string> } | null>}
7675
*/
7776
export async function match(pathname) {
78-
let path = pathname;
77+
const url = new URL(pathname, location.href);
78+
const intent = await get_navigation_intent(url, false);
7979

80-
if (base && path.startsWith(base)) {
81-
path = path.slice(base.length) || '/';
82-
}
83-
84-
if (hash_routing && path.startsWith('#')) {
85-
path = path.slice(1) || '/';
86-
}
87-
88-
const routes = get_routes();
89-
for (const route of routes) {
90-
const params = route.exec(path);
91-
if (params) {
92-
return { id: /** @type {RouteId} */ (route.id), params: decode_params(params) };
93-
}
80+
if (intent) {
81+
return {
82+
id: /** @type {RouteId} */ (intent.route.id),
83+
params: intent.params
84+
};
9485
}
9586

9687
return null;

packages/kit/src/runtime/app/paths/server.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { base, assets, relative, initial_base } from './internal/server.js';
22
import { resolve_route, exec } from '../../../utils/routing.js';
3-
import { decode_params } from '../../../utils/url.js';
3+
import { decode_params, decode_pathname } from '../../../utils/url.js';
44
import { try_get_request_store } from '@sveltejs/kit/internal/server';
55
import { manifest } from '__sveltekit/server';
6+
import { get_hooks } from '__SERVER__/internal.js';
67

78
/** @type {import('./client.js').asset} */
89
export function asset(file) {
@@ -31,16 +32,29 @@ export function resolve(id, params) {
3132

3233
/** @type {import('./client.js').match} */
3334
export async function match(pathname) {
34-
let path = pathname;
35+
const store = try_get_request_store();
36+
const origin = store?.event.url.origin ?? 'http://sveltekit';
37+
const url = new URL(pathname, origin);
3538

36-
if (base && path.startsWith(base)) {
37-
path = path.slice(base.length) || '/';
39+
const { reroute } = await get_hooks();
40+
41+
let resolved_path =
42+
(await reroute?.({ url, fetch: store?.event.fetch ?? fetch })) ?? url.pathname;
43+
44+
try {
45+
resolved_path = decode_pathname(resolved_path);
46+
} catch {
47+
return null;
48+
}
49+
50+
if (base && resolved_path.startsWith(base)) {
51+
resolved_path = resolved_path.slice(base.length) || '/';
3852
}
3953

4054
const matchers = await manifest._.matchers();
4155

4256
for (const route of manifest._.routes) {
43-
const match = route.pattern.exec(path);
57+
const match = route.pattern.exec(resolved_path);
4458
if (!match) continue;
4559

4660
const matched = exec(match, route.params, matchers);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ export function get_routes() {
203203
*/
204204
export let remote_responses = {};
205205

206+
export { get_navigation_intent };
207+
206208
/** @type {Array<((url: URL) => boolean)>} */
207209
const invalidated = [];
208210

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export const testPaths = [
22
'/match/load/foo',
33
'/match/slug/test-slug',
4-
'/match/not-a-real-route-that-exists'
4+
'/match/not-a-real-route-that-exists',
5+
'/reroute/basic/a'
56
];

packages/kit/test/apps/basics/test/test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,8 @@ test.describe('$app/paths', () => {
779779
path: '/match/slug/test-slug',
780780
expected: { id: '/match/slug/[slug]', params: { slug: 'test-slug' } }
781781
},
782-
{ path: '/match/not-a-real-route-that-exists', expected: null }
782+
{ path: '/match/not-a-real-route-that-exists', expected: null },
783+
{ path: '/reroute/basic/a', expected: { id: '/reroute/basic/b', params: {} } }
783784
];
784785

785786
for (const { path, expected } of samples) {

0 commit comments

Comments
 (0)