How to cache Vue components?

Although Vue's SSR is quite fast, it can't match the performance of pure string-based templating due to the cost of creating component instances and Virtual DOM nodes. In cases where SSR performance is critical, wisely leveraging caching strategies can greatly improve response time and reduce server load.

To avoid boilerplate, use Component Cache module for Nuxt.js. This module uses vue-server-renderer to add LRU cache support for Vue components.

Usage

  • Add @nuxtjs/component-cache dependency using Yarn or npm to your project
  • Add @nuxtjs/component-cache to modules section of nuxt.config.js
  1. {
  2. modules: [
  3. // Simple usage
  4. '@nuxtjs/component-cache',
  5. // With options
  6. ['@nuxtjs/component-cache', {
  7. max: 10000,
  8. maxAge: 1000 * 60 * 60
  9. }]
  10. ]
  11. }

See component-level caching for more information.

Don't forget, that

  • Cache-able components must define a unique name option.
  • You should NOT cache components, that
    • have child components that may rely on global state.
    • have child components that produce side effects on the render context.