Skip to content

Commit ff9f91e

Browse files
committed
fix: caching conditions in library view
Signed-off-by: Fernando Fernández <[email protected]>
1 parent cefe3f7 commit ff9f91e

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

frontend/src/composables/apis.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,15 @@ function _sharedInternalLogic<T extends Record<K, (...args: any[]) => any>, K ex
210210
/**
211211
* TODO: Check why previous returns unknown by default without the type annotation
212212
*/
213-
const cachedData = computed<ReturnType<typeof apiStore.getCachedRequest> | undefined>((previous) =>
214-
loading.value || isNil(loading.value) ? previous : apiStore.getCachedRequest(`${String(unref(api)?.name)}.${String(unref(methodName))}`, stringArgs.value)
215-
);
213+
const cachedData = computed<ReturnType<typeof apiStore.getCachedRequest> | undefined>((previous) => {
214+
const currentCachedRequest = apiStore.getCachedRequest(`${String(unref(api)?.name)}.${String(unref(methodName))}`, stringArgs.value);
215+
216+
if ((loading.value || isNil(loading.value)) && !currentCachedRequest) {
217+
return previous;
218+
}
219+
220+
return currentCachedRequest;
221+
});
216222
const isCached = computed(() => Boolean(cachedData.value));
217223
const data = computed<ReturnData<T, K, typeof ofBaseItem>>(() => {
218224
if (ops.skipCache.request && result.value) {

frontend/src/pages/library/[itemId].vue

+12-9
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
<VChip
1010
size="small"
1111
class="ma-2 hidden-sm-and-down">
12-
<template v-if="loading">
13-
{{ t('lazyLoading', { value: lazyLoadLimit }) }}
12+
<template v-if="!fullQueryIsCached">
13+
{{ t('lazyLoading', { value: items.length }) }}
1414
</template>
1515
<template v-else>
1616
{{ items?.length ?? 0 }}
@@ -73,7 +73,7 @@ import { getItemsApi } from '@jellyfin/sdk/lib/utils/api/items-api';
7373
import { getMusicGenresApi } from '@jellyfin/sdk/lib/utils/api/music-genres-api';
7474
import { getPersonsApi } from '@jellyfin/sdk/lib/utils/api/persons-api';
7575
import { getStudiosApi } from '@jellyfin/sdk/lib/utils/api/studios-api';
76-
import { computed, onMounted, ref, shallowRef } from 'vue';
76+
import { computed, onBeforeMount, ref, shallowRef } from 'vue';
7777
import { useI18n } from 'vue-i18n';
7878
import { useRoute } from 'vue-router/auto';
7979
@@ -93,7 +93,7 @@ const innerItemKind = shallowRef<BaseItemKind>();
9393
const sortBy = shallowRef<string>();
9494
const sortAscending = shallowRef(true);
9595
const queryLimit = shallowRef<number | undefined>(lazyLoadLimit);
96-
const lazyLoadIds = shallowRef<string[]>([]);
96+
const lazyLoadIds = shallowRef<BaseItemDto['Id'][]>([]);
9797
const filters = ref<Filters>({
9898
status: [],
9999
features: [],
@@ -222,17 +222,20 @@ const { loading, data: queryItems } = await useBaseItem(api, method)(() => ({
222222
limit: queryLimit.value
223223
}));
224224
225-
const items = computed(() => {
226-
return queryLimit.value ? queryItems.value : [...(apiStore.getItemsById(lazyLoadIds.value) as BaseItemDto[]), ...queryItems.value];
227-
});
225+
/**
226+
* The queryItems for the 2nd request will return the items from (lazyloadLimit, n],
227+
* so checking if just the first matches is a good solution
228+
*/
229+
const fullQueryIsCached = computed(() => loading.value ? !queryLimit.value && queryItems.value[0].Id !== lazyLoadIds.value[0] : true);
230+
const items = computed(() => fullQueryIsCached.value ? [...(apiStore.getItemsById(lazyLoadIds.value) as BaseItemDto[]), ...queryItems.value] : queryItems.value);
228231
229232
route.meta.title = library.value.Name;
230233
231234
/**
232235
* We fetch the 1st 100 items and, after mount, we fetch the rest.
233236
*/
234-
onMounted(() => {
235-
lazyLoadIds.value = queryItems.value.map((i) => i.Id as string);
237+
onBeforeMount(() => {
238+
lazyLoadIds.value = queryItems.value.map((i) => i.Id);
236239
queryLimit.value = undefined;
237240
});
238241
</script>

0 commit comments

Comments
 (0)