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

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

Conversation

dipankarmaikap
Copy link
Contributor

@dipankarmaikap dipankarmaikap commented Jan 14, 2025

This PR stabilizes the experimental Live Preview feature and introduces breaking changes. The useStoryblok hook is now removed. Below are the key updates and usage examples.

Breaking Changes

  1. Removal of useStoryblok:
    The useStoryblok hook has been removed 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 } from '@storyblok/astro';
import StoryblokComponent from '@storyblok/astro/StoryblokComponent.astro';
import BaseLayout from '../layouts/Layout.astro';

const { slug } = Astro.params;
let story = 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>
     <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):

@alexjoverm alexjoverm requested review from alvarosabu and removed request for manuelschroederdev January 17, 2025 11:17
Copy link
Contributor

@alvarosabu alvarosabu left a comment

Choose a reason for hiding this comment

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

Blocking the merge while doing the QG

@alvarosabu
Copy link
Contributor

@dipankarmaikap QG passed, you can merge, great job

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