perf: dynamically load comments on posts
This commit is contained in:
1 parent
5407a0e62d
commit
1cde3a1f78
3 files changed
+91
-57
No files matched your search
@@ -0,0 +1,11 @@
|
|||||||
|
// to allow for loading before promises are finished on the client.
|
||||||
|
|
||||||
|
import { browser } from '$app/environment'
|
||||||
|
|
||||||
|
type AnyFunction = (...args: any[]) => any
|
||||||
|
|
||||||
|
export const awaitIfServer = async <T>(
|
||||||
|
promise: Promise<T>
|
||||||
|
): Promise<{
|
||||||
|
data: Promise<T> | T
|
||||||
|
}> => ({ data: browser ? promise : await promise })
|
||||||
@@ -21,7 +21,7 @@
|
|||||||
} from 'svelte-hero-icons'
|
} from 'svelte-hero-icons'
|
||||||
import PostLink from '$lib/components/lemmy/post/PostLink.svelte'
|
import PostLink from '$lib/components/lemmy/post/PostLink.svelte'
|
||||||
import PostMeta from '$lib/components/lemmy/post/PostMeta.svelte'
|
import PostMeta from '$lib/components/lemmy/post/PostMeta.svelte'
|
||||||
import { Material, Select, removeToast, toast } from 'mono-svelte'
|
import { Material, Select, Spinner, removeToast, toast } from 'mono-svelte'
|
||||||
import type { CommentSortType } from 'lemmy-js-client'
|
import type { CommentSortType } from 'lemmy-js-client'
|
||||||
import { profile } from '$lib/auth.js'
|
import { profile } from '$lib/auth.js'
|
||||||
import { instance } from '$lib/instance.js'
|
import { instance } from '$lib/instance.js'
|
||||||
@@ -47,17 +47,17 @@
|
|||||||
|
|
||||||
let post = data.post
|
let post = data.post
|
||||||
|
|
||||||
onMount(async () => {
|
// onMount(async () => {
|
||||||
if (
|
// if (
|
||||||
!(post.post_view.read && $userSettings.markPostsAsRead) &&
|
// !(post.post_view.read && $userSettings.markPostsAsRead) &&
|
||||||
$profile?.jwt
|
// $profile?.jwt
|
||||||
) {
|
// ) {
|
||||||
getClient().markPostAsRead({
|
// getClient().markPostAsRead({
|
||||||
read: $userSettings.markPostsAsRead,
|
// read: $userSettings.markPostsAsRead,
|
||||||
post_id: post.post_view.post.id,
|
// post_id: post.post_view.post.id,
|
||||||
})
|
// })
|
||||||
}
|
// }
|
||||||
})
|
// })
|
||||||
|
|
||||||
afterNavigate(async () => {
|
afterNavigate(async () => {
|
||||||
// reactivity hack
|
// reactivity hack
|
||||||
@@ -109,6 +109,8 @@
|
|||||||
|
|
||||||
$: remoteView =
|
$: remoteView =
|
||||||
$page.params.instance?.toLowerCase() != $instance.toLowerCase()
|
$page.params.instance?.toLowerCase() != $instance.toLowerCase()
|
||||||
|
|
||||||
|
const test = 'wow'
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<svelte:head>
|
<svelte:head>
|
||||||
@@ -302,36 +304,48 @@ flex-wrap gap-4 sticky top-20 w-full box-border z-20 mt-4"
|
|||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{#if $profile?.user}
|
{#await data.comments}
|
||||||
<CommentForm
|
<div class="flex flex-col gap-4">
|
||||||
postId={post.post_view.post.id}
|
{#each new Array(10) as empty}
|
||||||
on:comment={(comment) =>
|
<div class="animate-pulse flex flex-col gap-2 skeleton w-full">
|
||||||
(data.comments.comments = [
|
<div class="w-96 h-4" />
|
||||||
comment.detail.comment_view,
|
<div class="w-full h-12" />
|
||||||
...data.comments.comments,
|
<div class="w-48 h-4" />
|
||||||
])}
|
</div>
|
||||||
locked={post.post_view.post.locked ||
|
{/each}
|
||||||
$page.params.instance.toLowerCase() != $instance.toLowerCase()}
|
</div>
|
||||||
on:focus={() => (commenting = true)}
|
{:then comments}
|
||||||
tools={commenting}
|
{#if $profile?.user}
|
||||||
preview={commenting}
|
<CommentForm
|
||||||
placeholder={commenting ? undefined : $t('routes.post.addComment')}
|
postId={post.post_view.post.id}
|
||||||
rows={commenting ? 7 : 1}
|
on:comment={(comment) =>
|
||||||
|
(comments.comments = [
|
||||||
|
comment.detail.comment_view,
|
||||||
|
...comments.comments,
|
||||||
|
])}
|
||||||
|
locked={post.post_view.post.locked ||
|
||||||
|
$page.params.instance.toLowerCase() != $instance.toLowerCase()}
|
||||||
|
on:focus={() => (commenting = true)}
|
||||||
|
tools={commenting}
|
||||||
|
preview={commenting}
|
||||||
|
placeholder={commenting ? undefined : $t('routes.post.addComment')}
|
||||||
|
rows={commenting ? 7 : 1}
|
||||||
|
/>
|
||||||
|
{/if}
|
||||||
|
<Comments
|
||||||
|
post={post.post_view.post}
|
||||||
|
nodes={buildCommentsTree(
|
||||||
|
comments.comments,
|
||||||
|
undefined,
|
||||||
|
(c) =>
|
||||||
|
!(
|
||||||
|
($userSettings.hidePosts.deleted && c.comment.deleted) ||
|
||||||
|
($userSettings.hidePosts.removed && c.comment.removed)
|
||||||
|
)
|
||||||
|
)}
|
||||||
|
isParent={true}
|
||||||
/>
|
/>
|
||||||
{/if}
|
{/await}
|
||||||
<Comments
|
|
||||||
post={post.post_view.post}
|
|
||||||
nodes={buildCommentsTree(
|
|
||||||
data.comments.comments,
|
|
||||||
undefined,
|
|
||||||
(c) =>
|
|
||||||
!(
|
|
||||||
($userSettings.hidePosts.deleted && c.comment.deleted) ||
|
|
||||||
($userSettings.hidePosts.removed && c.comment.removed)
|
|
||||||
)
|
|
||||||
)}
|
|
||||||
isParent={true}
|
|
||||||
/>
|
|
||||||
{#if post.post_view.counts.comments > 5}
|
{#if post.post_view.counts.comments > 5}
|
||||||
<EndPlaceholder>
|
<EndPlaceholder>
|
||||||
<span class="text-black dark:text-white font-bold">
|
<span class="text-black dark:text-white font-bold">
|
||||||
@@ -350,3 +364,9 @@ flex-wrap gap-4 sticky top-20 w-full box-border z-20 mt-4"
|
|||||||
</EndPlaceholder>
|
</EndPlaceholder>
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<style lang="postcss">
|
||||||
|
.skeleton * {
|
||||||
|
@apply bg-slate-200 dark:bg-zinc-800 rounded-md;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@@ -1,22 +1,20 @@
|
|||||||
|
import { browser } from '$app/environment'
|
||||||
import { env } from '$env/dynamic/public'
|
import { env } from '$env/dynamic/public'
|
||||||
import { profile } from '$lib/auth.js'
|
import { profile } from '$lib/auth.js'
|
||||||
import CommunityCard from '$lib/components/lemmy/community/CommunityCard.svelte'
|
import CommunityCard from '$lib/components/lemmy/community/CommunityCard.svelte'
|
||||||
import { getClient } from '$lib/lemmy.js'
|
import { getClient } from '$lib/lemmy.js'
|
||||||
|
import { awaitIfServer } from '$lib/promise.js'
|
||||||
import { SSR_ENABLED, userSettings } from '$lib/settings.js'
|
import { SSR_ENABLED, userSettings } from '$lib/settings.js'
|
||||||
import { error } from '@sveltejs/kit'
|
import { error } from '@sveltejs/kit'
|
||||||
import { get } from 'svelte/store'
|
import { get } from 'svelte/store'
|
||||||
|
|
||||||
export async function load({ params, url, fetch }) {
|
export async function load({ params, url, fetch }) {
|
||||||
const post = await getClient(params.instance.toLowerCase(), fetch).getPost({
|
|
||||||
id: Number(params.id),
|
|
||||||
})
|
|
||||||
|
|
||||||
let max_depth = post.post_view.counts.comments > 100 ? 1 : 3
|
|
||||||
|
|
||||||
const thread = url.searchParams.get('thread')
|
const thread = url.searchParams.get('thread')
|
||||||
let parentId: number | undefined
|
let parentId: number | undefined
|
||||||
let showContext: string | undefined = undefined
|
let showContext: string | undefined = undefined
|
||||||
|
|
||||||
|
let max_depth = 3
|
||||||
|
|
||||||
if (thread) {
|
if (thread) {
|
||||||
const split = thread.split('.')
|
const split = thread.split('.')
|
||||||
if (split.length >= 9) {
|
if (split.length >= 9) {
|
||||||
@@ -49,21 +47,26 @@ export async function load({ params, url, fetch }) {
|
|||||||
parent_id: parentId,
|
parent_id: parentId,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const post = await getClient(params.instance.toLowerCase(), fetch).getPost({
|
||||||
|
id: Number(params.id),
|
||||||
|
})
|
||||||
|
const comments = getClient(params.instance, fetch).getComments(commentParams)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
thread: {
|
thread: {
|
||||||
showContext: showContext,
|
showContext: showContext,
|
||||||
singleThread: parentId != undefined
|
singleThread: parentId != undefined,
|
||||||
},
|
},
|
||||||
post: post,
|
post: post,
|
||||||
commentSort: sort,
|
commentSort: sort,
|
||||||
comments: await getClient(params.instance, fetch).getComments(commentParams),
|
comments: (await awaitIfServer(comments)).data,
|
||||||
slots: {
|
// slots: {
|
||||||
sidebar: {
|
// sidebar: {
|
||||||
component: CommunityCard,
|
// component: CommunityCard,
|
||||||
props: {
|
// props: {
|
||||||
community_view: post.community_view,
|
// community_view: post.community_view,
|
||||||
},
|
// },
|
||||||
},
|
// },
|
||||||
},
|
// },
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user