Why does cache.varies not reflect in response headers while cache.maxAge does?
#3529
-
|
Hi Nitro team! 👋 I've been exploring Nitro's caching capabilities and noticed an inconsistency in how caching rules are exposed in HTTP responses:
Expected Behavior:When configuring route rules like: routeRules: {
'/profile': {
cache: {
maxAge: 86400,
varies: ['cookie', 'accept-language']
}
}
}I expect the response to include: Current Behavior:Only Questions:
The Thanks for your insights! 🙏 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
@Leon2xiaowu this is by design.
to also set the // nitro.config.ts
export default defineNitroConfig({
routeRules: {
'/profile': {
cache: { maxAge: 86400, varies: ['cookie', 'accept-language'] },
headers: { 'Vary': 'Cookie, Accept-Language' },
},
},
});or use a middleware if you need more control: // server/middleware/vary.ts
export default defineEventHandler((event) => {
if (event.path.startsWith('/profile')) {
setHeader(event, 'Vary', 'Cookie, Accept-Language');
}
});ref: nitro cache docs |
Beta Was this translation helpful? Give feedback.
@Leon2xiaowu this is by design.
cache.variescontrols which request headers are used to generate the internal cache key (server-side differentiation), not which headers appear in the HTTP response. it tells nitro's caching layer "create separate cache entries when these request headers differ."cache.maxAgedoes double duty (sets both internal TTL and theCache-Controlresponse header), butvariesis internal only.to also set the
Varyresponse header for CDN/browser caching, combine both options: