For Gatsby there is gatsby-image, which works super well. For Next.js there is next-optimized-images which is not very mature. The latest version of Next.js also provides next/image, but as far as I have understood, it requires you to run the Next.js backend server, so it is not an option for 100% static websites.
https://nextjs.org/docs/basic-features/image-optimization
> 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 10 million images.
Sometimes I also bumped into issues with Next.js where the framework was trying to send some code to run on the client browser instead of running it during page generation. The division between server and client seemed a bit too hazy for my taste and felt magical in the wrong ways.