admin管理员组

文章数量:1304249

I'm getting response from api that is a list of objects. The object contains property imageUrl, which is a link to to the image. How can I set cache-control to that image?

I'm getting response from api that is a list of objects. The object contains property imageUrl, which is a link to to the image. How can I set cache-control to that image?

Share Improve this question edited Jul 22, 2021 at 9:48 Woodkah asked Jul 22, 2021 at 9:43 WoodkahWoodkah 291 gold badge1 silver badge4 bronze badges 3
  • you can write proper code to get exact answer – Avani Bataviya Commented Jul 22, 2021 at 9:55
  • That wasn't very helpful – Woodkah Commented Jul 22, 2021 at 10:13
  • 1 Couple of questions: 1) Are you using next/image ponent to render your images; 2) If yes, are you using the Next.js' built-in Image Optimization or a cloud provider's optimization (see https://nextjs/docs/basic-features/image-optimization#loader)? – juliomalves Commented Jul 22, 2021 at 15:51
Add a ment  | 

2 Answers 2

Reset to default 6

Add the below code in the "next.config.js" file of your project to cache images for 60 seconds:

module.exports = {
  images: {
    minimumCacheTTL: 60,
  },
}

You can change the cache time in secs instead of 60.

Here is thr Next.js official documentation https://nextjs/docs/api-reference/next/image#style

The above answer by Naveen didn't work for me.

images: {
remotePatterns: [
  {
    protocol: 'https',
    hostname: 'host.',
    port: '',
    pathname: '/pathname/**',
  },
],

},

add the above code to next.config.js (from Nextjs documentation).

本文标签: javascriptHow can I cache dynamic images in Next appStack Overflow