perf: async load posts on main page
This commit is contained in:
4 files changed
+81
-58
No files matched your search
@@ -1,9 +1,8 @@
|
||||
import { browser } from '$app/environment'
|
||||
import { env } from '$env/dynamic/public'
|
||||
import { settings } from '$lib/settings.svelte'
|
||||
import { aliases, loadTranslations } from '$lib/i18n/translations.js'
|
||||
|
||||
export const ssr = (env.PUBLIC_SSR_ENABLED?.toLowerCase() ?? 'false') == 'true'
|
||||
export const ssr = false
|
||||
|
||||
export const load = async () => {
|
||||
if (browser) {
|
||||
|
||||
+60
-37
@@ -6,25 +6,27 @@
|
||||
import PostFeed from '$lib/components/lemmy/post/feed/PostFeed.svelte'
|
||||
import VirtualFeed from '$lib/components/lemmy/post/feed/VirtualFeed.svelte'
|
||||
import EndPlaceholder from '$lib/components/ui/EndPlaceholder.svelte'
|
||||
import LabelStat from '$lib/components/ui/LabelStat.svelte'
|
||||
import Skeleton from '$lib/components/ui/generic/Skeleton.svelte'
|
||||
import Header from '$lib/components/ui/layout/pages/Header.svelte'
|
||||
import Tabs from '$lib/components/ui/layout/pages/Tabs.svelte'
|
||||
import Pageination from '$lib/components/ui/Pageination.svelte'
|
||||
import { t } from '$lib/i18n/translations.js'
|
||||
import { site } from '$lib/lemmy.svelte.js'
|
||||
import { postFeeds } from '$lib/lemmy/postfeed.svelte.js'
|
||||
import { settings } from '$lib/settings.svelte.js'
|
||||
import Button from 'mono-svelte/button/Button.svelte'
|
||||
import { onMount } from 'svelte'
|
||||
import { ArrowRight, ChartBar, Icon } from 'svelte-hero-icons'
|
||||
|
||||
let { data = $bindable() } = $props()
|
||||
|
||||
let type = $state(data.feed.value.type_)
|
||||
let sort = $state(data.feed.value.sort)
|
||||
|
||||
$effect(() => {
|
||||
if (type) settings.defaultSort.feed = type
|
||||
if (sort) settings.defaultSort.sort = sort
|
||||
onMount(async () => {
|
||||
await data.feed.value
|
||||
$effect(() => {
|
||||
if (data.filters.value.type_)
|
||||
settings.defaultSort.feed = data.filters.value.type_
|
||||
if (data.filters.value.sort)
|
||||
settings.defaultSort.sort = data.filters.value.sort
|
||||
})
|
||||
})
|
||||
|
||||
const FeedComponent = $derived(
|
||||
@@ -44,10 +46,19 @@
|
||||
|
||||
{#snippet extended()}
|
||||
<form class="flex items-center gap-2" method="get" action="/">
|
||||
{#if type}
|
||||
<Location name="type" navigate bind:selected={type} />
|
||||
{#if data.filters.value.type_}
|
||||
<Location
|
||||
name="type"
|
||||
navigate
|
||||
bind:selected={data.filters.value.type_}
|
||||
/>
|
||||
{/if}
|
||||
<Sort placement="bottom" name="sort" navigate bind:selected={sort} />
|
||||
<Sort
|
||||
placement="bottom"
|
||||
name="sort"
|
||||
navigate
|
||||
bind:selected={data.filters.value.sort}
|
||||
/>
|
||||
<ViewSelect placement="bottom" />
|
||||
|
||||
<noscript>
|
||||
@@ -68,31 +79,43 @@
|
||||
{/snippet}
|
||||
</EndPlaceholder> -->
|
||||
|
||||
<FeedComponent
|
||||
bind:posts={data.feed.value.posts.posts}
|
||||
bind:feedData={() => data.feed.value, v => (postFeeds.value.main.data = v)}
|
||||
feedId="main"
|
||||
/>
|
||||
<svelte:element
|
||||
this={settings.infiniteScroll && !settings.posts.noVirtualize
|
||||
? 'noscript'
|
||||
: 'div'}
|
||||
class="mt-auto flex flex-col"
|
||||
>
|
||||
<EndPlaceholder>
|
||||
<Icon src={ChartBar} size="16" mini />
|
||||
{#await data.feed.value}
|
||||
{#each new Array(5) as _, index}{_}
|
||||
<div
|
||||
class="animate-pop-in"
|
||||
style="animation-delay: {index * 50}ms; opacity: 0; width: {(1 /
|
||||
((index + 1) % 3)) *
|
||||
100}%"
|
||||
>
|
||||
<Skeleton />
|
||||
</div>{/each}
|
||||
{:then feed}
|
||||
<FeedComponent
|
||||
bind:posts={feed.posts.posts}
|
||||
bind:feedData={() => feed, v => (postFeeds.value.main.data = v)}
|
||||
feedId="main"
|
||||
/>
|
||||
<svelte:element
|
||||
this={settings.infiniteScroll && !settings.posts.noVirtualize
|
||||
? 'noscript'
|
||||
: 'div'}
|
||||
class="mt-auto flex flex-col"
|
||||
>
|
||||
<EndPlaceholder>
|
||||
<Icon src={ChartBar} size="16" mini />
|
||||
|
||||
{$t('routes.frontpage.footer', {
|
||||
users: site.data?.site_view?.counts?.users_active_day ?? '??',
|
||||
})}
|
||||
{#snippet action()}
|
||||
<Pageination
|
||||
cursor={{ next: data.feed.value.cursor.next }}
|
||||
href={page =>
|
||||
typeof page == 'number' ? `?page=${page}` : `?cursor=${page}`}
|
||||
back={false}
|
||||
/>
|
||||
{/snippet}
|
||||
</EndPlaceholder>
|
||||
</svelte:element>
|
||||
{$t('routes.frontpage.footer', {
|
||||
users: site.data?.site_view?.counts?.users_active_day ?? '??',
|
||||
})}
|
||||
{#snippet action()}
|
||||
<Pageination
|
||||
cursor={{ next: feed.cursor.next }}
|
||||
href={page =>
|
||||
typeof page == 'number' ? `?page=${page}` : `?cursor=${page}`}
|
||||
back={false}
|
||||
/>
|
||||
{/snippet}
|
||||
</EndPlaceholder>
|
||||
</svelte:element>
|
||||
{/await}
|
||||
</div>
|
||||
+19
-15
@@ -1,6 +1,6 @@
|
||||
import { t } from '$lib/i18n/translations.js'
|
||||
import { postFeed } from '$lib/lemmy/postfeed.svelte.js'
|
||||
import { ReactiveState } from '$lib/promise.svelte.js'
|
||||
import { ReactiveState, awaitIfServer } from '$lib/promise.svelte.js'
|
||||
import { settings } from '$lib/settings.svelte'
|
||||
import type { ListingType, SortType } from 'lemmy-js-client'
|
||||
import { ChevronDoubleUp } from 'svelte-hero-icons'
|
||||
@@ -13,21 +13,25 @@ export async function load({ url, fetch }) {
|
||||
const listingType: ListingType =
|
||||
(url.searchParams.get('type') as ListingType) || settings.defaultSort.feed
|
||||
|
||||
const feed = postFeed({
|
||||
id: 'main',
|
||||
request: {
|
||||
page_cursor: cursor,
|
||||
sort: sort,
|
||||
type_: listingType,
|
||||
limit: 20,
|
||||
show_hidden: settings.posts.showHidden,
|
||||
},
|
||||
url: url,
|
||||
fetch: fetch,
|
||||
})
|
||||
|
||||
return {
|
||||
feed: new ReactiveState(
|
||||
await postFeed({
|
||||
id: 'main',
|
||||
request: {
|
||||
page_cursor: cursor,
|
||||
sort: sort,
|
||||
type_: listingType,
|
||||
limit: 20,
|
||||
show_hidden: settings.posts.showHidden,
|
||||
},
|
||||
url: url,
|
||||
fetch: fetch,
|
||||
}),
|
||||
),
|
||||
feed: new ReactiveState((await awaitIfServer(feed)).data),
|
||||
filters: new ReactiveState({
|
||||
sort: sort,
|
||||
type_: listingType,
|
||||
}),
|
||||
contextual: {
|
||||
actions: [
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user