Skip to content

Commit 7afa167

Browse files
authored
chore: prepare hash based routing (#13210)
* pass URL to _preload_code and get_url_path * add get_route_id * get_route_id is a misnomer
1 parent 921fb86 commit 7afa167

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

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

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -424,9 +424,9 @@ async function _preload_data(intent) {
424424
return load_cache.promise;
425425
}
426426

427-
/** @param {string} pathname */
428-
async function _preload_code(pathname) {
429-
const route = routes.find((route) => route.exec(get_url_path(pathname)));
427+
/** @param {URL} url */
428+
async function _preload_code(url) {
429+
const route = routes.find((route) => route.exec(get_url_path(url)));
430430

431431
if (route) {
432432
await Promise.all([...route.layouts, route.leaf].map((load) => load?.[1]()));
@@ -875,7 +875,7 @@ async function load_route({ id, invalidating, url, params, route, preload }) {
875875

876876
/** @type {import('types').ServerNodesResponse | import('types').ServerRedirectNode | null} */
877877
let server_data = null;
878-
const url_changed = current.url ? id !== current.url.pathname + current.url.search : false;
878+
const url_changed = current.url ? id !== get_page_key(current.url) : false;
879879
const route_changed = current.route ? route.id !== current.route.id : false;
880880
const search_params_changed = diff_search_params(current.url, url);
881881

@@ -1175,7 +1175,12 @@ function get_navigation_intent(url, invalidating) {
11751175
// reroute could alter the given URL, so we pass a copy
11761176
let rerouted;
11771177
try {
1178-
rerouted = app.hooks.reroute({ url: new URL(url) }) ?? url.pathname;
1178+
rerouted = app.hooks.reroute({ url: new URL(url) }) ?? url;
1179+
1180+
if (typeof rerouted === 'string') {
1181+
url.pathname = rerouted;
1182+
rerouted = url;
1183+
}
11791184
} catch (e) {
11801185
if (DEV) {
11811186
// in development, print the error...
@@ -1195,7 +1200,7 @@ function get_navigation_intent(url, invalidating) {
11951200
const params = route.exec(path);
11961201

11971202
if (params) {
1198-
const id = url.pathname + url.search;
1203+
const id = get_page_key(url);
11991204
/** @type {import('./types.js').NavigationIntent} */
12001205
const intent = {
12011206
id,
@@ -1209,9 +1214,14 @@ function get_navigation_intent(url, invalidating) {
12091214
}
12101215
}
12111216

1212-
/** @param {string} pathname */
1213-
function get_url_path(pathname) {
1214-
return decode_pathname(pathname.slice(base.length) || '/');
1217+
/** @param {URL} url */
1218+
function get_url_path(url) {
1219+
return decode_pathname(url.pathname.slice(base.length) || '/');
1220+
}
1221+
1222+
/** @param {URL} url */
1223+
function get_page_key(url) {
1224+
return url.pathname + url.search;
12151225
}
12161226

12171227
/**
@@ -1547,7 +1557,7 @@ function setup_preload() {
15471557
(entries) => {
15481558
for (const entry of entries) {
15491559
if (entry.isIntersecting) {
1550-
_preload_code(/** @type {HTMLAnchorElement} */ (entry.target).href);
1560+
_preload_code(new URL(/** @type {HTMLAnchorElement} */ (entry.target).href));
15511561
observer.unobserve(entry.target);
15521562
}
15531563
}
@@ -1569,7 +1579,7 @@ function setup_preload() {
15691579
const options = get_router_options(a);
15701580

15711581
// we don't want to preload data for a page we're already on
1572-
const same_url = url && current.url.pathname + current.url.search === url.pathname + url.search;
1582+
const same_url = url && get_page_key(current.url) === get_page_key(url);
15731583

15741584
if (!options.reload && !same_url) {
15751585
if (priority <= options.preload_data) {
@@ -1591,7 +1601,7 @@ function setup_preload() {
15911601
}
15921602
}
15931603
} else if (priority <= options.preload_code) {
1594-
_preload_code(/** @type {URL} */ (url).pathname);
1604+
_preload_code(/** @type {URL} */ (url));
15951605
}
15961606
}
15971607
}
@@ -1611,7 +1621,7 @@ function setup_preload() {
16111621
}
16121622

16131623
if (options.preload_code === PRELOAD_PRIORITIES.eager) {
1614-
_preload_code(/** @type {URL} */ (url).pathname);
1624+
_preload_code(/** @type {URL} */ (url));
16151625
}
16161626
}
16171627
}
@@ -1855,19 +1865,21 @@ export function preloadCode(pathname) {
18551865
throw new Error('Cannot call preloadCode(...) on the server');
18561866
}
18571867

1868+
const url = new URL(pathname, current.url);
1869+
18581870
if (DEV) {
18591871
if (!pathname.startsWith(base)) {
18601872
throw new Error(
18611873
`pathnames passed to preloadCode must start with \`paths.base\` (i.e. "${base}${pathname}" rather than "${pathname}")`
18621874
);
18631875
}
18641876

1865-
if (!routes.find((route) => route.exec(get_url_path(pathname)))) {
1877+
if (!routes.find((route) => route.exec(get_url_path(url)))) {
18661878
throw new Error(`'${pathname}' did not match any routes`);
18671879
}
18681880
}
18691881

1870-
return _preload_code(pathname);
1882+
return _preload_code(url);
18711883
}
18721884

18731885
/**

0 commit comments

Comments
 (0)