@storyblok/nuxt 7.2.0 → 8.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/module.d.mts CHANGED
@@ -1,5 +1,21 @@
1
1
  import * as _nuxt_schema from '@nuxt/schema';
2
+ import { AsyncDataOptions, AsyncData, NuxtError } from 'nuxt/app';
3
+ import { ISbResult, ISbStoriesParams, StoryblokBridgeConfigV2 } from '@storyblok/vue';
4
+ import { ComputedRef } from 'vue';
2
5
 
6
+ /**
7
+ * Options for the useAsyncStoryblok composable.
8
+ * Extends Nuxt's AsyncDataOptions with Storyblok-specific configuration.
9
+ */
10
+ interface UseAsyncStoryblokOptions extends AsyncDataOptions<ISbResult> {
11
+ /** Storyblok API parameters for fetching stories */
12
+ api: ISbStoriesParams;
13
+ /** Storyblok Bridge configuration for live preview */
14
+ bridge: StoryblokBridgeConfigV2;
15
+ }
16
+ interface UseAsyncStoryblokResult extends AsyncData<ISbResult, NuxtError<unknown>> {
17
+ story: ComputedRef<ISbResult['data']['story']>;
18
+ }
3
19
  interface ModuleOptions {
4
20
  accessToken: string;
5
21
  enableSudoMode: boolean;
@@ -9,7 +25,8 @@ interface ModuleOptions {
9
25
  apiOptions: any;
10
26
  componentsDir: string;
11
27
  }
28
+
12
29
  declare const _default: _nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
13
30
 
14
31
  export { _default as default };
15
- export type { ModuleOptions };
32
+ export type { ModuleOptions, UseAsyncStoryblokOptions, UseAsyncStoryblokResult };
package/dist/module.d.ts CHANGED
@@ -1,5 +1,21 @@
1
1
  import * as _nuxt_schema from '@nuxt/schema';
2
+ import { AsyncDataOptions, AsyncData, NuxtError } from 'nuxt/app';
3
+ import { ISbResult, ISbStoriesParams, StoryblokBridgeConfigV2 } from '@storyblok/vue';
4
+ import { ComputedRef } from 'vue';
2
5
 
6
+ /**
7
+ * Options for the useAsyncStoryblok composable.
8
+ * Extends Nuxt's AsyncDataOptions with Storyblok-specific configuration.
9
+ */
10
+ interface UseAsyncStoryblokOptions extends AsyncDataOptions<ISbResult> {
11
+ /** Storyblok API parameters for fetching stories */
12
+ api: ISbStoriesParams;
13
+ /** Storyblok Bridge configuration for live preview */
14
+ bridge: StoryblokBridgeConfigV2;
15
+ }
16
+ interface UseAsyncStoryblokResult extends AsyncData<ISbResult, NuxtError<unknown>> {
17
+ story: ComputedRef<ISbResult['data']['story']>;
18
+ }
3
19
  interface ModuleOptions {
4
20
  accessToken: string;
5
21
  enableSudoMode: boolean;
@@ -9,7 +25,8 @@ interface ModuleOptions {
9
25
  apiOptions: any;
10
26
  componentsDir: string;
11
27
  }
28
+
12
29
  declare const _default: _nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
13
30
 
14
31
  export { _default as default };
15
- export type { ModuleOptions };
32
+ export type { ModuleOptions, UseAsyncStoryblokOptions, UseAsyncStoryblokResult };
package/dist/module.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@storyblok/nuxt",
3
3
  "configKey": "storyblok",
4
- "version": "7.2.0",
4
+ "version": "8.0.1",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "0.8.4",
7
7
  "unbuild": "unknown"
package/dist/module.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { defineNuxtModule, createResolver, addComponentsDir, addPlugin, addImports, addImportsDir } from '@nuxt/kit';
1
+ import { defineNuxtModule, createResolver, addComponentsDir, addImportsDir, addPlugin, addImports } from '@nuxt/kit';
2
2
 
3
3
  const module = defineNuxtModule({
4
4
  meta: {
@@ -29,6 +29,7 @@ const module = defineNuxtModule({
29
29
  nuxt.options.build.transpile.push(resolver.resolve("./runtime"));
30
30
  nuxt.options.build.transpile.push("@storyblok/nuxt");
31
31
  nuxt.options.build.transpile.push("@storyblok/vue");
32
+ addImportsDir(resolver.resolve("./runtime/composables"));
32
33
  nuxt.options.runtimeConfig.public.storyblok = options;
33
34
  const enablePluginCondition = options.usePlugin === true && options.enableSudoMode === false;
34
35
  if (enablePluginCondition) {
@@ -57,7 +58,6 @@ const module = defineNuxtModule({
57
58
  addImports({ name, as: name, from: "@storyblok/vue" });
58
59
  }
59
60
  nuxt.options.typescript.hoist.push("@storyblok/vue");
60
- addImportsDir(resolver.resolve("./runtime/composables"));
61
61
  if (options.devtools) {
62
62
  nuxt.hook("devtools:customTabs", (iframeTabs) => {
63
63
  iframeTabs.push({
@@ -1,6 +1,68 @@
1
- import type { ISbStoriesParams, ISbStoryData, StoryblokBridgeConfigV2 } from '@storyblok/vue';
2
- export declare const useAsyncStoryblok: (url: string, apiOptions?: ISbStoriesParams, bridgeOptions?: StoryblokBridgeConfigV2) => Promise<import("vue").Ref<ISbStoryData<import("storyblok-js-client").ISbComponentType<string> & {
3
- [index: string]: any;
4
- }>, ISbStoryData<import("storyblok-js-client").ISbComponentType<string> & {
5
- [index: string]: any;
6
- }>>>;
1
+ import { type ISbResult, type ISbStoriesParams, type StoryblokBridgeConfigV2 } from '@storyblok/vue';
2
+ import { type ComputedRef, type Ref } from 'vue';
3
+ import type { AsyncDataOptions, NuxtError } from '#app';
4
+ import type { DedupeOption } from 'nuxt/app/defaults';
5
+ /**
6
+ * Options for the useAsyncStoryblok composable.
7
+ * Extends Nuxt's AsyncDataOptions with Storyblok-specific configuration.
8
+ */
9
+ export interface UseAsyncStoryblokOptions extends AsyncDataOptions<ISbResult> {
10
+ /** Storyblok API parameters for fetching stories */
11
+ api: ISbStoriesParams;
12
+ /** Storyblok Bridge configuration for live preview */
13
+ bridge: StoryblokBridgeConfigV2;
14
+ }
15
+ interface AsyncDataExecuteOptions {
16
+ dedupe?: DedupeOption;
17
+ cause?: 'initial' | 'refresh:hook' | 'refresh:manual' | 'watch';
18
+ }
19
+ export interface UseAsyncStoryblokResult {
20
+ story: ComputedRef<ISbResult['data']['story']>;
21
+ data: Ref<ISbResult>;
22
+ pending: Ref<boolean>;
23
+ error: Ref<NuxtError<unknown> | null>;
24
+ refresh: (opts?: AsyncDataExecuteOptions) => Promise<void>;
25
+ execute: (opts?: AsyncDataExecuteOptions) => Promise<void>;
26
+ clear: () => void;
27
+ }
28
+ /**
29
+ * Composable for fetching Storyblok stories with async data handling and live preview support.
30
+ *
31
+ * This composable combines Nuxt's useAsyncData with Storyblok's bridge functionality to provide:
32
+ * - Async data fetching with loading and error states
33
+ * - Automatic caching based on URL and API parameters
34
+ * - Live preview updates when editing in Storyblok
35
+ * - SSR/SSG compatibility
36
+ *
37
+ * @param url - The story URL path (e.g., 'home', 'blog/my-post')
38
+ * @param options - Configuration options for AsyncData, API calls and bridge setup
39
+ * @returns An object containing the async data result with additional story computed property
40
+ *
41
+ * @example
42
+ * ```vue
43
+ * <script setup>
44
+ * const { data, pending, error, story } = await useAsyncStoryblok('home', {
45
+ * api: {
46
+ * version: 'published', // or 'draft' for preview
47
+ * cv: Date.now()
48
+ * },
49
+ * bridge: {
50
+ * resolveRelations: ['featured-posts.posts', 'featured-posts.authors'],
51
+ * resolveLinks: 'url'
52
+ * }
53
+ * })
54
+ * </script>
55
+ *
56
+ * <template>
57
+ * <div v-if="pending">Loading...</div>
58
+ * <div v-else-if="error">Error: {{ error.message }}</div>
59
+ * <div v-else>
60
+ * <h1>{{ story?.content?.title }}</h1>
61
+ * <div v-html="story?.content?.body"></div>
62
+ * </div>
63
+ * </template>
64
+ * ```
65
+ *
66
+ */
67
+ export declare function useAsyncStoryblok(url: string, options: UseAsyncStoryblokOptions): Promise<UseAsyncStoryblokResult>;
68
+ export {};
@@ -1,28 +1,38 @@
1
1
  import { useStoryblokApi, useStoryblokBridge } from "@storyblok/vue";
2
- import { onMounted, useAsyncData, useState } from "#imports";
3
- export const useAsyncStoryblok = async (url, apiOptions = {}, bridgeOptions = {}) => {
2
+ import { computed, watch } from "vue";
3
+ import { useAsyncData } from "#app";
4
+ const stableStringify = (obj) => {
5
+ const sortedKeys = Object.keys(obj).sort();
6
+ const sortedObj = sortedKeys.reduce((acc, key) => {
7
+ acc[key] = obj[key];
8
+ return acc;
9
+ }, {});
10
+ return JSON.stringify(sortedObj);
11
+ };
12
+ export async function useAsyncStoryblok(url, options) {
4
13
  const storyblokApiInstance = useStoryblokApi();
5
- const uniqueKey = `${JSON.stringify(apiOptions)}${url}`;
6
- const story = useState(`${uniqueKey}-state`);
7
- onMounted(() => {
8
- if (story.value && story.value.id) {
9
- useStoryblokBridge(
10
- story.value.id,
11
- (evStory) => story.value = evStory,
12
- bridgeOptions
13
- );
14
- }
15
- });
16
- if (!story.value) {
17
- const { data } = await useAsyncData(uniqueKey, () => {
18
- return storyblokApiInstance.get(
19
- `cdn/stories/${url}`,
20
- apiOptions
21
- );
14
+ const { api, bridge, ...rest } = options;
15
+ const uniqueKey = `${stableStringify(api)}${url}`;
16
+ const result = await useAsyncData(uniqueKey, () => storyblokApiInstance.get(`cdn/stories/${url}`, api), rest);
17
+ if (import.meta.client) {
18
+ watch(result.data, (newData) => {
19
+ if (newData?.data.story && newData.data.story.id) {
20
+ useStoryblokBridge(newData.data.story.id, (evStory) => {
21
+ newData.data.story = evStory;
22
+ }, bridge);
23
+ }
24
+ }, {
25
+ immediate: true
22
26
  });
23
- if (data) {
24
- story.value = data.value?.data.story;
25
- }
26
27
  }
27
- return story;
28
- };
28
+ return {
29
+ data: result.data,
30
+ pending: result.pending,
31
+ error: result.error,
32
+ refresh: result.refresh,
33
+ execute: result.execute,
34
+ clear: result.clear,
35
+ story: computed(() => result.data.value?.data.story)
36
+ };
37
+ }
38
+ ;
@@ -1,2 +1,2 @@
1
- declare const _default: import("#app").Plugin<Record<string, unknown>> & import("#app").ObjectPlugin<Record<string, unknown>>;
1
+ declare const _default: import("nuxt/app").Plugin<Record<string, unknown>> & import("nuxt/app").ObjectPlugin<Record<string, unknown>>;
2
2
  export default _default;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@storyblok/nuxt",
3
3
  "type": "module",
4
- "version": "7.2.0",
4
+ "version": "8.0.1",
5
5
  "description": "Storyblok Nuxt module",
6
6
  "homepage": "https://github.com/storyblok/monoblok/tree/main/packages/nuxt#readme",
7
7
  "repository": {
@@ -25,7 +25,7 @@
25
25
  "dist"
26
26
  ],
27
27
  "dependencies": {
28
- "@storyblok/vue": "9.2.0"
28
+ "@storyblok/vue": "9.2.1"
29
29
  },
30
30
  "devDependencies": {
31
31
  "@cypress/vite-dev-server": "^6.0.3",