fix: reload posts if instance changes
This commit is contained in:
1 parent
0b13cbc429
commit
802b11e1ed
2 files changed
+37
-17
No files matched your search
+18
-3
@@ -1,9 +1,22 @@
|
||||
<script lang="ts" context="module">
|
||||
export const _posts = writable<{
|
||||
interface CachedPosts {
|
||||
data: GetPostsResponse
|
||||
params: URLSearchParams
|
||||
lastSeen: number
|
||||
}>(undefined)
|
||||
instance: string
|
||||
}
|
||||
|
||||
export const _posts = writable<CachedPosts>(undefined)
|
||||
|
||||
export const shouldReload = (
|
||||
cache: CachedPosts,
|
||||
params: string,
|
||||
instance: string
|
||||
): boolean =>
|
||||
!(
|
||||
cache &&
|
||||
(params == cache.params.toString() || cache.instance != instance)
|
||||
)
|
||||
</script>
|
||||
|
||||
<script lang="ts">
|
||||
@@ -27,12 +40,13 @@
|
||||
import { t } from '$lib/translations.js'
|
||||
import InfiniteScroll from 'svelte-infinite-scroll'
|
||||
import { Button, Material, Spinner } from 'mono-svelte'
|
||||
import { writable } from 'svelte/store'
|
||||
import { get, writable } from 'svelte/store'
|
||||
import type { GetPostsResponse } from 'lemmy-js-client'
|
||||
import { userSettings } from '$lib/settings.js'
|
||||
import { afterNavigate } from '$app/navigation'
|
||||
import { browser } from '$app/environment'
|
||||
import { onMount } from 'svelte'
|
||||
import { instance } from '$lib/instance.js'
|
||||
|
||||
export let data
|
||||
|
||||
@@ -69,6 +83,7 @@
|
||||
data: data.posts,
|
||||
params: ps.params,
|
||||
lastSeen: ps.lastSeen,
|
||||
instance: ps.instance,
|
||||
}))
|
||||
|
||||
loading = false
|
||||
|
||||
+19
-14
@@ -3,8 +3,9 @@ import { userSettings } from '$lib/settings.js'
|
||||
import type { GetPostsResponse, ListingType, SortType } from 'lemmy-js-client'
|
||||
import { get, writable } from 'svelte/store'
|
||||
import { error } from '@sveltejs/kit'
|
||||
import { _posts } from './+page.svelte'
|
||||
import { _posts, shouldReload } from './+page.svelte'
|
||||
import { browser } from '$app/environment'
|
||||
import { instance } from '$lib/instance'
|
||||
|
||||
export async function load({ url, fetch }) {
|
||||
const cursor = url.searchParams.get('cursor') as string | undefined
|
||||
@@ -20,23 +21,27 @@ export async function load({ url, fetch }) {
|
||||
|
||||
const cached = get(_posts)
|
||||
|
||||
const posts =
|
||||
cached && url.searchParams.toString() == cached.params.toString()
|
||||
? cached.data
|
||||
: await getClient(undefined, fetch).getPosts({
|
||||
limit: 20,
|
||||
page: page,
|
||||
sort: sort,
|
||||
type_: listingType,
|
||||
page_cursor: cursor,
|
||||
show_hidden: settings.posts.showHidden,
|
||||
})
|
||||
const posts = shouldReload(cached, url.searchParams.toString(), get(instance))
|
||||
? await getClient(undefined, fetch).getPosts({
|
||||
limit: 20,
|
||||
page: page,
|
||||
sort: sort,
|
||||
type_: listingType,
|
||||
page_cursor: cursor,
|
||||
show_hidden: settings.posts.showHidden,
|
||||
})
|
||||
: cached.data
|
||||
|
||||
if (
|
||||
!(cached && url.searchParams.toString() == cached.params.toString()) &&
|
||||
shouldReload(cached, url.searchParams.toString(), get(instance)) &&
|
||||
browser
|
||||
)
|
||||
_posts.set({ data: posts, params: url.searchParams, lastSeen: 0 })
|
||||
_posts.set({
|
||||
data: posts,
|
||||
params: url.searchParams,
|
||||
lastSeen: 0,
|
||||
instance: get(instance),
|
||||
})
|
||||
|
||||
try {
|
||||
return {
|
||||
|
||||
Reference in new issue
Block a user