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

Streamlining useAsyncStoryblok to nuxt's useAsyncData #836

Open
1 task done
jrutila opened this issue May 15, 2024 · 4 comments · May be fixed by #837
Open
1 task done

Streamlining useAsyncStoryblok to nuxt's useAsyncData #836

jrutila opened this issue May 15, 2024 · 4 comments · May be fixed by #837
Assignees
Labels
enhancement [Issue][PR] New feature p3 pending-triage [Issue] Ticket is pending to be prioritised

Comments

@jrutila
Copy link

jrutila commented May 15, 2024

Description

In the latest versions useAsyncStoryblok returns only the story. You cannot control the useAsyncData that is inside the useAsyncStoryblok at all. For example, you would like to instruct lazy or client only fetching.

To be more in line with the way nuxt composables like useAsyncData or useFetch work the useAsyncStoryblok should return the object that contains the usual data, pending, etc properties. One could also provide the AsyncDataOptions for the composable.

This would be a breaking change and thus needs more discussion.

I can submit a PR for this if the idea sounds solid.

Suggested solution or improvement

This implementation of useAsyncStoryblok should work as described above.

import { useStoryblokApi, useStoryblokBridge } from "@storyblok/vue";
import type { ISbStoriesParams, StoryblokBridgeConfigV2, ISbStoryData } from '@storyblok/vue';
import { useState, onMounted, useAsyncData } from "#imports";
import { type AsyncDataOptions } from "#imports";

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

  onMounted(() => {
    if (story.value && story.value.id) {
      useStoryblokBridge(
        story.value.id,
        evStory => (story.value = evStory),
        bridgeOptions,
      );
    }
  });

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

This could be then called like this (from playground folder):

const { data: story, pending } = await useAsyncStoryblok(`vue/articles/${path.params.slug}`, {
  version: "draft",
  language: "en"
}, {},
{ server: false, lazy: true, dedupe: "defer" }); // <- any asyncData options to give the control to the dev

Additional context

No response

Validations

@jrutila jrutila added enhancement [Issue][PR] New feature pending-author [Issue] Awaiting further information or action from the issue author pending-triage [Issue] Ticket is pending to be prioritised labels May 15, 2024
@jrutila jrutila linked a pull request May 15, 2024 that will close this issue
@jrutila
Copy link
Author

jrutila commented May 15, 2024

Created the PR after all...

@johnjenkins
Copy link

thanks - need this too. Also the current implementation loses any error context from the storyblok fetch and doesn't throw any errors too

@alvarosabu alvarosabu self-assigned this Jul 2, 2024
@alvarosabu alvarosabu added bug [Issue] Something isn't working p3 and removed pending-author [Issue] Awaiting further information or action from the issue author bug [Issue] Something isn't working labels Jul 2, 2024
@alvarosabu alvarosabu linked a pull request Jul 2, 2024 that will close this issue
@alvarosabu
Copy link
Contributor

Hi @jrutila thanks for reaching out! Can you confirm for me if the PR I linked is correct?

Since this introduces a breaking change let me discuss it internally with the team on how would be the best approach. 🙏

@jrutila
Copy link
Author

jrutila commented Jul 2, 2024

Yes, the PR is correct. I implemented some deprecation warning already, but please do discuss it through and change as needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement [Issue][PR] New feature p3 pending-triage [Issue] Ticket is pending to be prioritised
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants