DEV Community

Tibor Udvari
Tibor Udvari

Posted on • Originally published at tiborudvari.com

How to work with Storybook and Astro Image

If you're wondering how to use Astro components in Storybook, the short answer is you can't. Astro renders its components on the server side, making it impossible to load them into Storybook. Maybe the Astro Container API will make this easier in the future 🤞, but right now, it's not even close to the DX of Storybook.

That being said, if you're like me, you're probably only interested in the Astro Image component for all those groovy optimizations, so it makes sense to conjure up a sneaky workaround clever workflow.

The idea is to make a mock client-side image component that we pass in the Storybook context, then switch it out for the real thing later with Astro. Named slots will allow us to do this.

Here's a simple demo of a Figure component with an image and a caption in Preact.

// src/components/Figure.tsx 
export default function Figure({ image, caption }) {
  return (
    <div class="flex flex-col">
      {image}
      <span>{caption}</span>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

Because of serialization, args don't work in Storybook with this approach, so do this instead:

// src/components/Figure.stories.tsx
import Figure from "./Figure";
import PreactImage as Image from "./PreactImage";

export default {
  component: Figure,
};

const image = (
  <PreactImage
    src="https://cdn.shopify.com/static/sample-images/bath.jpeg"
    width={800}
    height={600}
    alt="A lovely bath"
  />
);

export const Default = {
  render: () => <Figure image={image} />,
};
Enter fullscreen mode Exit fullscreen mode

Finally, swap it out with an Astro Image in the regular project. Notice the named image slot.

// pages/index.astro for example
---
import Figure from "../components/Figure";
import { Image } from "astro:assets";
---
<Figure >
  <Image
    slot="image"
    src="https://cdn.shopify.com/static/sample-images/bath.jpeg"
    width={800}
    height={600}
    alt="A lovely bath"
  />
</Figure>
Enter fullscreen mode Exit fullscreen mode

Now you've got a Storybook that closely resembles the Astro build.

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay