Skip to content

Commit

Permalink
fix: wrap api instance call with useAsyncData to avoid network dupl…
Browse files Browse the repository at this point in the history
…ication on SSG
  • Loading branch information
alvarosabu committed Apr 18, 2024
1 parent 96fd3c8 commit fa7746a
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/runtime/composables/useAsyncStoryblok.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ export const useAsyncStoryblok = async (
apiOptions: ISbStoriesParams = {},
bridgeOptions: StoryblokBridgeConfigV2 = {}
) => {
const storyblokApiInstance = useStoryblokApi();
const uniqueKey = `${JSON.stringify(apiOptions)}${url}`;
const story = useState<ISbStoryData>(`${uniqueKey}-state`);
const storyblokApiInstance = useStoryblokApi();

onMounted(() => {
if (story.value && story.value.id) {
Expand All @@ -22,12 +22,15 @@ export const useAsyncStoryblok = async (
});

if (!story.value) {
const { data } = await storyblokApiInstance.get(
`cdn/stories/${url}`,
apiOptions
);
story.value = data.story;
};

return story;
const { data } = await useAsyncData('story', () => {
return storyblokApiInstance.get(
`cdn/stories/${url}`,
apiOptions
);
})
if(data) {
story.value = data.value?.data.story
}
}
return story
};

0 comments on commit fa7746a

Please sign in to comment.