4

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.

Matej
  • 155
  • 1
  • 15

2 Answers2

4

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
    }
})
Matej
  • 155
  • 1
  • 15
  • Sorry if my answer wasn't specific enough regarding the seconds. – kissu Dec 12 '22 at 14:36
  • No problem, I'm just having trouble with specifying it to work only for some routes. When I enable it on '/test-swr': { swr: 5 }, it doesn't work for some reason... – Matej Dec 14 '22 at 08:17
  • Is `/test-swr` specified above or below `/**`? Try putting it above. – kissu Dec 14 '22 at 10:51
  • seem like no matter where I put it, the `/**` overrides the TTL for all routes this is what it looks like: ` routeRules: { '/no-swr/time-test': { swr: 0 }, '/**': { swr: 10 }, } ` and when I only enable it for a specific route, it stays disabled ` routeRules: { '/time-test': { swr: 10 }, } ` – Matej Dec 14 '22 at 21:24
  • @Matej where do you host your app? I was told swr works on Netlify only and would like to check that. Thanks. – David Dahan Feb 21 '23 at 12:30
  • @DavidDahan there are several places where SWR may be working, mainly most of the Edge Rendering (SSR) places like Cloudflare workers, Vercel etc IMO. – kissu Feb 21 '23 at 15:13
  • @kissu thanks I was wondering because on [this page](https://nuxt.com/docs/guide/concepts/rendering#route-rules) I can read both `currently Netlify and Vercel are supported` and `currently enables full incremental static generation on Netlify, with Vercel coming soon`. So I'm not sure if Vercel is actually supported. – David Dahan Feb 21 '23 at 18:16
  • 1
    @DavidDahan ISR is not the same as SWR and I think that it should be supported indeed. – kissu Feb 21 '23 at 21:05
2

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' },
  }
})
kissu
  • 40,416
  • 14
  • 65
  • 133