Environment aware routeRules

I have this routeRules:
routeRules: {
'/**': {
cache: {
swr: true,
staleMaxAge: 30,
},
},
},
routeRules: {
'/**': {
cache: {
swr: true,
staleMaxAge: 30,
},
},
},
They work as expected in production, but I don't want the swr enabled in dev mode. How can I make this environment aware?
8 Replies
kapa.ai
kapa.ai2mo ago
Hi @Samuelreichoer. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To make your routeRules environment-aware in Nuxt, you can use per-environment overrides in your nuxt.config.ts file. This allows you to specify different configurations for development and production. Here's how you can disable the SWR cache in development and enable it only in production:
export default defineNuxtConfig({
$production: {
routeRules: {
'/**': {
cache: {
swr: true,
staleMaxAge: 30,
},
},
},
},
$development: {
routeRules: {
// No SWR caching in development
},
},
})
export default defineNuxtConfig({
$production: {
routeRules: {
'/**': {
cache: {
swr: true,
staleMaxAge: 30,
},
},
},
},
$development: {
routeRules: {
// No SWR caching in development
},
},
})
With this setup, the routeRules will only apply the SWR cache in production, and not in development. You can read more about environment overrides in the official documentation: Nuxt Configuration: Environment Overrides ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@1065266379382587422> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Samuelreichoer
SamuelreichoerOP2mo ago
@kapa.ai How can I set a spezific environment?
kapa.ai
kapa.ai2mo ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- To set a specific environment in Nuxt, you can use the --envName flag with your Nuxt CLI command. For example, to use the staging environment, you would run:
npx nuxt build --envName staging
npx nuxt build --envName staging
This will apply the configuration under the $env: { staging: { ... } } key in your nuxt.config.ts. You can define as many environments as you need using $production, $development, or custom environments under $env in your config file Nuxt Configuration: Environment Overrides. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@1065266379382587422> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
dario
dario2mo ago
Hi, Samuel, Did you fix the problem already?
Samuelreichoer
SamuelreichoerOP2mo ago
Jep the suggested answer was my solution
dario
dario2mo ago
great to hear

Did you find this page helpful?