Cache Policy
Heya, I am trying to set a proper cache policy for images in /public (and images generated by the nuxtimg sizes tag) and stuff in _nuxt, how would I do that?
2 Replies
Hi @Just Simeon. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
I'll help you set up proper cache policies for your public images and Nuxt Image generated assets.
Caching Public Images and Nuxt Image Assets
For images in the
/public
directory and those generated by NuxtImg
with the sizes
attribute, you have a few options:
1. For Nuxt Image Generated Images
Nuxt Image uses the IPX provider by default which handles image optimization. When using NuxtImg
with the sizes
attribute, it generates optimized versions of your images that are served from the /_ipx/
path.
These images are automatically cached by the browser based on the response headers. You can configure caching behavior through Nitro's route rules:
This configuration enables server-side SWR (stale-while-revalidate) caching for all images processed by Nuxt Image.
2. For Static Assets in Public Directory
For regular images in your /public
directory, you can set similar cache rules:
3. For _nuxt Assets
For assets in the _nuxt
directory (which contains your bundled JavaScript, CSS, and other assets), you can set:
The immutable
flag tells browsers that the file will never change, which improves caching efficiency.
Additional Considerations
If you're using Nuxt Image with a specific provider like Vercel or AWS Amplify, they have their own caching mechanisms. For example, Vercel's Edge Network automatically optimizes images when you deploy to their platform, as mentioned in the Vercel provider documentation.
If you're generating a static site, Nuxt Image will optimize and save your images locally during generation, as noted in the Static Images documentation.
Remember that if you're using the dir
option to change where Nuxt Image looks for source images (e.g., from public
to assets/images
), this might affect how caching works for certain providers:
This configuration is explained in the dir configuration documentation.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@294593079812292609> Kapa.ai is still learning and improving, please let me know how I did by reacting below.