@nuxtjs/sanity
Version:
Sanity integration for Nuxt
34 lines (33 loc) • 1.11 kB
JavaScript
import { refreshNuxtData, useState } from "#imports";
export const useSanityTagRevalidation = ({
client,
queryKey,
liveStore
}) => {
const liveContentTags = useState(`tags:${queryKey}`, () => []);
let unsubscribe = () => {
};
let getLastLiveEventId = () => void 0;
if (import.meta.client && liveStore) {
const subscriber = liveStore.subscribe(queryKey, (tags, updateLastLiveEventId) => {
const tagsSet = new Set(tags);
if (liveContentTags.value.some((tag) => tagsSet.has(tag))) {
updateLastLiveEventId();
refreshNuxtData(queryKey);
}
});
unsubscribe = subscriber.unsubscribe;
getLastLiveEventId = subscriber.getLastLiveEventId;
}
const fetchTags = async (query, params, options) => {
const { syncTags } = await client.fetch(query, params, {
...options,
resultSourceMap: false,
returnQuery: false,
stega: false,
tag: ["fetch-sync-tags"].filter(Boolean).join(".")
});
liveContentTags.value = syncTags?.map((tag) => `sanity:${tag}`) || [];
};
return { fetchTags, getLastLiveEventId, unsubscribe };
};