Skip to content

Commit

Permalink
fix: decrease site fetches
Browse files Browse the repository at this point in the history
  • Loading branch information
Xyphyn committed Sep 15, 2023
1 parent b7038b1 commit da6f4ba
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 29 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "photon-lemmy",
"version": "1.6.5",
"version": "1.7.0",
"private": true,
"scripts": {
"dev": "vite dev",
Expand Down
17 changes: 16 additions & 1 deletion src/lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import { getClient, site } from '$lib/lemmy.js'
import { getInbox, getInboxItemPublished } from '$lib/lemmy/inbox.js'
import { userSettings } from '$lib/settings.js'
import { moveItem } from '$lib/util.js'
import type { GetSiteResponse, MyUserInfo } from 'lemmy-js-client'
import {
LemmyHttp,
type GetSiteResponse,
type MyUserInfo,
} from 'lemmy-js-client'
import { get, writable } from 'svelte/store'

const getDefaultProfile = (): Profile => ({
Expand Down Expand Up @@ -197,9 +201,20 @@ const serializeUser = (user: Profile): Profile => ({
user: undefined,
})

instance.subscribe(async (i) => {
try {
// fetching site is handled by auth.ts if logged in
if (get(profile)?.jwt) return
const s = await new LemmyHttp(`https://${i}`).getSite()

site.set(s)
} catch (e) {}
})

export async function setUserID(id: number) {
const pd = get(profileData)
if (id == -1) {
instance.set(DEFAULT_INSTANCE_URL)
resetProfile()
return
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/components/ui/Navbar.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
Powered by <Link
href="https://github.com/Xyphyn/photon"
highlight={false}
class="font-bold flex items-center gap-0.5 hover:underline text-black dark:text-white"
class="!font-bold flex items-center gap-0.5 hover:underline text-black dark:text-white"
>
<Logo width={12} /> Photon
</Link>
Expand Down
6 changes: 0 additions & 6 deletions src/lib/lemmy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ export const getInstance = () => get(instance)

export const site = writable<GetSiteResponse | undefined>(undefined)

if (LINKED_INSTANCE_URL) {
getClient(LINKED_INSTANCE_URL)
.getSite({})
.then((s) => site.set(s))
}

export async function validateInstance(instance: string): Promise<boolean> {
if (instance == '') return false

Expand Down
2 changes: 0 additions & 2 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@
$: webManifest = pwaInfo ? pwaInfo.webManifest.linkTag : ''
$: darkTheme = $theme && inDarkTheme()
$: console.log(darkTheme)
onMount(() => {
if (browser) {
window
Expand Down
27 changes: 12 additions & 15 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import { GlobeAmericas, Icon } from 'svelte-hero-icons'
import { profile } from '$lib/auth.js'
import ViewSelect from '$lib/components/lemmy/ViewSelect.svelte'
import { site } from '$lib/lemmy.js'
export let data
Expand All @@ -23,13 +24,13 @@
<Modal bind:open={sidebar}>
<span slot="title">About</span>
<div class="mx-auto">
{#await data.streamed.site then site}
{#if $site}
<SiteCard
site={site.site_view}
taglines={site.taglines}
admins={site.admins}
site={$site.site_view}
taglines={$site.taglines}
admins={$site.admins}
/>
{/await}
{/if}
</div>
</Modal>

Expand Down Expand Up @@ -75,22 +76,18 @@
</div>
</div>
<div class="hidden xl:block">
{#await data.streamed.site}
{#if !$site}
<StickyCard>
<div class="h-64 grid place-items-center">
<Spinner width={32} />
</div>
</StickyCard>
{:then site}
{:else}
<SiteCard
site={site.site_view}
taglines={site.taglines}
admins={site.admins}
site={$site.site_view}
taglines={$site.taglines}
admins={$site.admins}
/>
{:catch}
<StickyCard>
<h1 class="font-bold text-lg">Failed to load</h1>
</StickyCard>
{/await}
{/if}
</div>
</div>
3 changes: 0 additions & 3 deletions src/routes/+page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ export async function load({ url, fetch }) {
type_: listingType,
auth: get(profile)?.jwt,
}),
streamed: {
site: getClient(undefined, fetch).getSite({}),
},
}
} catch (err) {
throw error(500, {
Expand Down

0 comments on commit da6f4ba

Please sign in to comment.