diff --git a/src/app.html b/src/app.html index 41147b96..4e1b63ae 100644 --- a/src/app.html +++ b/src/app.html @@ -2,7 +2,7 @@
- + %sveltekit.head% diff --git a/src/hooks.server.ts b/src/hooks.server.ts index f6a13b19..f3f87e04 100644 --- a/src/hooks.server.ts +++ b/src/hooks.server.ts @@ -1,19 +1,27 @@ import { error } from '@sveltejs/kit' +function buildUrl(inputUrl: URL): URL { + let withoutCors = inputUrl.pathname.replace('/cors/', '') + + if (!withoutCors.startsWith('https://')) { + withoutCors = 'https://' + withoutCors + } + + return new URL(`${withoutCors}?${inputUrl.searchParams.toString()}`) +} + export async function handle({ event, resolve }) { // annoying hack to fix lemmy's CORS if (event.url.pathname.startsWith('/cors')) { - const instance = `${event.url.pathname.split('/')[2]}` - const url = `${event.url.pathname - .split('/') - .slice(3) - .join('/')}?${event.url.searchParams.toString()}` + // cut off the 1st slash (empty), and remove the /cors event.request.headers.delete('origin') event.request.headers.delete('host') try { - const data = await fetch(`https://${instance}/${url.toString()}`, { + const url = buildUrl(event.url) + + const data = await fetch(url, { method: event.request.method, body: event.request.body, headers: event.request.headers, @@ -22,9 +30,33 @@ export async function handle({ event, resolve }) { signal: AbortSignal.timeout(20 * 1000), }) - const json = await data.json() + if (!data.ok) { + return new Response( + JSON.stringify({ + message: await data.text(), + }), + { + status: data.status, + } + ) + } - return new Response(JSON.stringify(json)) + try { + const json = await data.json() + + return new Response(JSON.stringify(json), { + status: data.status, + }) + } catch (err) { + return new Response( + JSON.stringify({ + message: 'Failed to fetch', + }), + { + status: data.status, + } + ) + } } catch (error) { console.error(error) return new Response( diff --git a/src/lib/components/community/CommunityLink.svelte b/src/lib/components/community/CommunityLink.svelte index ee1d94aa..0ef51309 100644 --- a/src/lib/components/community/CommunityLink.svelte +++ b/src/lib/components/community/CommunityLink.svelte @@ -7,13 +7,13 @@ function linkFromCommunity(community: Community) { const local = community.local - if (local) { - return `/c/${community.name}` - } else { - const domain = new URL(community.actor_id).hostname + // if (local) { + // return `/c/${community.name}` + // } else { + const domain = new URL(community.actor_id).hostname - return `/c/${community.name}@${domain}` - } + return `/c/${community.name}@${domain}` + // } } @@ -26,6 +26,7 @@ width={24} height={24} class="rounded-full aspect-square object-cover" + loading="lazy" /> {/if} {community.title} diff --git a/src/routes/post/[id]/Avatar.svelte b/src/lib/components/ui/Avatar.svelte similarity index 51% rename from src/routes/post/[id]/Avatar.svelte rename to src/lib/components/ui/Avatar.svelte index 95905de2..6619e1db 100644 --- a/src/routes/post/[id]/Avatar.svelte +++ b/src/lib/components/ui/Avatar.svelte @@ -5,4 +5,10 @@ export let width: number -{toast.content}
+