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 server-side caching strategies in your Nuxt projects:
Nuxt.js, powered by Nitro, allows you to define caching rules for specific routes using the routeRules
property in nuxt.config.ts
. This feature supports:
/
) can be pre-rendered.export default defineNuxtConfig({
routeRules: {
'/': { prerender: true },
'/products': { swr: true },
'/products/**': { swr: 3600 }, // Cache for 1 hour
}
});
For API routes, you can apply caching by specifying TTL (time-to-live) values in routeRules
. For instance, caching responses from /api/*
for an hour using maxAge
can offload server pressure.
Example:
export default defineNuxtConfig({
routeRules: {
'/api/*': { cache: { maxAge: 3600 } } // Cache for 1 hour
}
});
The defineCachedEventHandler
is part of the server-side caching API in the Nitro engine, which powers the server-side of Nuxt. This method allows you to cache the results of server-side event handlers that fetch data or perform other operations. It caches the handler’s response and serves cached data for subsequent requests, reducing the server load.
Example:
export default defineCachedEventHandler(async (event) => {
const data = await fetchData();
return data;
}, {
maxAge: 60 * 60 // Cache for 1 hour
});
This function is part of Nitro’s powerful server-side caching mechanism, allowing you to cache responses and reduce redundant server processing, especially for data that changes infrequently.
Learn how to balance API caching between RouteRules and defineCachedEventHandler
Similar to defineCachedEventHandler
, defineCachedFunction
is another utility from the Nitro caching layer that allows you to cache the result of any server-side function. This is useful for server-side computations or data fetching functions that are expensive to run frequently.
defineCachedFunction
allows you to cache the result for a specified duration, improving the performance of your server-side logic.Example:
const cachedFunction = defineCachedFunction(async () => {
const result = await expensiveFunction();
return result;
}, {
maxAge: 3600 // Cache for 1 hour
});
This approach is especially useful when you want to cache computational results or data fetches that don't need to be executed repeatedly within a short period.
Nitro supports deploying Nuxt apps on serverless and edge platforms (e.g., Vercel, Cloudflare Workers), which can handle edge-side caching to serve cached pages with super-low latency.
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.
By using Unstorage with drivers like Redis or memory, you can implement server-side caching for dynamic content. This is useful for API endpoints or server-rendered pages, where Unstorage can cache responses for a defined duration (TTL) and serve them directly from the cache to improve performance.
When deploying Nuxt apps on edge networks or serverless platforms like Cloudflare Workers or Vercel, Unstorage can be used to manage edge-side caches through drivers like Cloudflare KV or HTTP-based drivers, ensuring fast delivery of cached content from the closest server to the user.
Since Nuxt 3 is built on the Nitro engine, Unstorage can be combined with Nitro’s caching mechanisms, such as defineCachedEventHandler
, to store and retrieve cached data across different drivers. This adds a layer of flexibility, allowing you to cache data server-side and optimise the retrieval of dynamic data.
In this scenario, the API response is cached in memory and can be reused, reducing the number of server requests.
By leveraging Unstorage in your Nuxt project, you can implement a flexible, powerful caching layer across both server-side and client-side environments, optimising performance across various platforms and deployment environments.
I've also written about Client-side caching strategies in Nuxt, which might be worth checking out. It covers key techniques to optimize performance by effectively caching data on the client side.
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!