From 472aa00e65b4b9a0ce2cd1717396a20696a88f04 Mon Sep 17 00:00:00 2001 From: Xylight <80978739+Xyphyn@users.noreply.github.com> Date: Sun, 4 May 2025 14:04:21 -0700 Subject: [PATCH] v2: replace @tanstack/svelte-virtual with custom virtualizer (#516) * impr: use custom virtualizer for post feeds * fix: use resizeobserver to update heights dynamically * fix: add overcsan and initialOffset for items * misc: update package.json (remove tanstack) * misc: use binary search to improve performance * fix: use translate for cheap gpu composite time * fix: reduce recalculate calls * feat: re-add list restore * fix: use relative instead of translations * misc: add ssr support to virtualizer * fix: onload scroll broken --- package.json | 1 - .../comment/CommentListVirtualizer.svelte | 65 ++---- .../lemmy/post/feed/VirtualFeed.svelte | 177 +++++++---------- src/lib/components/render/VirtualList.svelte | 186 ++++++++++++++++++ src/lib/i18n/en.json | 3 +- src/lib/lemmy/postfeed.svelte.ts | 6 + .../post/[instance]/[id=integer]/+page.svelte | 8 + .../[id=integer]/CommentProvider.svelte | 21 +- 8 files changed, 303 insertions(+), 164 deletions(-) create mode 100644 src/lib/components/render/VirtualList.svelte diff --git a/package.json b/package.json index 9e05f8e7..d32d76a5 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,6 @@ "@sveltejs/kit": "^2.17.1", "@sveltejs/vite-plugin-svelte": "^4.0.0", "@tailwindcss/container-queries": "^0.1.1", - "@tanstack/svelte-virtual": "^3.8.1", "@types/nprogress": "^0.2.0", "@types/ua-parser-js": "^0.7.39", "autoprefixer": "^10.4.16", diff --git a/src/lib/components/lemmy/comment/CommentListVirtualizer.svelte b/src/lib/components/lemmy/comment/CommentListVirtualizer.svelte index d77b39b7..aafdd93c 100644 --- a/src/lib/components/lemmy/comment/CommentListVirtualizer.svelte +++ b/src/lib/components/lemmy/comment/CommentListVirtualizer.svelte @@ -1,12 +1,9 @@ -
-
+ - {#each items as row, index (row.index)} -
+ {#snippet item(item, index)} +
[nodes[row.index]], (v) => (nodes[row.index] = v[0]) - } + bind:nodes={() => [nodes[index]], (v) => (nodes[index] = v[0])} {post} />
- {/each} -
+ {/snippet} +
diff --git a/src/lib/components/lemmy/post/feed/VirtualFeed.svelte b/src/lib/components/lemmy/post/feed/VirtualFeed.svelte index 4631dea3..35835c70 100644 --- a/src/lib/components/lemmy/post/feed/VirtualFeed.svelte +++ b/src/lib/components/lemmy/post/feed/VirtualFeed.svelte @@ -1,8 +1,5 @@ -
+ {#if !page.url.searchParams.get('noVirtualize')} + + {/if} {#await data.comments.value}
{#each new Array(10) as empty, index} @@ -400,6 +407,7 @@ focus={data.thread.value.focus} onupdate={reloadComments} bind:sort={data.commentSort.value} + virtualize={!page.url.searchParams.get('noVirtualize')} /> {#if comments.comments.length == 0} void focus?: string + virtualize?: boolean } let { @@ -40,6 +42,7 @@ sort = $bindable(), onupdate, focus, + virtualize = true, }: Props = $props() let comments = $state(passedComments) @@ -128,8 +131,16 @@
{/if} - +{#if virtualize} + +{:else} +
+
+ +
+
+{/if}