N
Nuxt2mo ago
Felix

Is it possible to consider host header during ISR caching?

hey, when using routeRules isr, is it possible to consider the host header as well? i am working on a multi-tenanted app and trying to cache the response per tenant.
2 Replies
kapa.ai
kapa.ai2mo ago
Hi @Felix. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Yes, when using routeRules with ISR (Incremental Static Regeneration) in a multi-tenant application, you can consider the host header by using the cache.varies option in your route rules. When utilizing Hybrid Rendering with cache strategies like ISR, all incoming request headers are dropped when handling cached responses via the Nitro caching layer. This means that by default, useRequestURL() will return localhost for the host property, which isn't ideal for multi-tenant applications. To make your ISR caching consider the host header, you need to specify which headers should be considered when caching and serving responses:
export default defineNuxtConfig({
routeRules: {
'/blog/**': {
isr: true,
cache: {
varies: ['host', 'x-forwarded-host']
}
}
}
})
export default defineNuxtConfig({
routeRules: {
'/blog/**': {
isr: true,
cache: {
varies: ['host', 'x-forwarded-host']
}
}
}
})
By adding the cache.varies option with host and x-forwarded-host, you're telling Nitro to create separate cache entries for different hostnames, which is essential for multi-tenant applications where different tenants access your app through different domains. This approach is specifically documented for multi-tenant environments in the useRequestURL documentation, which mentions:
When utilizing Hybrid Rendering with cache strategies, all incoming request headers are dropped when handling the cached responses via the Nitro caching layer (meaning useRequestURL will return localhost for the host). You can define the cache.varies option to specify headers that will be considered when caching and serving the responses, such as host and x-forwarded-host for multi-tenant environments.
---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@223579303122567168> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?