next-optimized-images icon indicating copy to clipboard operation
next-optimized-images copied to clipboard

Issue with NextJS 10

Open ScreamZ opened this issue 5 years ago • 14 comments

Have a look at this thread https://github.com/vercel/next.js/issues/18946

ScreamZ avatar Nov 13 '20 11:11 ScreamZ

Hello there! First, thank's you for the work made here.

I hope to add more context to this issue.

The problem

I am trying to bump my project to nextJS 10.
This project use next-optimized-images in the latest stable version.
The build break with the following error :

Error: Cannot find module '<project-path>/node_modules/responsive-loader/lib/cjs.js/sharp'

Indeed, in responsive-loader.js#l11, detectedLoaders.responsive is equal to <project-path>/node_modules/responsive-loader/lib/cjs.js. Notice I don't know if this is intended.
After analyzing the structure, I suppose It should be <project-path>/node_modules/responsive-loader/lib/adapters.

I think I ran into this because nextJS 10 bring sharp dependency.
I was previously using sharp, but the responsive loader seems to detect sharp first.

Solutions

Fix on the version 2

This was my chosen solution. I opened a pull request for it. It is minimal, and all checks are OK.
I hope a maintainer will find the time to review it.

Upgrade to the version 3 canary

I tried this but ran into #195

guillaumeLamanda avatar Nov 15 '20 11:11 guillaumeLamanda

Thanks 😅

ScreamZ avatar Nov 15 '20 11:11 ScreamZ

Is this still useful with the new version?

SalahAdDin avatar Nov 22 '20 21:11 SalahAdDin

Is this still useful with the new version?

With the alpha 3 version, I encounter #195. Version 3 will be a good solution when stable 😃

guillaumeLamanda avatar Nov 23 '20 14:11 guillaumeLamanda

@SalahAdDin It is, to import from modules

ScreamZ avatar Nov 23 '20 15:11 ScreamZ

@ScreamZ I mean, is this page still relevant with the new NextJS image api?

SalahAdDin avatar Nov 23 '20 16:11 SalahAdDin

Yeah, because NextJS don't do the same things exactly, they are optimizing other things

ScreamZ avatar Nov 23 '20 17:11 ScreamZ

is there any tutorial about that?

SalahAdDin avatar Nov 23 '20 17:11 SalahAdDin

As said in the NextJS documentation

Instead of optimizing images at build time, Next.js optimizes images on-demand, as users request them. Unlike static site generators and static-only solutions, your build times aren't increased, whether shipping 10 images or 10million images.

For my personal use case, I need to build time URL generation at least to fill product metadata.
Later, I think I can deduce the image URL from the image path, but I would like NextJS to provide a function for this purpose.

@ScreamZ , from my point of view, NextJS Image component and this library do the same things, but not at the same time.
Notice that the way NextJS is doing it is more scalable because images are optimized "on the fly", which reduce drastically the build time.

This still has drawbacks. ~First, you're using a managed service so optimize the images for you. This means: you need vercel (or another loader) for an optimized experience with NextJS <Image/>.~
Then, we can imagine a higher response time when calling for an image for the first image call. Later, vercel will leverage caching.

EDIT: It also works with a custom server as specified on the loaders page section.
The confusion was initiated because I deploy on vercel, so it uses the vercel image loader.

guillaumeLamanda avatar Nov 23 '20 18:11 guillaumeLamanda

This still has major drawbacks. First, you're using a managed service so optimize the images for you. This means: you need vercel for an optimized experience with NextJS. Notice you can use another image endpoint to use another service, but it needs some extra work on your side. Then, we can imagine a higher response time when calling for an image for the first image call. Later, vercel will leverage caching I suppose.

@guillaumeLamanda I'm not sure to understand, we must use vercel to work with the new image component of next.js ? In my case, 80% of my images are svg and the rest are png and gif and i have some uploaded images that come from a rest API. Basically i want to optimize the png and gif part. I don't really know if it's revelant to optimize the svg (and if you can explain me the benefit of optimizing svg i will be interested). And I don't use vercel i have an another server for this project.

When i read the next.js documentation and when i saw this library, I was thinking about wich solution is better for my current project. So, if you can help me to take the good decision i will be very happy 😄

JbPons avatar Nov 24 '20 08:11 JbPons

I'm not sure to understand, we must use vercel to work with the new image component of next.js ?

I do not think so, the documentation states the built-in optimization is used by default.

durchanek avatar Nov 26 '20 10:11 durchanek

@durchanek thank's I missed the default loader line. I updated the comment to clear the confusion it brings.

So @JbPons, the <Image/> component does not require to use Vercel. The loader page pointed above says it works with a custom server. You still need to be careful with your max-age header

guillaumeLamanda avatar Nov 26 '20 11:11 guillaumeLamanda

Thanks for the clarification

JbPons avatar Nov 26 '20 13:11 JbPons

I first implemented the workaround proposed here before realizing there is a better way recommended to fix this using the 'responsive' property in next.config.js. Reference: #206

lansolo99 avatar Feb 21 '21 16:02 lansolo99