question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Docs: Make it clear that next/image is not compatible with SSG (next export) and improve the related docs

See original GitHub issue

What is the improvement or update you wish to see?

There is no mention in the NextJS image documentation that next/image does not work with next export.

It should be mentioned on these pages: https://nextjs.org/docs/basic-features/image-optimization (main next/image docs) https://nextjs.org/docs/messages/no-img-element (error page you’re directed to when not using next/image)

This issue is just about improving the docs, but also there really should be improvements to the default config so that users can use images on their SSG site without having to follow docs to apply workarounds

Is there any context that might help us understand?

This has been my experience as a new NextJS user this week:

  • Start my project with create-next-app and design all my pages (with images)
  • See many @next/next/no-img-element ESLint warnings telling me to use next/image
  • Refactor my project to use next/image (including fixing some broken CSS since it’s actually an img inside a div not a direct image replacement)
  • Try building and get the following message
Error: Image Optimization using Next.js' default loader is not compatible with `next export`.
  Possible solutions:
    - Use `next start` to run a server, which includes the Image Optimization API.
    - Use any provider which supports Image Optimization (like Vercel).
    - Configure a third-party loader in `next.config.js`.
    - Use the `loader` prop for `next/image`.
  Read more: https://nextjs.org/docs/messages/export-image-api
  • The first two ‘solutions’ are just saying don’t use SSG which is not really a solution, and the last 2 don’t link to any examples or documentation on what to do, so I ended up having to browse google and read through a bunch of GitHub issues (1, 2, 3, 4)
  • Tried using a custom loader as described here which again required refactoring all my Image components. This fixed my build errors but doesn’t actually fix image optimization it just disables it.
  • Try using next-optimized-images, which once again required refactoring all my images back into img components… only to find this doesn’t work either - on build I get a bunch of errors from the various image packages like this and I didn’t want to spend more time debugging dependencies of a dependency that is already an unoffocial workaround.
  • Try instead using next-image-export-optimizer which once again requires refactoring every image component in my project. At this point I’m desperate and have read literally hundreds of GitHub comments, this is the last remaining solution I was able to find and is a repo with only 47 stars. It manages to build without errors but I find the result is a bunch of dead image links on my page.
  • After many hours wasted I finally go back to the original workaround to disable image optimization completely.

I decided to use NextJS over CRA even though I don’t need SSR because I saw people touting it as having a better DX and sane defaults, but that doesn’t feel true at all for SSG so far

There are many highly upvoted comments requesting similar improvement to the documentation and saying it’s misleading to market NextJS as an SSG in its current state (https://github.com/vercel/next.js/issues/18356#issuecomment-718059371, https://github.com/vercel/next.js/discussions/19065#discussioncomment-127703, https://github.com/vercel/next.js/discussions/19065#discussioncomment-250617, https://github.com/vercel/next.js/discussions/19065#discussioncomment-479925, https://github.com/vercel/next.js/discussions/19065#discussioncomment-2708222)

Does the docs page already exist? Please link to it.

https://nextjs.org/docs/basic-features/image-optimization

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:10
  • Comments:6

github_iconTop GitHub Comments

9reactions
adamerosecommented, May 3, 2022

@adamerose

There’s an article here that shows how you can go about setting a custom loader and other alternatives too

I don’t want to put all my website’s images put on a third party cloud provider, I need control of hosting and would like to just export a complete static website to host myself. This is especially true for anyone working on internal webapps which may include sensitive data.

Aside from cloud hosting, this article gives the same solution I mentioned in my OP - fiddle with config or custom loaders in order to opt out of image optimization so SSG will actually work. My request with this issue is that this requirement is made clear in the NextJS documentation not just external blogs and GitHub discussions.

I think it would be possible to create every variation of the optimized image at build-time.

Yes there is discussion of this and other possible ways to fix image optimization for SSG, but in the mean time my hope with this issue is to update the current documentation so that people understand this is currently not possible and can know what is needed to fix next export without digging through GitHub issues and blogs.

My understanding is users need to do at least one of these things before they can run next export without errors

  1. Use img instead of next/image, and modify their .eslint.json to suppress the NextJS rule against using img
  2. Add a custom dummy () =>{} loader to all their Image tags
  3. Change their next.config.js to disable image optimization
  4. Use some third party package like next-optimized-images (which will not work without also doing step 3, see here)

This is pretty simple when listed out, but as a new user took me many hours reading blogs and issues to understand what my options were, when all I wanted to do was export and serve a static HTML website. Many of those solutions also require refactoring code which wouldn’t have been needed if I understood this ahead of time

6reactions
PatrikTheDevcommented, Apr 25, 2022

That isn’t necessarily true, next start still allows you to use SSG pages, but you also get a server runtime for API routes and in this case, Image Optimization.

I think it would be possible to create every variation of the optimized image at build-time. This solution wouldn’t support dynamic domains or URLs, just static imports (which isn’t always the case and likely why the team decided not to implement it this way)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error: Image Optimization using Next.js default loader is not ...
js' default loader is not compatible with next export . Possible solutions: Use next start to run a server, which includes the Image ......
Read more >
Basic Features: Image Optimization - Next.js
Some of the optimizations built into the Image component include: Improved Performance: Always serve correctly sized image for each device, using modern image ......
Read more >
next-i18next - npm
What is this? Although Next.js provides internationalised routing directly, it does not handle any management of translation content, or the ...
Read more >
Hosting - Next.js - JavaScript - AWS Amplify Docs
js' default loader is not compatible with next export . To manually deploy the next-app example, you must edit the index.js file to...
Read more >
Build environment variables | Netlify Docs
Use this variable with projects that generate static HTML using next export . DISABLE_IPX : when set to true for a Next.js site,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found