I'm trying to get cache headers working for images loaded with custom loader which utilizes the defa

I'm trying to get cache headers working for images loaded with custom loader which utilizes the default Cloudflare Image Optimization implementation. These files are served from the public folder (public/images) which by default gets no cache (public, max-age=0, must-revalidate) and so the optimizer serves them with this header as well.

I've tried to add the following config settings:

{
  async headers() {
    return [
      {
        source: '/cdn-cgi/image/(.*)', // does not work
        headers: [
          {
            key: 'cache-control',
            value: 'public, max-age=14400',
          },
        ],
      },
      {
        source: '/images/(.*)', // does not work
        headers: [
          {
            key: 'cache-control',
            value: 'public, max-age=14400',
          },
        ],
      },
    ];
  },
  images: {
    minimumCacheTTL: 3600, // this does nothing
    loader: 'custom',
    loaderFile: './imageLoader.js',
  },
}


I also tried adding them to _headers file in the root of the project but they don't seem to get included in the final build.

Only way I've been able to get cache headers working for these files is by modifying ./.vercel/output/static/_headers after the build by adding this to the end of the generated file:

/images/*
  cache-control: public,max-age=3600


Am I missing something or is this the only way to get cache headers working for these images?
Was this page helpful?