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

squoosh crash on static import png #26527

Closed
akomm opened this issue Jun 23, 2021 · 11 comments · Fixed by #29506
Closed

squoosh crash on static import png #26527

akomm opened this issue Jun 23, 2021 · 11 comments · Fixed by #29506
Labels
bug Issue was opened via the bug report template.

Comments

@akomm
Copy link

akomm commented Jun 23, 2021

What version of Next.js are you using?

11.0.1

What version of Node.js are you using?

14.15.0

What browser are you using?

firefox

What operating system are you using?

linux

How are you deploying your application?

other

Describe the Bug

static import of png:

Error: `unwrap_throw` failed
    at <anonymous>:wasm-function[168]:0x165c5
    at decode (<anonymous>:wasm-function[7]:0x9e7c)

$ grep unwrap_throw -r node_modules
binary file: node_modules/next/dist/next-server/server/lib/squoosh/png/squoosh_png_bg.wasm
binary file: node_modules/next/dist/next-server/server/lib/squoosh/png/squoosh_oxipng_bg.wasm

Expected Behavior

no error

To Reproduce

statically import a png file and use the import in jsx/tsx code

@akomm akomm added the bug Issue was opened via the bug report template. label Jun 23, 2021
@akomm
Copy link
Author

akomm commented Jun 23, 2021

It does not affect all PNG files it seems. Had to many different bugs last few days, currently no time debugging another one (this one comes just after another one fixed with the static import of images and right into the next bug ;( )
If you can't find the reason, I might come back later when I have the time. I hope the info is enough though...

@akomm
Copy link
Author

akomm commented Jun 23, 2021

Might be this: GoogleChromeLabs/squoosh#1057

@velvetkevorkian
Copy link

I'm also seeing this in Ubuntu in WSL as we upgrade to Next 11.0.1 -- even images that don't throw an error in the build process are showing up with encoding errors in the browser.

As a workaround it looks like you can disable static images in next.config.js

  images: {
    // https://github.com/vercel/next.js/issues/26527
    disableStaticImages: true,
  },

and use Webpack asset modules instead, e.g.

webpack: (config) => {
    const assetRegex = new RegExp(`.(png|jpe?g|gif|woff|woff2|ico|svg)$`)

    config.module.rules.push({
      test: assetRegex,
      type: 'asset/resource',
      generator: {
        filename: './static/assets/[name]-[contenthash].[ext]',
      },
    })

    return config
  }

@ipy
Copy link

ipy commented Jul 19, 2021

Thanks to @velvetkevorkian 's solution, while using TypeScript I have to use additional loaders to conform the new typings defined for images.

    const assetRegex = new RegExp(`.(png|jpe?g|gif|woff|woff2|ico|svg)$`);
    config.module.rules.push({
      test: assetRegex,
      use: [
        {
          loader: 'string-replace-loader',
          options: {
            search: 'export default (.*)',
            replace: (_, src) =>
              `export default { src: ${src.replace(/;$/, '')} }`,
            flags: 'm',
          },
        },
        {
          loader: 'url-loader',
          options: { outputPath: './static/assets/' },
        },
      ],
      type: 'javascript/auto',
    });
import img from 'path/to/image.png';

<img src={img.src} />

@dperetti
Copy link

dperetti commented Aug 3, 2021

I think it may have to do with the image ratio.

Both jpg and png images below make the build crash.

When using the jpg image, the error is:

Error: Call retries were exceeded

When using the png image, the error is:

Error: `unwrap_throw` failed
    at <anonymous>:wasm-function[168]:0x165c5
    at encode (<anonymous>:wasm-function[9]:0xbdf5)

nextJsCrash
nextJsCrash

@dperetti
Copy link

dperetti commented Aug 3, 2021

Kudos to @leerob 👍

yarn add next@canary sharp

Fixes the issue for me.

@akomm
Copy link
Author

akomm commented Aug 25, 2021

@dperetti AFAIK it adds an option to use an alternative transformer. Because I'v looked through canary pre-release change-logs and so far did not see fix for this in general. Obviously this option is nice for those who are blocked by this issue!

Regarding this issue:

If it is related to the previously linked issue, nextjs team could fix this generally, by bumping squoosh version used.

@kodiakhq kodiakhq bot closed this as completed in #29506 Oct 6, 2021
kodiakhq bot pushed a commit that referenced this issue Oct 6, 2021
Bump `squoosh` to the latest version, currently commit [cad09160](GoogleChromeLabs/squoosh@cad0916).

Ideally, we would use the version published to npm but it hasn't been published in [two months](https://www.npmjs.com/package/@squoosh/lib?activeTab=versions) and we have a patch (#23565) that isn't available upstream.

This also is a precursor to getting support for AVIF.

- Fixes #27092
- Fixes #26527 
- Reapplies the patch from #23565
@styfle
Copy link
Member

styfle commented Oct 6, 2021

This has been fixed in v11.1.3-canary.46.

You can try it out today with yarn add next@canary, thanks!

@jakelazaroff
Copy link

Just a heads up, I don't think bumping squoosh solved the issue, at least on macOS. Here's a minimal test case with next@11.1.3-canary.68: https://codesandbox.io/s/weathered-rain-5lpnh (note that you have to download as a zip and run yarn build, unless there's some way to do that in CodeSandbox that I'm unaware of).

If you yarn add sharp and then yarn build, it works.

@KevinShiCA
Copy link

FYI this seems to be broken on next@11.1.3-canary.97 again, even with sharp installed. Going back to next@11.1.3-canary.68 as suggested by @jakelazaroff fixes it.

@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 27, 2022
natew pushed a commit to natew/next.js that referenced this issue Feb 16, 2022
Bump `squoosh` to the latest version, currently commit [cad09160](GoogleChromeLabs/squoosh@cad0916).

Ideally, we would use the version published to npm but it hasn't been published in [two months](https://www.npmjs.com/package/@squoosh/lib?activeTab=versions) and we have a patch (vercel#23565) that isn't available upstream.

This also is a precursor to getting support for AVIF.

- Fixes vercel#27092
- Fixes vercel#26527 
- Reapplies the patch from vercel#23565
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants