Announcing gatsby-source-storyblok V4
Published: 2022-06-07 23:29:11

We'd like to announce [gatsby-source-storyblok](https://github.com/storyblok/gatsby-source-storyblok) V4 has been released to improve your Storyblok x Gatsby DX🥳

We reviewed how Gatsby & Storyblok projects are handled differently from our [@storyblok/react](https://github.com/storyblok/storyblok-react/tree/feature/v1) SDK and took care of the pain points to deliver less complication.

Let us know how's your DX with our [gatsby-source-storyblok](https://github.com/storyblok/gatsby-source-storyblok) V4 SDK!

> Special thanks to [Alex](https://twitter.com/alexjoverm) for his hard & awesome collaboration work 🙏

You'll need TL;DR? You can jump directly to the **[LIVE DEMO](https://codesandbox.io/s/github/storyblok/getting-started/tree/master/gatsbyjs?fontsize=14&hidenavigation=1&theme=dark)** in CodeSandbox right away.

# Usage
First things first, install [gatsby-source-storyblok](https://github.com/storyblok/gatsby-source-storyblok) by running the command below.

```bash
npm install gatsby-source-storyblok
// yarn add gatsby-source-storyblok
```

# Initialization

Next step, register the plugin on your application and add the access token from your Storyblok space.

> i.e. You can initialize just once in `components/layout.jsx` if you use [gatsby-source-storyblok](https://github.com/storyblok/gatsby-source-storyblok) for your Gatsby projects.

If you would like to use the Storyblok API Client, you can add `apiPlugin`.

```js
import { storyblokInit, apiPlugin } from "gatsby-source-storyblok";
import Page from "./components/Page.jsx";
import Teaser from "./components/Teaser.jsx";

storyblokInit({
  accessToken: "YOUR_ACCESS_TOKEN",
  // bridge: true,
  use: [apiPlugin],
  components: {
    page: Page,
    teaser: Teaser,
  },
});
```

Did you realize something?😎

You don't have to handle conditionally returning components by yourself any more!

We took care of all and you just need to add all your components to the components object in the `storyblokInit` function, and that's it!

# Query Storyblok API and listen for Visual Editor changes

You can use the convenient `useStoryblokState(originalStory, bridgeOptions)` hook to get the story from the [Storyblok CDN API](https://www.storyblok.com/docs/api/content-delivery/v2), and automatically use [Storyblok Bridge]((https://www.storyblok.com/docs/guide/essentials/visual-editor?utm_source=github.com&utm_medium=readme&utm_campaign=gatsby-source-storyblok)) to listen for the Visual Editor live changes.

```jsx
// pages/index.jsx
import { useStoryblokState } from "gatsby-source-storyblok"

import Layout from "../components/layout"

const IndexPage = ({ data }) => {
  let story = data.storyblokEntry
  story = useStoryblokState(story)

  return (
    <Layout>
      <h1>{story.name}</h1>
    </Layout>
  )
}

export default IndexPage

export const query = graphql`
  query HomeQuery {
    storyblokEntry(full_slug: { eq: "home" }) {
      name
      full_slug
    }
  }
`
```

# Link your components to Storyblok Visual Editor

For every React component you want to link to its corresponding Storyblok component, you can use `storyblokEditable` function with the blok content where `blok` is the actual blok data coming from [Storyblok's Content Delivery API](https://www.storyblok.com/docs/api/content-delivery?utm_source=github.com&utm_medium=readme&utm_campaign=gatsby-source-storyblok)

```jsx
// components/Feature.jsx
import { storyblokEditable } from "gatsby-source-storyblok";

const Feature = ({ blok }) => {
  return (
    <div {...storyblokEditable(blok)} key={blok._uid}>
      <div>{blok.name}</div>
      <p>{blok.description}</p>
    </div>
  );
};

export default Feature;
```

Then the `<StoryblokComponent blok={blok} />` will take care of loading them for you 😉.

# Example

> TL;DR: Play with the **[Gatsby LIVE DEMO](https://codesandbox.io/s/github/storyblok/getting-started/tree/master/gatsbyjs?fontsize=14&hidenavigation=1&theme=dark)**

# Next Steps

Want to contribute? You can create an issue or PR on the [Gatsby SDK repo](https://github.com/storyblok/gatsby-source-storyblok) or get in touch.

# Resource

- gatsby-source-storyblok docs: [https://www.npmjs.com/package/gatsby-source-storyblok](https://www.npmjs.com/package/gatsby-source-storyblok)
- Storyblok Learning Hub: [https://www.storyblok.com/docs](https://www.storyblok.com/docs)
- Storyblok website, announcement blog post: [https://www.storyblok.com/mp/announcing-gatsby-sdk-v4](https://www.storyblok.com/mp/announcing-gatsby-sdk-v4)