Replies: 5 comments 2 replies
-
Proposed implementation: #26103 |
Beta Was this translation helpful? Give feedback.
-
This is exactly what we would need as well 👍 |
Beta Was this translation helpful? Give feedback.
-
Could you configure the |
Beta Was this translation helpful? Give feedback.
-
We have an application hosted on Azure and we're using WEBSITE_RUN_FROM_PACKAGE which enables running the application from a ZIP package for faster deployments (2mins instead of 15mins). Having a way to specify the custom location of cache images would be highly beneficial for this scenario. |
Beta Was this translation helpful? Give feedback.
-
This is also important for systems like NixOS, GUIX (I assume), and various embedded environments with readonly system images. Ideally, |
Beta Was this translation helpful? Give feedback.
-
Summary
Image Optimizer stores cached images under
{distDir}/cache/images
. This RFC proposes making this path configurable such that the cached images can be stored in a different location that can be even outside of the folder of the project.Background
Depending on how you look at it, Azure is either in position 1 or 2 of the biggest cloud providers in the world. Azure App Service, one of its most popular hosted services, allows developers to easily deploy code written in multiple programming languages across multiple operating systems and have it scale automatically with load.
The two main types of app deployment in Azure App Service are package deployment and Zip deployment:
Therefore, because zip deployments are read only, the Image Optimizer inside next.js fails to write to the image cache, which in turn means we cannot use the awesome features of next/image.
I would like to also mention that I do not think this feature is Azure-specific. I can think of several scenarios across other cloud providers where storing the image cache separately can come in handy, for example storing image cache on a separate faster volume, or other instances where it's not wise to write into the same folder as your project.
Implementation
There are folders on the Azure App Service machines that we can write into, even if we use a zip deployment. For example /home/. So we would like to direct the Image Optimizer to optionally use a writable folder by specifying a custom path using an environment variable such as
NEXT_IMAGE_CACHE_PATH
.I noticed that in next-server.ts you send in server.distDir to imageOptimizer. I wonder if we could also send in an optional imageCacheDir. If imageCacheDir is defined, the code will use that path for the image cache. If it's not defined, then it will instead use the default join(distDir, 'cache', 'images')
I took a stab at the proposed implementation here: #26103. I have validated that imageOptimizer code does work with a folder that is located out of distDir.
Breaking changes?
None. This is an optional environment variable. If it is not specified, ImageOptimizer will store the cached images in
{distDir}/cache/images
like before.Downsides
I don't see any. This change is so small that me writing this RFC took longer than implementing it. It's literally a couple of lines of code. I understand where the next.js team is coming from with requesting RFCs to make sure a feature is popular enough before adding the future maintenance burden for it, but in this particular case in my opinion the change is so minimal that it almost has no cost. I did classify this as a feature request as opposed to bug, but one could maybe argue either way.
Describe alternatives you've considered
We could in theory use a symbolic link for the image cache to a writable folder but it's more complicated than it sounds because that symbolic link would need to be part of the deployed zip package, which complicates the build time if our developers are on a Windows machine and the deployment is on Linux, or vice versa.
Beta Was this translation helpful? Give feedback.
All reactions