Skip to content

Commit

Permalink
fix: refactor the async composable to use native fetch and useState only
Browse files Browse the repository at this point in the history
  • Loading branch information
Dawntraoz committed May 3, 2023
1 parent f631382 commit 0174f59
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
23 changes: 10 additions & 13 deletions lib/src/runtime/composables/useAsyncStoryblok.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { useStoryblokApi, useStoryblokBridge } from "@storyblok/vue";
import type { ISbStoriesParams, StoryblokBridgeConfigV2, ISbStoryData, ISbError, ISbResult } from '@storyblok/vue';
import { useAsyncData, useState, onMounted, createError } from "#imports";
import type { ISbStoriesParams, StoryblokBridgeConfigV2, ISbStoryData } from '@storyblok/vue';
import { useState, onMounted } from "#imports";

export const useAsyncStoryblok = async (
url: string,
apiOptions: ISbStoriesParams = {},
bridgeOptions: StoryblokBridgeConfigV2 = {}
) => {
const uniqueKey = `${JSON.stringify(apiOptions)}${url}`;
const story = useState<ISbStoryData>(`${uniqueKey}-state`, () => ({} as ISbStoryData));
const story = useState<ISbStoryData>(`${uniqueKey}-state`);
const storyblokApiInstance = useStoryblokApi();

onMounted(() => {
Expand All @@ -21,16 +21,13 @@ export const useAsyncStoryblok = async (
}
});

const { data, error } = await useAsyncData<ISbResult, ISbError>(
`${uniqueKey}-asyncdata`,
() => storyblokApiInstance.get(`cdn/stories/${url}`, apiOptions),
);

if (error.value?.response && error.value?.response.status >= 400 && error.value?.response.status < 600) {
throw createError({ statusCode: error.value?.response.status, statusMessage: error.value?.message?.message || 'Something went wrong when fetching from storyblok.' });
}

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

return story;
};
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion playground/pages/[...slug].vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ try {
resolve_relations: ["popular-articles.articles"]
});
} catch (error) {
console.log(error);
story.value = error;
}
</script>

Expand Down
7 changes: 7 additions & 0 deletions playground/pages/error.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script setup lang="ts">
const story = await useStoryblok("vue-test", { version: "draft" });
</script>

<template>
<StoryblokComponent v-if="story" :blok="story.content" />
</template>
1 change: 1 addition & 0 deletions playground/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const richText = computed(() => renderRichText(story.value.content.richText));

<template>
<div>
<NuxtLink to="vue">Vue</NuxtLink>
<div v-html="richText"></div>
<StoryblokComponent v-if="story" :blok="story.content" />
</div>
Expand Down

0 comments on commit 0174f59

Please sign in to comment.