storybook icon indicating copy to clipboard operation
storybook copied to clipboard

next.js NextImage's broken

Open bonesoul opened this issue 2 years ago • 9 comments

I've been using the trick here to fix optimized next.js image components:

https://storybook.js.org/blog/get-started-with-storybook-and-next-js/

// .storybook/preview.js
import * as NextImage from "next/image";

const OriginalNextImage = NextImage.default;

Object.defineProperty(NextImage, "default", {
  configurable: true,
  value: (props) => <OriginalNextImage {...props} unoptimized />,
});

though with latest builds, it seems to be broken. any fixes?

bonesoul avatar Apr 13 '22 13:04 bonesoul

Encountered same issue. It seems the fix through defineProperty is broken for Storybook 6.4.22 (after: https://github.com/storybookjs/storybook/pull/17875) and also for Storybook 6.5, and might not be working for new versions neither. I did resolve the issue with creating ./.storybook/NextImage.js as following:

import Image from '../node_modules/next/image';
const NextImage = props => <Image unoptimized {...props} />
export default NextImage;

and then creating an alias for next/image by putting this line into webpackFinal function within ./.storybook/main.js

module.exports = {
  ...
  webpackFinal: async (config) => {
    ...
    config.resolve.alias["next/image"] = require.resolve("./.storybook/NextImage.js");
    ...
  }
  ...
}

Note: The import through ../node_modules is necessary not to create a cyclically resolving alias.

optimista avatar Apr 21 '22 17:04 optimista

Thanks for the workaround @optimista, it worked a charm for us!

michalkleiner avatar Apr 22 '22 11:04 michalkleiner

Thank you @optimista!

it worked by changing the following two points!

  1. webpackFinal returns the altered config
  2. Change the path of "./.storybook/NextImage.js" to "./NextImage.js"
  webpackFinal: async (config) => {
-     config.resolve.alias["next/image"] = require.resolve("./.storybook/NextImage.js");
+     config.resolve.alias["next/image"] = require.resolve("./NextImage.js");
+     return config;
    },

knaot0 avatar Apr 23 '22 12:04 knaot0

This didn't work for me at [email protected] I found this at nextjs repo https://github.com/vercel/next.js/issues/36417#issuecomment-1117360509 and that fixed it for me if I use [email protected] (didn't work with 6.5.0)

If you're curious, the error I was getting was:

ERR! TypeError: Cannot read property 'push' of undefined
ERR!     at Object.babel (.../node_modules/.pnpm/@[email protected]_t4cqyaxwbtpsab7uej7nin732q/node_modules/@storybook/builder-webpack5/dist/cjs/presets/preview-preset.js:37:18)
ERR!     at .../node_modules/.pnpm/@[email protected]_t4cqyaxwbtpsab7uej7nin732q/node_modules/@storybook/core-common/dist/cjs/presets.js:294:28

Using

iamgoddog avatar May 19 '22 13:05 iamgoddog

@bonesoul @iamgoddog I'm still @6.4, but this worked for me.

https://github.com/vercel/next.js/issues/36417#issuecomment-1117360509

souppower avatar May 23 '22 07:05 souppower

@bonesoul @iamgoddog I'm still @6.4, but this worked for me.

https://github.com/vercel/next.js/issues/36417#issuecomment-1117360509

Yes it worked @6.4 for me also.

iamgoddog avatar May 23 '22 14:05 iamgoddog

@iamgoddog I believe the push of undefined error is unrelated and was fixed in https://github.com/storybookjs/storybook/releases/tag/v6.5.4 by this PR https://github.com/storybookjs/storybook/pull/18284

shilman avatar May 24 '22 09:05 shilman

Had this issue. Can confirm that this works in 6.5. Thanks!

clodal avatar Oct 02 '22 03:10 clodal

Anyone using version "^6.5" managed to solve it?

Guilherme-Farias avatar Oct 11 '22 20:10 Guilherme-Farias

It worked on v6.5

// .storybook/preview.js

import * as NextImage from 'next/image';
import React from 'react';

const OriginalNextImage = NextImage.default;

Object.defineProperty(NextImage, 'default', {
  configurable: true,
  value: (props) =>
    React.createElement(OriginalNextImage, { ...props, unoptimized: true }),
});

Object.defineProperty(NextImage, '__esModule', {
  configurable: true,
  value: true,
});

jakapatb avatar Nov 04 '22 09:11 jakapatb

Jeepers creepers!! I just released https://github.com/storybookjs/storybook/releases/tag/v7.0.0-alpha.57 containing PR #20028 that references this issue. Upgrade today to the @next NPM tag to try it out!

npx sb upgrade --prerelease

Closing this issue. Please re-open if you think there's still more to do.

shilman avatar Dec 03 '22 14:12 shilman