fix: posts not reloading when changing instance

This commit is contained in:
Xylight committed 2024-07-06 18:14:29 -07:00
1 parent c8efc4c44b
commit 6a4a60ec7d
2 files changed
+6 -9

No files matched your search

+4 -7
View File
@@ -1,5 +1,5 @@
<script lang="ts" context="module"> <script lang="ts" context="module">
interface CachedPosts { export interface CachedPosts {
data: GetPostsResponse data: GetPostsResponse
params: URLSearchParams params: URLSearchParams
lastSeen: number lastSeen: number
@@ -9,14 +9,11 @@
export const _posts = writable<CachedPosts>(undefined) export const _posts = writable<CachedPosts>(undefined)
export const shouldReload = ( export const shouldReload = (
cache: CachedPosts, cache: CachedPosts | undefined,
params: string, params: string,
instance: string instance: string
): boolean => ): boolean =>
!( cache?.instance != instance || cache?.params.toString() != params
cache &&
(params == cache.params.toString() || cache.instance != instance)
)
</script> </script>
<script lang="ts"> <script lang="ts">
@@ -112,7 +109,7 @@
const observer = new IntersectionObserver(callback, { const observer = new IntersectionObserver(callback, {
root: null, root: null,
rootMargin: '0px', rootMargin: '0px',
threshold: 0.5, threshold: 0.25,
}) })
const elements = document.querySelectorAll('.post-container') const elements = document.querySelectorAll('.post-container')
+2 -2
View File
@@ -3,7 +3,7 @@ import { userSettings } from '$lib/settings.js'
import type { GetPostsResponse, ListingType, SortType } from 'lemmy-js-client' import type { GetPostsResponse, ListingType, SortType } from 'lemmy-js-client'
import { get, writable } from 'svelte/store' import { get, writable } from 'svelte/store'
import { error } from '@sveltejs/kit' import { error } from '@sveltejs/kit'
import { _posts, shouldReload } from './+page.svelte' import { _posts, shouldReload, type CachedPosts } from './+page.svelte'
import { browser } from '$app/environment' import { browser } from '$app/environment'
import { instance } from '$lib/instance' import { instance } from '$lib/instance'
@@ -19,7 +19,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
const cached = get(_posts) const cached: CachedPosts | undefined = get(_posts)
const posts = shouldReload(cached, url.searchParams.toString(), get(instance)) const posts = shouldReload(cached, url.searchParams.toString(), get(instance))
? await getClient(undefined, fetch).getPosts({ ? await getClient(undefined, fetch).getPosts({