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!: making livepreview stable and removing useStoryblok function #1050

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

dipankarmaikap
Copy link
Contributor

This PR stabilizes the experimental Live Preview feature and introduces breaking changes. The useStoryblok hook is now deprecated and replaced by the new getLiveStory function. Below are the key updates and usage examples.

Breaking Changes

  1. Removal of useStoryblok:
    The useStoryblok hook has been deprecated in favor of getLiveStory. This function simplifies the implementation of live preview in your Astro application.

  2. Named Export for storyblok:
    The storyblok initialization is now exported as a named export instead of a default export. Update your astro.config.mjs imports accordingly.


Usage Example

Configure Storyblok in astro.config.mjs using the named export:

import { storyblok } from '@storyblok/astro';

export default {
  integrations: [
    storyblok({
      accessToken: 'OsvN....',
      bridge: {
        resolveRelations: ['featured-articles.posts'],
      },
      enableFallbackComponent: true,
      livePreview: true,
    }),
  ],
};

Page Example

Here’s how you can use getLiveStory in a page:

---
// pages/[...slug].astro
import {
  getLiveStory,
  useStoryblokApi,
  type ISbStoryData,
} from '@storyblok/astro';
import StoryblokComponent from '@storyblok/astro/StoryblokComponent.astro';
import BaseLayout from '../layouts/Layout.astro';

const { slug } = Astro.params;
let story: ISbStoryData | null = null;

const liveStory = await getLiveStory(Astro);
if (liveStory) {
  story = liveStory;
} else {
  const sbApi = useStoryblokApi();
  const { data } = await sbApi.get(`cdn/stories/${slug || 'home'}`, {
    version: 'draft',
    resolve_relations: ['featured-articles.posts'],
  });
  story = data?.story;
}
---

<BaseLayout>
  {story && <StoryblokComponent blok={story.content} />}
</BaseLayout>

Listening for Live Preview Updates

When the live preview updates content in the Storyblok editor, a custom event (storyblok-live-preview-updated) is triggered. Applications can listen for this event as follows:

<script>
  document.addEventListener("storyblok-live-preview-updated", () => {
    console.log("Live preview: body updated");
  });
</script>

Closing Issues

This PR addresses and closes the following issue(s):

`useStoryblok` function has been deprecated and replaced with a new function, `getLiveStory`. For details on how to use getLiveStory and its capabilities, please refer to the updated documentation.
BREAKING CHANGE: The default export has been replaced with a named export when initializing the Storyblok SDK in your `astro.config.js` file.
You should now use:
`import { storyblok } from '@storyblok/astro';`

Update your imports accordingly to ensure compatibility.
A new event is introduced that triggers when changes are made in the Storyblok Visual Editor.
This is particularly helpful for generating CSS dynamically, as outlined in issue #864.
Copy link
Contributor

@alexjoverm alexjoverm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @dipankarmaikap this is a great PR, thanks! The named export is ideal for bundling as is set as a standard, and I like how the getLiveStory it's particularly adapted for Astro's api needs.

I added you a comment about a typo.

Also, can we add some tests on the new feature/changes? I know some parts of this SDK might not be too easy to test, mostly due to Astro integration. However, as a general developing practice, we should always ensure that what we publish is decently tested, at least with some minimal tests.


export { getLiveStory, renderRichText, useStoryblokApi } from './lib/healpers';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dipankarmaikap I think there is a typo on the filename, do you mean helpers?

@JakiChen
Copy link

heyyyy, @dipankarmaikap, @alexjoverm I'm not sure if that's more straightforward:It is to directly allow users to access liveStory through locals

  • Or it can solve the problem that Astro needs to be manually configured directly as a context.

  • Or simply provide a LiveStorkyblokComponents?

Finally, thanks to the team's efforts and improvements, this is the most user-friendly headless CMS I've experienced so far

---
// pages/[...slug].astro
import {
  getLiveStory,
  useStoryblokApi,
  type ISbStoryData,
} from '@storyblok/astro';
import StoryblokComponent from '@storyblok/astro/StoryblokComponent.astro';
import BaseLayout from '../layouts/Layout.astro';

const { slug } = Astro.params;
let story: ISbStoryData | null = null;

if (Astro.locals.liveStory) {
  story = liveStory;
} else {
  const sbApi = useStoryblokApi();
  const { data } = await sbApi.get(`cdn/stories/${slug || 'home'}`, {
    version: 'draft',
    resolve_relations: ['featured-articles.posts'],
  });
  story = data?.story;
}
---

<BaseLayout>
  {story && <StoryblokComponent blok={story.content} />}
</BaseLayout>

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