In Nuxt.js, both client-side and server-side caching strategies are critical for optimising performance and reducing server load. Here’s how you can apply different client-side caching strategies in your Nuxt projects:
getCachedData
:Using useFetch
and useAsyncData
composables, you can enable client-side caching of fetched data. getCachedData
is an option used in the useFetch
and useAsyncData
composable to manage cached data. It provides a mechanism to fetch cached data for a particular key before making a new request. If cached data is available, it returns the cached response; otherwise, it triggers a fresh fetch.
useAsyncData('myKey', async () => {
return await $fetch('/api/data')
}, {
getCachedData: (key) => {
// Ref: https://nuxt.com/docs/api/composables/use-async-data#params > options > getCachedData
const data = nuxtApp.payload.data[key] || nuxt.static.data[key]
if (!data) {
return
}
return data
}
})
When you fetch the data from an API endpoint using either useFetch
or useAsyncData
, resulting payload is stored in payload.data
. Nuxt's caching mechanism makes sure to prevent redundant API calls if an identical request is made twice. This enhances our app performance by reducing the need to re-fetch the same data multiple times.
useNuxtData
ComposableThe useNuxtData
composable is primarily used for client-side state management and caching in Nuxt. It provides an easy way to access and persist shared data across different components or pages, without repeatedly fetching it from the server. This composable can automatically cache data during the server-side rendering phase and rehydrate it on the client-side, allowing for seamless data usage between server and client.
Client-Side Caching: When data is fetched on the server (e.g., during asyncData
or fetch
hooks), Nuxt caches the result and automatically makes it available through useNuxtData
on the client, so that you don’t need to refetch it again after hydration.
Hydration: Data is cached during SSR and is available during client-side rendering, reducing redundant API calls.
Example:
const { data } = useNuxtData();
useNuxtData
primarily helps with caching already-fetched data on the client and improves performance by reducing unnecessary server requests when the data is already available.
This is mostly about ensuring data that’s already fetched is reused on the client side, reducing duplicate network requests. It works well when data is fetched via asyncData
, useFetch
, or fetch
hooks during SSR, and is then passed to the client during hydration.
Unstorage is a universal key-value storage solution that integrates well within the Nuxt.js ecosystem, particularly for caching and persistent storage across multiple environments.
Although Unstorage is more focused on server-side storage, its drivers (like localStorage or IndexedDB) can be used for caching data client-side in browser environments. This helps reduce redundant network requests and improves user experience by caching resources locally.
Workbox is a robust tool that significantly enhances caching capabilities for Progressive Web Apps (PWAs) by simplifying the process of creating service workers, managing caches, and implementing caching strategies.
We can use Workbox within Vite via vite-plugin-pwa, to automate and fine-tune caching strategies for both static and dynamic assets, enabling offline capabilities, faster loading times, and enhanced user experiences.
I've also written about Server-side caching strategies in Nuxt, which you might find helpful. It covers various techniques that can optimize your app's performance by caching responses efficiently on the server side. Feel free to check it out!
By the way, if you didn't know, I'm also working on a Nuxt book! Performance optimization plays a crucial role in building efficient Nuxt applications, and it's a topic I’ll be covering in my upcoming book. Stay tuned for more insights in the book! You can subscribe and follow my progress over on NuxtDojo Substack. I'd love to have you join me on the journey!