How to add Cache Control to generated static files in nuxt 2

196 views Asked by At

We have a SSR project with Nuxt2. I am trying to add Cache-Control Header to generated static files in the _nuxt directory.

I added below code server_middleware/cacheBuster.ts

import { ServerMiddleware } from '@nuxt/types'

type CacheBustingHeader = 'Cache-Control' | 'Pragma' | 'Expires'

const CACHE_BUSTING_HEADERS_MAP: Record<CacheBustingHeader, string> = {
  'Cache-Control': 'no-cache, no-store, must-revalidate',
  Pragma: 'no-cache',
  Expires: '0',
}
const cacheBuster: ServerMiddleware = (_req, res, next) => {
  // Set response headers as needed
  Object.keys(CACHE_BUSTING_HEADERS_MAP).forEach((key) => {
    res.setHeader(key, CACHE_BUSTING_HEADERS_MAP[key as CacheBustingHeader])
  })
  // Call the next middleware in the stack
  next()
}

export default cacheBuster

and used in the nuxt.config.ts in the serverMiddleware

serverMiddleware: [cacheBuster]

But it's not working for the static files. even though its working in the page calls enter image description here

Is there a way to do that. add the cache-control to static files in nuxt2?

0

There are 0 answers