Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow asyncOptions #837

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface ModuleOptions {
devtools: boolean, // enable nuxt/devtools integration
apiOptions: any, // storyblok-js-client options
componentsDir: string, // enable storyblok global directory for components
asyncDataOutput: boolean, // enable asyncData output, will become default
}

export default defineNuxtModule<ModuleOptions>({
Expand All @@ -31,6 +32,7 @@ export default defineNuxtModule<ModuleOptions>({
devtools: false,
componentsDir: '~/storyblok',
apiOptions: {},
asyncDataOutput: false,
},
setup(options, nuxt) {
const resolver = createResolver(import.meta.url);
Expand Down
42 changes: 30 additions & 12 deletions src/runtime/composables/useAsyncStoryblok.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { useStoryblokApi, useStoryblokBridge } from "@storyblok/vue";
import type { ISbStoriesParams, StoryblokBridgeConfigV2, ISbStoryData } from '@storyblok/vue';
import { useState, onMounted, useAsyncData } from "#imports";
import { useState, onMounted, useAsyncData, useRuntimeConfig } from "#imports";
import { type AsyncDataOptions } from "#imports";
import deprecate from "util-deprecate";

export const useAsyncStoryblok = async (
url: string,
apiOptions: ISbStoriesParams = {},
bridgeOptions: StoryblokBridgeConfigV2 = {}
bridgeOptions: StoryblokBridgeConfigV2 = {},
asyncOptions: AsyncDataOptions = undefined
) => {
const storyblokApiInstance = useStoryblokApi();
const uniqueKey = `${JSON.stringify(apiOptions)}${url}`;
Expand All @@ -21,16 +24,31 @@ export const useAsyncStoryblok = async (
}
});

if (!story.value) {
const { data } = await useAsyncData(uniqueKey, () => {
return storyblokApiInstance.get(
`cdn/stories/${url}`,
apiOptions
);
})
if(data) {
story.value = data.value?.data.story
const options = useRuntimeConfig().public.storyblok;
if (!options.asyncDataOutput && asyncOptions === undefined) {
if (!story.value) {
deprecate(() => {}, "`useAsyncStoryblok` will return an `AsyncData` object ({ data, pending, ... }) in the future. You can change this behavior now by passing an AsyncDataOptions object as the last argument or setting `asyncDataOutput: true` in module options in `nuxt.config`.", "STORYBLOK-DEPR-ASYNC")();
const { data } = await useAsyncData(uniqueKey, () => {
return storyblokApiInstance.get(
`cdn/stories/${url}`,
apiOptions
);
})
if(data) {
story.value = data.value?.data.story
}
}

return story
}
return story

return await useAsyncData(
uniqueKey,
() => {
return storyblokApiInstance
.get(`cdn/stories/${url}`, apiOptions)
.then((r) => r.data.story)
},
asyncOptions
)
};