Skip to content

Commit 87dc49f

Browse files
committed
DRY out
1 parent 830f0e2 commit 87dc49f

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

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

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { parse } from './parse.js';
1818
import * as storage from './session-storage.js';
1919
import {
2020
find_anchor,
21-
get_base_uri,
21+
resolve_url,
2222
get_link_info,
2323
get_router_options,
2424
is_external_url,
@@ -235,12 +235,8 @@ export function create_client(app, target) {
235235
redirect_count,
236236
nav_token
237237
) {
238-
if (typeof url === 'string') {
239-
url = new URL(url, get_base_uri(document));
240-
}
241-
242238
return navigate({
243-
url,
239+
url: resolve_url(url),
244240
scroll: noScroll ? scroll_state() : null,
245241
keepfocus: keepFocus,
246242
redirect_count,
@@ -1375,21 +1371,20 @@ export function create_client(app, target) {
13751371
}
13761372
},
13771373

1378-
goto: (href, opts = {}) => {
1379-
if (typeof href === 'string') {
1380-
href = new URL(href, get_base_uri(document));
1381-
}
1382-
if (href.origin !== origin) {
1374+
goto: (url, opts = {}) => {
1375+
url = resolve_url(url);
1376+
1377+
if (url.origin !== origin) {
13831378
return Promise.reject(
13841379
new Error(
13851380
DEV
1386-
? `Cannot use \`goto\` with an external URL. Use \`window.location = "${href}"\` instead`
1381+
? `Cannot use \`goto\` with an external URL. Use \`window.location = "${url}"\` instead`
13871382
: 'goto: invalid URL'
13881383
)
13891384
);
13901385
}
13911386

1392-
return goto(href, opts, 0);
1387+
return goto(url, opts, 0);
13931388
},
13941389

13951390
invalidate: (resource) => {
@@ -1409,7 +1404,7 @@ export function create_client(app, target) {
14091404
},
14101405

14111406
preload_data: async (href) => {
1412-
const url = new URL(href, get_base_uri(document));
1407+
const url = resolve_url(href);
14131408
const intent = get_navigation_intent(url, false);
14141409

14151410
if (!intent) {

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@ import { PRELOAD_PRIORITIES } from './constants.js';
88

99
export const origin = BROWSER ? location.origin : '';
1010

11-
/** @param {HTMLDocument} doc */
12-
export function get_base_uri(doc) {
13-
let baseURI = doc.baseURI;
11+
/** @param {string | URL} url */
12+
export function resolve_url(url) {
13+
if (url instanceof URL) return url;
14+
15+
let baseURI = document.baseURI;
1416

1517
if (!baseURI) {
16-
const baseTags = doc.getElementsByTagName('base');
17-
baseURI = baseTags.length ? baseTags[0].href : doc.URL;
18+
const baseTags = document.getElementsByTagName('base');
19+
baseURI = baseTags.length ? baseTags[0].href : document.URL;
1820
}
1921

20-
return baseURI;
22+
return new URL(url, baseURI);
2123
}
2224

2325
export function scroll_state() {

0 commit comments

Comments
 (0)