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 #1049

Closed
wants to merge 16 commits into 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):

dipankarmaikap and others added 15 commits December 24, 2024 17:15
The `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

🎉 This PR is included in version 6.0.0-next.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

@dipankarmaikap dipankarmaikap changed the title feat!: Making Live Preview Stable and Removing useStoryblok Function feat!: making livepreview stable and removing useStoryblok function Dec 30, 2024
@dipankarmaikap
Copy link
Contributor Author

dipankarmaikap commented Dec 30, 2024

Closing this with an old commit has uppercase text, which causes an issue with our release rule.

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