Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change TTL when using swr in Nuxt 3? (per-route preferably)

The Nuxt 3 documentations says that swr enables a static build, that lasts for a configurable TTL, however, nowhere was I able to find how exactly one would change the TTL & whether it can be set per-route. Is that possible? If so, how?

I've looked at github & also tried to find it in Vite / Nitro documentation but didn't find anything.

I found something about image TTL in Nitro config source files but I suppose that's not what I was looking for.

like image 288
Matej Avatar asked Sep 16 '25 13:09

Matej


2 Answers

After some investigation & experimenting, I found out that it is possible to adjust the TTL already! You just set it to an integer value instead of a boolean.

export default defineNuxtConfig({
    routeRules: {
        '/**': { swr: 5  }, // 👈🏻 TTL in seconds
    }
})
like image 118
Matej Avatar answered Sep 19 '25 02:09

Matej


This github issue is about that subject, it is still in the works (you can subscribe to it to get the latest updates!) but this is how the whole final API may look like:

export default defineNuxtConfig({
  routes: {
    '/': { prerender: true },
    '/blog/*': { static: true },
    '/stats/*': { swr: '10 min' }, // 👈🏻 TTL of 10 minutes
    '/admin/*': { ssr: false },
    '/react/*': { redirect: '/vue' },
  }
})
like image 35
kissu Avatar answered Sep 19 '25 01:09

kissu