nuxt / image

Plug-and-play image optimization for Nuxt applications.
https://image.nuxt.com
MIT License
1.34k stars 270 forks source link

Long term caching for images #329

Open KaragiannidesAgapios opened 3 years ago

KaragiannidesAgapios commented 3 years ago

Versions

Hey everyone and thank you for this great package.

I'm facing an issue with Lighthouse extension of chrome and i cannot fix it. Screen_Shot_2021-06-17_at_16 19 42

Here is my image component from the code:

  <nuxt-img
      src="/img/insurance/hero.jpg"
      class="h-full w-full object-cover"
      sizes="sm:375px md:768px lg:1024px"
      height="400"
/>

and here is the generated code:

<img 
   src="/_ipx/img/insurance/hero.jpg?s=1024_400" 
   height="400" 
   sizes="(max-width: 640px) 375px, (max-width: 768px) 768px, 1024px" 
   srcset="/_ipx/img/insurance/hero.jpg?s=375_400 375w, /_ipx/img/insurance/hero.jpg?s=768_400 768w, /_ipx/img/insurance/hero.jpg?s=1024_400 1024w" 
   class="h-full w-full object-cover"
>

My image is stored inside static/img/insurance directory. I tried to fix the issue by using the render option in nuxt.config like this (but it didn't solve the issue):

render: {
    // Setting up cache for 'static' directory - a year in milliseconds
    static: {
      maxAge: 60 * 60 * 24 * 365 * 1000,
    },
  },

I also tried the use of keep-alive prop in my layout like this(but it didn't solve the issue): <Nuxt keep-alive />


My question is this one: Is there any way to set maxAge through nuxt/image configs? If not are there any plans of exposing the ability to do so?

If there is another solution that you are aware please let know (or let me know how i can help with this).

pi0 commented 3 years ago

Hi @KaragiannidesAgapios. Thanks for the issue.

Since runtime transforms are not hashed, they should be cached in a way next deployment can invalidate them with low TTL (updating hero.jpeg being applied). We can expose maxAge module option to (ipx) (default is 5 minutes) but I think this enhancement should be coupled with another addition to add a build hash to generates sources (otherwise you should use distinct name to invalidate old images..)

KaragiannidesAgapios commented 2 years ago

Small update from my side here.

I updated the cache time for images in the server for a year so the result is I don't see the Lighthouse error anymore.

I added images inside assets directory so i can benefit from webpack, but the problem is that images are not updated on build. One must clear his cache, in order to see the updated content.

Also i noticed that this is not happening when a native html <img/> is used instead.

Do you guys have anything to propose here?

RobbieTheWagner commented 2 years ago

@pi0 have there been any developments on hash generation? We could really use asset fingerprinting here.

tonqa commented 2 years ago

Me too, please update. My CSS background-image generated by $img has a Cache-Control: max-age=300, public, s-maxage=300. There should be a better handling of this in order to increase the cache time.

raphaelbernhart commented 10 months ago

@pi0 Any progress with this?

Maybe we can help with this?