core icon indicating copy to clipboard operation
core copied to clipboard

nuxthub: cannot set expiration headers on static content

Open robert-hoffmann opened this issue 7 months ago • 1 comments

Describe the bug I want to set expiration headers on my static content

Example:

https://<domain>.com/_nuxt/PW1KWgW7.js
https://<domain>.com/_nuxt/entry.DDZap_SG.css

Cloudflare returns (for the above 2) cache-control: public, max-age=0, must-revalidate

and for all files served via _nuxt/*

I don't want this, since on production, all files names have unique names that are regenerated at each deploy

So i tried these methods (none work)

/server/middleware/static.ttl.ts

export default defineEventHandler((event) => {
  if (event.path.startsWith('/_nuxt/')) {
    setResponseHeader(event, 'cache-control', 'public, max-age=31536000, immutable')
  }
})

and

/public/_headers

# https://developers.cloudflare.com/workers/static-assets/headers/

# Cache hashed Nuxt chunks for one year
/_nuxt/*
  Cache-Control: public, max-age=31536000, immutable

and

nuxt.config.ts

  routeRules: {
    '/_nuxt/**': {
      cache: {
        maxAge: 31536000
      }
    }
  }, 

and

nuxt.config.ts

  routeRules: {
    '/_nuxt/**': {
      headers: { 'cache-control': 'public, max-age=31536000, immutable' }
    }
  }, 

PS. my nuxt.config.ts currently only contains this:

  hub: {
    // NuxtHub options
    cache: true
  },

And yes, i'm using compatibilty mode v4:

  compatibilityDate: '2025-05-15',
  future: {
    compatibilityVersion: 4,
  },  

Is this a bug ? Or how can i achieve the caching of all the js/css that is generated on compile

robert-hoffmann avatar May 26 '25 10:05 robert-hoffmann

I'm experiencing the same issue.

Current situation:

  • Cache settings are defined in /public/_headers
  • Local environment: The defined Cache-Control headers are applied correctly when using npx nuxthub preview
  • Production environment: After deployment, the settings are not reflected and Cache-Control remains at the default value: public, max-age=0, must-revalidate

k-urtica avatar Jun 05 '25 09:06 k-urtica