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

useStoryblok not working with Astro Server Islands #907

Open
dylanalizon opened this issue Aug 20, 2024 · 1 comment
Open

useStoryblok not working with Astro Server Islands #907

dylanalizon opened this issue Aug 20, 2024 · 1 comment

Comments

@dylanalizon
Copy link

storyblok.com


Expected Behavior

Trying to make an API request from a server islands result in an error storyblokApiInstance has not been initialized correctly.

Current Behavior

The server islands was not rendered an error was throw.

Steps to Reproduce

  1. Create a project with Astro and Storyblok plugin.
  2. Enable the experimental feature "Server islands".
  3. Try to use useStoryblok function inside a component which is a "Server Island" (use server:defer on the component)
  4. Deploy with "hybrid" mode.
@JakiChen
Copy link

JakiChen commented Sep 3, 2024

hello, @dylanalizon

You may want to delay initialization,

Instead of putting const storyblokApi = useStoryblokApi() in the outermost definition, each custom function is called (but called when used)

// utils/storyblok.ts
import { useStoryblokApi } from '@storyblok/astro';
import type { ISbStory, ISbStories, ISbStoryParams, ISbStoriesParams, ISbCustomFetch, ISbResult } from 'storyblok-js-client';

const version = import.meta.env.DEV ? 'draft' : 'published';

export async function get(slug: string, params?: ISbStoriesParams, fetchOptions?: ISbCustomFetch): Promise<ISbResult> {
    const storyblokApi = useStoryblokApi(); // Delayed initialization
    const data = await storyblokApi.get(slug, {
        version: version,
        ...params
    }, fetchOptions);
    return data;
}

export async function getAll(slug: string, params: ISbStoriesParams, entity?: string, fetchOptions?: ISbCustomFetch): Promise<any[]> {
    const storyblokApi = useStoryblokApi(); // Delayed initialization
    const data = await storyblokApi.getAll(slug, {
        version: version,
        ...params,
    }, entity, fetchOptions);
    return data;
}

export async function getStory(slug: string, params: ISbStoryParams, fetchOptions?: ISbCustomFetch): Promise<ISbStory> {
    const storyblokApi = useStoryblokApi(); // Delayed initialization
    const data = await storyblokApi.getStory(slug, {
        version: version,
        ...params,
    }, fetchOptions);

    return data;
}

export async function getStories(params: ISbStoriesParams, fetchOptions?: ISbCustomFetch): Promise<ISbStories> {
    const storyblokApi = useStoryblokApi(); // Delayed initialization
    const data = await storyblokApi.getStories({
        version: version,
        ...params
    }, fetchOptions);
    return data;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants