Skip to content

Commit 1172914

Browse files
author
Jicheng Lu
committed
use util function to build url
1 parent 227fe22 commit 1172914

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

src/lib/common/ProfileDropdown.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import { userStore } from '$lib/helpers/store';
66
import { PUBLIC_SERVICE_URL } from '$env/static/public';
77
import { _ } from 'svelte-i18n';
8+
import { buildUrl } from '$lib/helpers/utils/common';
89
910
/** @type {any} */
1011
export let user;
@@ -31,7 +32,7 @@
3132
>
3233
<img
3334
class="rounded-circle header-profile-user"
34-
src={`${PUBLIC_SERVICE_URL}${user?.avatar}?access_token=${$userStore?.token}`}
35+
src={`${buildUrl(PUBLIC_SERVICE_URL, user?.avatar)}?access_token=${$userStore?.token}`}
3536
alt=""
3637
on:error={e => handleAvatarLoad(e)}
3738
/>

src/lib/helpers/utils/common.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
export function range(size = 3, startAt = 0) {
22
return [...Array(size).keys()].map((i) => i + startAt);
3-
};
3+
};
4+
5+
/**
6+
* @param {string} baseUrl
7+
* @param {string} relativePath
8+
*/
9+
export function buildUrl(baseUrl, relativePath) {
10+
return new URL(relativePath, baseUrl);
11+
}

src/routes/page/user/me/+page.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import { _ } from 'svelte-i18n';
1010
import { userStore } from '$lib/helpers/store';
1111
import { PUBLIC_SERVICE_URL } from '$env/static/public';
12+
import { buildUrl } from '$lib/helpers/utils/common';
1213
1314
/** @type {import('$types').UserModel} */
1415
let currentUser;
@@ -69,7 +70,7 @@
6970
on:drop={e => handleFileDrop(e)}
7071
>
7172
<img
72-
src={`${PUBLIC_SERVICE_URL}${currentUser?.avatar}?access_token=${$userStore?.token}`}
73+
src={`${buildUrl(PUBLIC_SERVICE_URL, currentUser?.avatar || '').href}?access_token=${$userStore?.token}`}
7374
alt=""
7475
class="img-thumbnail rounded-circle"
7576
style="width: 100%; height: 100%;"

0 commit comments

Comments
 (0)