33
loading...
This website collects cookies to deliver better user experience
public
directory, resulting in the following error:
Failed to parse src "static/media/public/<imageName>.jpg" on `next/image`, if using relative image it must start with a leading slash "/" or be an absolute URL (http:// or https://)
blurDataURL
prop, resulting in the following error:
Image with src "static/media/public/<imageName>.jpg" has "placeholder='blur'" property but is missing the "blurDataURL" property.
// src/components/ImageTest.js
import Image from "next/image"
import testImage from "../public/testImage.jpg"
const ImageTest = () => (
<Image src={testImage}
alt="A stack of colorful cans"
layout="fill"
/>
)
export default ImageTest
// stories/ImageTest.stories.js
import React from 'react';
import ImageTest from '../components/ImageTest';
export default {
title: 'Image/ImageTest',
component: ImageTest,
};
const Template = (args) => <ImageTest {...args} />;
export const KitchenSink = Template.bind({});
KitchenSink.args = {};
public/
directory. But running with the -s public/
command line option to tell it about the directory doesn't solve the problem.Image
component. One of its most useful features is that it will automatically optimize the image you pass it and create and serve alternative sizings of it on demand. Next.js can't work its magic when the Image
component is rendered inside Storybook though, and that's why the solution here is to simply turn these optimizations off in this context. To do this, we'll have to add the following to Storybook's setup code:// .storybook/preview.js
import * as NextImage from "next/image";
const OriginalNextImage = NextImage.default;
Object.defineProperty(NextImage, "default", {
configurable: true,
value: (props) => (
<OriginalNextImage
{...props}
unoptimized
/>
),
});
Image
component's default export with our own version, which adds the unoptimized
prop to every instance of it. With this, "the source image will be served as-is instead of changing quality, size, or format", according to the Next.js documentation. And since we added this to Storybook's setup code, this will only be done when our component is rendered inside Storybook.Image
component and the props you can pass into it, take a look at the introduction to its features as well as its documentation.Image
component is that it will automatically generate small, blurred placeholder images for display during the loading of the full image.placeholder="blur"
prop:// src/components/ImageTest.js
import Image from "next/image"
import testImage from "../public/testImage.jpg"
const ImageTest = () => (
<Image src={testImage}
alt="A stack of colorful cans"
layout="fill"
placeholder="blur" // this is new!
/>
)
export default ImageTest
// .storybook/preview.js
import * as NextImage from "next/image";
const OriginalNextImage = NextImage.default;
Object.defineProperty(NextImage, "default", {
configurable: true,
value: (props) => (
<OriginalNextImage
{...props}
unoptimized
// this is new!
blurDataURL="data:image/jpeg;base64,/9j/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCAADAAQDASIAAhEBAxEB/8QAFQABAQAAAAAAAAAAAAAAAAAAAAf/xAAbEAADAAMBAQAAAAAAAAAAAAABAgMABAURUf/EABUBAQEAAAAAAAAAAAAAAAAAAAMF/8QAFxEAAwEAAAAAAAAAAAAAAAAAAAECEf/aAAwDAQACEQMRAD8Anz9voy1dCI2mectSE5ioFCqia+KCwJ8HzGMZPqJb1oPEf//Z"
/>
),
});
Image
component, take a look at the placeholder
prop's documentation.💡 This post was published 2021/07/21 and last updated 2021/07/21. Since node libraries change and break all the time, there's a non-trivial chance that all of this is obsolete and none of it works when you read this in your flying taxi, gulpin' on insect protein shakes sometime in the distant future or, let's be honest here, next week. If so, tell me in the comments and I'll see if an update would still be relevant.