perf: async load posts on main page
This commit is contained in:
4 files changed
+47
-24
No files matched your search
@@ -3,9 +3,6 @@ import { env } from '$env/dynamic/public'
|
|||||||
import { locale } from './i18n/translations'
|
import { locale } from './i18n/translations'
|
||||||
import { browser } from '$app/environment'
|
import { browser } from '$app/environment'
|
||||||
|
|
||||||
console.log('Using the following default settings from the environment:')
|
|
||||||
console.log(env)
|
|
||||||
|
|
||||||
export type View = 'cozy' | 'compact'
|
export type View = 'cozy' | 'compact'
|
||||||
|
|
||||||
export const SSR_ENABLED = env.PUBLIC_SSR_ENABLED?.toLowerCase() == 'true'
|
export const SSR_ENABLED = env.PUBLIC_SSR_ENABLED?.toLowerCase() == 'true'
|
||||||
@@ -230,7 +227,7 @@ function isObject(item: object) {
|
|||||||
* @param target
|
* @param target
|
||||||
* @param ...sources
|
* @param ...sources
|
||||||
*/
|
*/
|
||||||
// eslint-disable-next-line
|
|
||||||
function mergeDeep(target: any, ...sources: any[]) {
|
function mergeDeep(target: any, ...sources: any[]) {
|
||||||
if (!sources.length) return target
|
if (!sources.length) return target
|
||||||
const source = sources.shift()
|
const source = sources.shift()
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
import { browser } from '$app/environment'
|
import { browser } from '$app/environment'
|
||||||
import { env } from '$env/dynamic/public'
|
|
||||||
import { settings } from '$lib/settings.svelte'
|
import { settings } from '$lib/settings.svelte'
|
||||||
import { aliases, loadTranslations } from '$lib/i18n/translations.js'
|
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 () => {
|
export const load = async () => {
|
||||||
if (browser) {
|
if (browser) {
|
||||||
|
|||||||
+36
-13
@@ -6,25 +6,27 @@
|
|||||||
import PostFeed from '$lib/components/lemmy/post/feed/PostFeed.svelte'
|
import PostFeed from '$lib/components/lemmy/post/feed/PostFeed.svelte'
|
||||||
import VirtualFeed from '$lib/components/lemmy/post/feed/VirtualFeed.svelte'
|
import VirtualFeed from '$lib/components/lemmy/post/feed/VirtualFeed.svelte'
|
||||||
import EndPlaceholder from '$lib/components/ui/EndPlaceholder.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 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 Pageination from '$lib/components/ui/Pageination.svelte'
|
||||||
import { t } from '$lib/i18n/translations.js'
|
import { t } from '$lib/i18n/translations.js'
|
||||||
import { site } from '$lib/lemmy.svelte.js'
|
import { site } from '$lib/lemmy.svelte.js'
|
||||||
import { postFeeds } from '$lib/lemmy/postfeed.svelte.js'
|
import { postFeeds } from '$lib/lemmy/postfeed.svelte.js'
|
||||||
import { settings } from '$lib/settings.svelte.js'
|
import { settings } from '$lib/settings.svelte.js'
|
||||||
import Button from 'mono-svelte/button/Button.svelte'
|
import Button from 'mono-svelte/button/Button.svelte'
|
||||||
|
import { onMount } from 'svelte'
|
||||||
import { ArrowRight, ChartBar, Icon } from 'svelte-hero-icons'
|
import { ArrowRight, ChartBar, Icon } from 'svelte-hero-icons'
|
||||||
|
|
||||||
let { data = $bindable() } = $props()
|
let { data = $bindable() } = $props()
|
||||||
|
|
||||||
let type = $state(data.feed.value.type_)
|
onMount(async () => {
|
||||||
let sort = $state(data.feed.value.sort)
|
await data.feed.value
|
||||||
|
|
||||||
$effect(() => {
|
$effect(() => {
|
||||||
if (type) settings.defaultSort.feed = type
|
if (data.filters.value.type_)
|
||||||
if (sort) settings.defaultSort.sort = sort
|
settings.defaultSort.feed = data.filters.value.type_
|
||||||
|
if (data.filters.value.sort)
|
||||||
|
settings.defaultSort.sort = data.filters.value.sort
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
const FeedComponent = $derived(
|
const FeedComponent = $derived(
|
||||||
@@ -44,10 +46,19 @@
|
|||||||
|
|
||||||
{#snippet extended()}
|
{#snippet extended()}
|
||||||
<form class="flex items-center gap-2" method="get" action="/">
|
<form class="flex items-center gap-2" method="get" action="/">
|
||||||
{#if type}
|
{#if data.filters.value.type_}
|
||||||
<Location name="type" navigate bind:selected={type} />
|
<Location
|
||||||
|
name="type"
|
||||||
|
navigate
|
||||||
|
bind:selected={data.filters.value.type_}
|
||||||
|
/>
|
||||||
{/if}
|
{/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" />
|
<ViewSelect placement="bottom" />
|
||||||
|
|
||||||
<noscript>
|
<noscript>
|
||||||
@@ -68,9 +79,20 @@
|
|||||||
{/snippet}
|
{/snippet}
|
||||||
</EndPlaceholder> -->
|
</EndPlaceholder> -->
|
||||||
|
|
||||||
|
{#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
|
<FeedComponent
|
||||||
bind:posts={data.feed.value.posts.posts}
|
bind:posts={feed.posts.posts}
|
||||||
bind:feedData={() => data.feed.value, v => (postFeeds.value.main.data = v)}
|
bind:feedData={() => feed, v => (postFeeds.value.main.data = v)}
|
||||||
feedId="main"
|
feedId="main"
|
||||||
/>
|
/>
|
||||||
<svelte:element
|
<svelte:element
|
||||||
@@ -87,7 +109,7 @@
|
|||||||
})}
|
})}
|
||||||
{#snippet action()}
|
{#snippet action()}
|
||||||
<Pageination
|
<Pageination
|
||||||
cursor={{ next: data.feed.value.cursor.next }}
|
cursor={{ next: feed.cursor.next }}
|
||||||
href={page =>
|
href={page =>
|
||||||
typeof page == 'number' ? `?page=${page}` : `?cursor=${page}`}
|
typeof page == 'number' ? `?page=${page}` : `?cursor=${page}`}
|
||||||
back={false}
|
back={false}
|
||||||
@@ -95,4 +117,5 @@
|
|||||||
{/snippet}
|
{/snippet}
|
||||||
</EndPlaceholder>
|
</EndPlaceholder>
|
||||||
</svelte:element>
|
</svelte:element>
|
||||||
|
{/await}
|
||||||
</div>
|
</div>
|
||||||
+9
-5
@@ -1,6 +1,6 @@
|
|||||||
import { t } from '$lib/i18n/translations.js'
|
import { t } from '$lib/i18n/translations.js'
|
||||||
import { postFeed } from '$lib/lemmy/postfeed.svelte.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 { settings } from '$lib/settings.svelte'
|
||||||
import type { ListingType, SortType } from 'lemmy-js-client'
|
import type { ListingType, SortType } from 'lemmy-js-client'
|
||||||
import { ChevronDoubleUp } from 'svelte-hero-icons'
|
import { ChevronDoubleUp } from 'svelte-hero-icons'
|
||||||
@@ -13,9 +13,7 @@ export async function load({ url, fetch }) {
|
|||||||
const listingType: ListingType =
|
const listingType: ListingType =
|
||||||
(url.searchParams.get('type') as ListingType) || settings.defaultSort.feed
|
(url.searchParams.get('type') as ListingType) || settings.defaultSort.feed
|
||||||
|
|
||||||
return {
|
const feed = postFeed({
|
||||||
feed: new ReactiveState(
|
|
||||||
await postFeed({
|
|
||||||
id: 'main',
|
id: 'main',
|
||||||
request: {
|
request: {
|
||||||
page_cursor: cursor,
|
page_cursor: cursor,
|
||||||
@@ -26,8 +24,14 @@ export async function load({ url, fetch }) {
|
|||||||
},
|
},
|
||||||
url: url,
|
url: url,
|
||||||
fetch: fetch,
|
fetch: fetch,
|
||||||
|
})
|
||||||
|
|
||||||
|
return {
|
||||||
|
feed: new ReactiveState((await awaitIfServer(feed)).data),
|
||||||
|
filters: new ReactiveState({
|
||||||
|
sort: sort,
|
||||||
|
type_: listingType,
|
||||||
}),
|
}),
|
||||||
),
|
|
||||||
contextual: {
|
contextual: {
|
||||||
actions: [
|
actions: [
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in new issue
Block a user