reactjsnext.jslazy-loadingimage-optimization

How to serve images from remote storage to client with Next.js


This is a little bit more of a speculative question regarding the best practices of serving the images from remote object storage.

According to Next.js docs, one should load remote data to server components whenever possible. If that also accounts for images, the client would be able to receive the images with all the benefits the framework offers (e.g. optimised size and format, SEO, hide API keys, eliminate layout shift, etc).

  1. Wouldn't that mean that instead of being loaded directly to the client, the image is first loaded from remote storage to the server and only then to the client?
  2. And if so, do the optimization benefits still overweight the extra traffic induced to the server by this approach when implementing the page with tens of images*?

* I'm especially interested in the case when images are like some product photos, i.e. can be publicly available.


Solution

    1. Wouldn't that mean that instead of being loaded directly to the client, the image is first loaded from remote storage to the server and only then to the client?

    Those images should ideally be cached on the server, so that there is only one connection between client and server (and only occasionally an update connection between remote storage and server). If there is no caching, then yes, then it is two connections instead of one.

    1. And if so, do the optimization benefits still overweight the extra traffic induced to the server by this approach when implementing the page with tens of images?

    If so, there are still some things to consider.

    Some points that come to my mind are these (including caching, for the sake of completeness):

    If nothing of the above (and nothing of the points listed under Fetching data on the server) apply to you, then you are right, it's just another connection for each image.