Docs: Make it clear that next/image is not compatible with SSG (next export) and improve the related docs
See original GitHub issueWhat 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 usenext/image
- Refactor my project to use
next/image
(including fixing some broken CSS since it’s actually animg
inside adiv
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.
Issue Analytics
- State:
- Created a year ago
- Reactions:10
- Comments:6
Top GitHub Comments
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.
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 errorsimg
instead ofnext/image
, and modify their.eslint.json
to suppress the NextJS rule against usingimg
() =>{}
loader to all theirImage
tagsnext.config.js
to disable image optimizationnext-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
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)