image icon indicating copy to clipboard operation
image copied to clipboard

Long term caching for images

Open KaragiannidesAgapios opened this issue 3 years ago • 5 comments

Versions

  • nuxt: ^2.15.6
  • nuxt/image: "^0.4.14",

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).

KaragiannidesAgapios avatar Jun 18 '21 15:06 KaragiannidesAgapios

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..)

pi0 avatar Jun 18 '21 16:06 pi0

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?

KaragiannidesAgapios avatar May 16 '22 11:05 KaragiannidesAgapios

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

RobbieTheWagner avatar Jul 28 '22 02:07 RobbieTheWagner

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.

tonqa avatar Aug 17 '22 01:08 tonqa

@pi0 Any progress with this?

Maybe we can help with this?

raphaelbernhart avatar Jan 02 '24 12:01 raphaelbernhart