added own caching for ical feed

This commit is contained in:
Elmar Kresse
2024-07-22 20:23:56 +02:00
parent a9084d62f6
commit 08dc4b9185
6 changed files with 56 additions and 13 deletions

View File

@ -32,7 +32,7 @@ import iCalenderPlugin from "@fullcalendar/icalendar";
import router from "@/router"; import router from "@/router";
import { formatYearMonthDay } from "@/helpers/dates.ts"; import { formatYearMonthDay } from "@/helpers/dates.ts";
import { useI18n } from "vue-i18n"; import { useI18n } from "vue-i18n";
import { useQuery } from "@tanstack/vue-query"; import { useQuery, useQueryClient } from "@tanstack/vue-query";
import tokenStore from "@/store/tokenStore.ts"; import tokenStore from "@/store/tokenStore.ts";
import { parseICalData } from "@/helpers/ical.ts"; import { parseICalData } from "@/helpers/ical.ts";
import { fetchICalendarEvents } from "@/api/loadICal.ts"; import { fetchICalendarEvents } from "@/api/loadICal.ts";
@ -71,20 +71,39 @@ const toggle = (info: EventClickArg) => {
} }
}; };
const selectedToken = computed(() => props.token);
const selectedToken = computed(() => {
return props.token;
});
const mobilePage = inject("mobilePage") as Ref<boolean>; const mobilePage = inject("mobilePage") as Ref<boolean>;
const date: Ref<Date> = ref(new Date()); const date: Ref<Date> = ref(new Date());
const { data: calendar } = useQuery({ const { data: calendar} = useQuery({
queryKey: ["userCalendar", selectedToken], queryKey: ["userCalendar", selectedToken],
queryFn: () => fetchICalendarEvents(selectedToken.value), queryFn: () => fetchICalendarEvents(selectedToken.value),
select: (data) => { select: (data) => {
return data; return data;
}, },
staleTime: 12 * 60 * 60 * 1000, // 12 hours
refetchOnWindowFocus: "always",
refetchOnReconnect: "always",
networkMode: "offlineFirst", networkMode: "offlineFirst",
enabled: () => tokenStore().token !== "", enabled: () => tokenStore().token !== ""
staleTime: 5000000, // 500 seconds });
const queryClient = useQueryClient();
const invalidateAndRefetchCalendar = () => {
console.debug("invalidateAndRefetchCalendar", selectedToken);
const queryKey = ["userCalendar", selectedToken];
queryClient.invalidateQueries({queryKey: queryKey}).then(() => {
queryClient.refetchQueries({queryKey: queryKey});
});
};
defineExpose({
invalidateAndRefetchCalendar
}); });
const events = computed(() => { const events = computed(() => {

View File

@ -94,7 +94,8 @@
"success": "Erfolg", "success": "Erfolg",
"error": "Fehler", "error": "Fehler",
"successDetail": "Kalender erfolgreich gelöscht", "successDetail": "Kalender erfolgreich gelöscht",
"errorDetail": "Fehler beim Löschen des Kalenders" "errorDetail": "Fehler beim Löschen des Kalenders",
"successDetailLoad": "Kalender erfolgreich geladen"
} }
}, },
"additionalModules": { "additionalModules": {

View File

@ -94,7 +94,8 @@
"success": "Success", "success": "Success",
"error": "Error", "error": "Error",
"successDetail": "calendar successfully deleted", "successDetail": "calendar successfully deleted",
"errorDetail": "calendar could not be deleted" "errorDetail": "calendar could not be deleted",
"successDetailLoad": "calendar successfully loaded"
} }
}, },
"additionalModules": { "additionalModules": {

View File

@ -94,7 +94,8 @@
"success": "成功", "success": "成功",
"error": "エラー", "error": "エラー",
"successDetail": "カレンダーが正常に削除されました", "successDetail": "カレンダーが正常に削除されました",
"errorDetail": "カレンダーを削除できませんでした" "errorDetail": "カレンダーを削除できませんでした",
"successDetailLoad": "カレンダーが正常に読み込まれました"
} }
}, },
"additionalModules": { "additionalModules": {

View File

@ -22,6 +22,8 @@ if (tokenFromUrl) {
loadCalendar(); loadCalendar();
} }
const calendarViewerRef = ref<InstanceType<typeof CalendarViewer>>();
function loadCalendar() { function loadCalendar() {
try { try {
token.value = extractToken(token.value); token.value = extractToken(token.value);
@ -35,18 +37,26 @@ function loadCalendar() {
}); });
return; return;
} }
moduleStore().removeAllModules(); moduleStore().removeAllModules();
tokenStore().setToken(token.value); tokenStore().setToken(token.value);
calendarViewerRef.value?.invalidateAndRefetchCalendar();
toast.add({
severity: "success",
summary: t("editCalendarView.toast.success"),
detail: t("editCalendarView.toast.successDetailLoad"),
life: 3000,
});
} }
onMounted(() => { onMounted(() => {
if (token.value && token.value !== "") { if (token.value && token.value !== "") {
loadCalendar(); //loadCalendar();
tokenStore().setToken(token.value);
} }
}); });
</script> </script>
<template> <template>
<DynamicPage <DynamicPage
:hide-content="false" :hide-content="false"
@ -64,10 +74,10 @@ onMounted(() => {
:label="$t('userCalender.searchButton')" :label="$t('userCalender.searchButton')"
icon="pi pi-refresh" icon="pi pi-refresh"
@click="loadCalendar()" @click="loadCalendar()"
/> ></Button>
</template> </template>
<template #content> <template #content>
<CalendarViewer :token="tokenStore().token" /> <CalendarViewer :token="tokenStore().token" ref="calendarViewerRef" />
</template> </template>
</DynamicPage> </DynamicPage>
</template> </template>

View File

@ -85,6 +85,17 @@ export default defineConfig({
globPatterns: ["**/*.{js,css,html,ico,png,svg,json,vue,txt,woff2}"], globPatterns: ["**/*.{js,css,html,ico,png,svg,json,vue,txt,woff2}"],
cleanupOutdatedCaches: true, cleanupOutdatedCaches: true,
runtimeCaching: [ runtimeCaching: [
{
urlPattern: ({ url }) => url.pathname.startsWith('/api/feed'),
method: 'GET',
handler: 'NetworkFirst',
options: {
cacheName: 'calendar-feed-cache',
expiration: {
maxAgeSeconds: 12 * 60 * 60, // 12 hours
},
},
},
{ {
urlPattern: /^https?.*/, urlPattern: /^https?.*/,
handler: "NetworkFirst", handler: "NetworkFirst",