DEV Community

Cover image for How to enable Cache in Vue Storefront 2
Jakub Andrzejewski for Vue Storefront

Posted on

7 4

How to enable Cache in Vue Storefront 2

Cache is a really important concept in modern web development that allows to greatly improve the second load of the certain page and in general, improve the User Experience. If you are not yet familiar with it, I have published an article about it some time ago that you can read here.

In Vue Storefront cache can be enabled on both browser and the server. The first one will be using a Cache-Control response header to cache the response on the browser, while the second, will be using a cache driver like Redis to cache all pages.

Browser Cache

To enable Cache on the browser level, we will be using a http-cache package from Vue Storefront that you can check out here.

Installation

This package handles adding http-cache header to document after render for caching capabilities

First, install the dependency

yarn add @vue-storefront/http-cache
Enter fullscreen mode Exit fullscreen mode

Next, add it to the modules array in your nuxt.config.js:

['@vue-storefront/http-cache']
Enter fullscreen mode Exit fullscreen mode

And that's it. Thanks to this module, homepage, category page, and product page will be automatically returning a response header Cache-Control with a certain default value that will enable your browser to cache it properly. Check out the following section to see some configuration options.

Configuration

The package allows you to configure certain properties of it to make it work differently and suit your needs best.

default

This property allows you do override default value of http-cache header which is initially set to max-age=60

['@vue-storefront/http-cache', {
  default: 'max-age=120'
}]
Enter fullscreen mode Exit fullscreen mode

matchRoute

Customize http-cache values for selected routes. you can use * for a wildcard. To remove http-cache header use none value.

['@vue-storefront/http-cache', {
  matchRoute: {
    '/': 'max-age=240',
    '/p/*': 'max-age=360',
    '/c/*': 'none'
  }
}]
Enter fullscreen mode Exit fullscreen mode

Server Cache

To enable Cache on the server side, we can also use the packages provided by Vue Storefront, namely @vue-storefront/cache and @vue-storefront/redis-cache.

First, let's install the required dependencies

yarn add @vue-storefront/cache
yarn add @vue-storefront/redis-cache
Enter fullscreen mode Exit fullscreen mode

Next, let's add required configuration for the packages to work correctly

// nuxt.config.js

export default {
  modules: [
    ['@vue-storefront/cache/nuxt', {
      invalidation: {
        // Invalidation options
      },
      driver: [
        '@vue-storefront/redis-cache',
        {
          defaultTimeout: 86400,
          redis: {
            host: 'localhost',
            port: 6379,
            password: 'password'
          }
        }
      ]
    }]
  ]
};
Enter fullscreen mode Exit fullscreen mode

Check out the following docs to understand better the process of using server cache with Vue Storefront:

Summary

Well done! You have just enabled the Cache on both browser and server environments of your Vue Storefront application. This should improve the performance of you e-commerce website by a mile!

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)

ACI image

ACI.dev: The Only MCP Server Your AI Agents Need

ACI.dev’s open-source tool-use platform and Unified MCP Server turns 600+ functions into two simple MCP tools on one server—search and execute. Comes with multi-tenant auth and natural-language permission scopes. 100% open-source under Apache 2.0.

Star our GitHub!

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay