Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

next.js NextImage's broken #17950

Closed
bonesoul opened this issue Apr 13, 2022 · 11 comments
Closed

next.js NextImage's broken #17950

bonesoul opened this issue Apr 13, 2022 · 11 comments

Comments

@bonesoul
Copy link

bonesoul commented Apr 13, 2022

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?

@optimista
Copy link

optimista commented Apr 21, 2022

Encountered same issue. It seems the fix through defineProperty is broken for Storybook 6.4.22 (after: #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.

@michalkleiner
Copy link

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

@knaot0
Copy link

knaot0 commented Apr 23, 2022

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;
    },

MH4GF added a commit to MH4GF/log.mh4gf.dev that referenced this issue Apr 23, 2022
ビルドしたStoryBookをChromaticや `npx http-server storybook-static` で立ち上げると以下のようなエラーが出る。

```
Uncaught TypeError: Cannot redefine property: default
    at Function.defineProperty (<anonymous>)
    at preview.jsx:6:8
```

これはStoryBook6.4.22から壊れているとのことで、このIssueでワークアラウンドが紹介されており、それで解決できた。
storybookjs/storybook#17950
@iamgoddog
Copy link

iamgoddog commented May 19, 2022

This didn't work for me at [email protected]
I found this at nextjs repo vercel/next.js#36417 (comment) 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

@souppower
Copy link
Contributor

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

vercel/next.js#36417 (comment)

@iamgoddog
Copy link

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

vercel/next.js#36417 (comment)

Yes it worked @6.4 for me also.

@shilman
Copy link
Member

shilman commented May 24, 2022

@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 #18284

@clodal
Copy link

clodal commented Oct 2, 2022

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

@Guilherme-Farias
Copy link

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

@jakapatb
Copy link

jakapatb commented Nov 4, 2022

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,
});

@shilman
Copy link
Member

shilman commented Dec 3, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants