Skip to content

Instantly share code, notes, and snippets.

@MuhammadKhizar7
Created January 29, 2025 00:19
Show Gist options
  • Save MuhammadKhizar7/488ab69fa584598f64d3106510a61f64 to your computer and use it in GitHub Desktop.
Save MuhammadKhizar7/488ab69fa584598f64d3106510a61f64 to your computer and use it in GitHub Desktop.
useFatchWithCached.ts
import { StorageSerializers } from '@vueuse/core';
export default async <T>(url: string) => {
// Use sessionStorage to cache the lesson data
const cached = useSessionStorage<T>(url, null, {
// By passing null as default it can't automatically
// determine which serializer to use
serializer: StorageSerializers.object,
});
if (!cached.value) {
const { data, error } = await useFetch<T>(url, {
headers: useRequestHeaders(['cookie']),
});
if (error.value) {
throw createError({
...error.value,
statusMessage: `Could not fetch data from ${url}`,
});
}
cached.value = data.value as T;
} else {
console.log(`Getting value from cache for ${url}`);
}
return cached;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment