Logo

dev-resources.site

for different kinds of informations.

Announcing a Storyblok Loader for the Astro Content Layer API

Published at
9/19/2024
Categories
storyblok
astro
headless
webdev
Author
manuelschroederdev
Categories
4 categories in total
storyblok
open
astro
open
headless
open
webdev
open
Author
18 person written this
manuelschroederdev
open
Announcing a Storyblok Loader for the Astro Content Layer API

We are very excited to announce that the latest alpha version of our Astro integration @storyblok/astro now supports the new Astro Content Layer API. The Content Layer API is an innovative new solution to fetch and handle content from external APIs and store it locally. It is designed to scale efficiently and handle large sites with thousands of pages highly performantly. With our new, built-in Storyblok loader, fetching your stories and turning them into an Astro Content Collection is simple and straightforward to accomplish.

How to use it

First of all, install the latest alpha version of our integration in your Astro project:

npm i @storyblok/astro@alpha
Enter fullscreen mode Exit fullscreen mode

As this is an experimental opt-in feature, you need to enable it by setting contentLayer to true in your astro.config.mjs file.

export default defineConfig({
  integrations: [
    storyblok({
      accessToken: "your-access-token",
      contentLayer: true,
    }),
  ],
  experimental: {
    contentLayer: true,
  },
});
Enter fullscreen mode Exit fullscreen mode

Next, in the src/content folder, you need to create a config.ts file that defines one or more collections that consist of the entirety of stories available in your Storyblok space.

import { storyblokLoader } from "@storyblok/astro";
import { defineCollection } from "astro:content";

const storyblokCollection = defineCollection({
  loader: storyblokLoader({
    accessToken: "your-access-token",
    apiOptions: {
      region: "us",
    },
    version: "published",
  }),
});

export const collections = {
  storyblok: storyblokCollection,
};
Enter fullscreen mode Exit fullscreen mode

Let’s look at an example […slug].astro dynamic route that renders the content entries stored in a collection.

---
import StoryblokComponent from "@storyblok/astro/StoryblokComponent.astro";
import BaseLayout from "../layouts/BaseLayout.astro";
import { getCollection } from "astro:content";
import { type ISbStoryData } from "@storyblok/astro";

export async function getStaticPaths() {
  const stories = await getCollection("storyblok");

  return stories.map(({ data }: { data: ISbStoryData }) => {
    return {
      params: { slug: data.full_slug },
      props: { story: data },
    };
  });
}

interface Props {
  story: ISbStoryData;
}

const { story } = Astro.props;
---

<BaseLayout>
  <StoryblokComponent blok={story.content} />
</BaseLayout>
Enter fullscreen mode Exit fullscreen mode

Benefits of using Storyblok with the Astro Content Layer API

The Astro Content Layer API stores all stories of a Storyblok space locally in the project. This approach provides two benefits:

  • Only new and updated stories have to be fetched from Storyblok instead of fetching all stories with every build. Especially with large-scale sites with thousands of pages, this can significantly reduce API traffic and reduce build times.
  • Direct access to the database provides a powerful query API to perform complex search and filtering operations.

The Astro Content Layer API is designed to work with Static Site Generation (SSG). Therefore, it is a perfect approach to consider for the production environment of a Storyblok project. You can learn more about deploying Storyblok projects in this tutorial.

Next steps

We hope you’re excited to try out this new feature! We would absolutely love to see your projects built with Storyblok and Astro, and hear your feedback about our experimental support of the new Content Layer API.

Would you like to contribute to the development of @storyblok/astro? Feel free to create an issue or a PR in the official GitHub repository.

headless Article's
30 articles in total
Favicon
India’s Best Headless SEO Company
Favicon
CMS: Payload CMS
Favicon
Mastering Authorization in Umbraco 14/15: Real-World Management API Challenges and Solutions
Favicon
Disable Frontend in Headless Wordpress
Favicon
What is New for Developers in Strapi 5: Top 10 Changes
Favicon
WordPress Headless + CPT + ACF: Building a Flexible Content Platform
Favicon
Built with Astro, Crystallize, and Stripe (Nerd Streetwear Online Store) Part I
Favicon
Strapi or WordPress: How to Decide Which CMS is Right for You
Favicon
Announcing a Storyblok Loader for the Astro Content Layer API
Favicon
Building the Frontend with Astro and Integration with Stripe (Nerd Streetwear Online Store) Part II
Favicon
Using Headless Woo commerce store api v1 in nextjs : issue faced and solutions
Favicon
Headless Browser Test: What Is It and How to Do It?
Favicon
7 Awesome Multi-Tenant Features in Headless CMS That'll Make Your Life Easier
Favicon
Making headless components easy to style
Favicon
How to Survive If You Still Have a Traditional CMS
Favicon
Demystifying Headless Commerce: Exploring Top Solutions and Their Benefits
Favicon
Comparing Sitecore XP (.NET) and Sitecore XM Cloud (TypeScript): Solr vs. GraphQL for Queries
Favicon
How to Build an E-commerce Store with Sanity and Next.js
Favicon
Getting to Know ButterCMS
Favicon
How to Set Up a Headless WordPress Site with Astro
Favicon
Utilize React Native's Headless JS for developing advanced features! 🚀
Favicon
Looking to Build Your 1st Headless or JamStack site?
Favicon
Moving to WP Headless
Favicon
Announcing Live Preview for Storyblok’s Astro Integration
Favicon
Save Time Building Jamstack / Headless CMS Backed Websites
Favicon
An Unforgettable Experience at UDLA: Exploring Sitecore XM Cloud and Headless Development
Favicon
Incerro Partners with Sanity to Accelerate Scalable Digital Solution Development
Favicon
Customize Your Hashnode Blog Frontend with Headless Frontend and Laravel
Favicon
Generate Types for your components with the Storyblok CLI!âš¡
Favicon
What are headless UI libraries?

Featured ones: