DEV Community

Cover image for Shopify Hydrogen: Cache overview
Rafael Corrêa Gomes
Rafael Corrêa Gomes

Posted on • Updated on • Originally published at rafaelcg.com

Shopify Hydrogen: Cache overview

Using Shopify Hydrogen we need to be aware of how to leverage cache. My goal with this article is to clarify when and how to use full-page caching and sub-request caching. If you don't know what's Shopify Hydrogen I highly recommend checking the previous article Shopify Hydrogen Introduction.

During the development be aware of the two main cache systems. The right cache setup makes a huge difference in your project.

Native cache

There's a native cache system for all your components and pages, you need to know then in order to customize it.

The sub-request caches the API requests to Shopify and to third-party services, the following cache options are applied.

  • Cache the data for 1 second.
  • Serve stale data for up to 9 seconds while getting a fresh response in the background.
{
  maxAge: 1,
  staleWhileRevalidate: 9,
}
Enter fullscreen mode Exit fullscreen mode

The full-page caches the pages, the following cache options are applied.

  • Cache the page for 1 hour.
  • Serve the stale page for up to 23 hours while getting a fresh response in the background.
{
  maxAge: 60 * 60,
  staleWhileRevalidate: 23 * 60 * 60,
}
Enter fullscreen mode Exit fullscreen mode

During the development, you won't have cache, in case you need you can enable it customizing your vite/config.js

export default defineConfig({
  plugins: [hydrogen(shopifyConfig, {devCache: true})],
});

Enter fullscreen mode Exit fullscreen mode

How to set a custom cache?

If you need more and less time to keep or refresh the cache, then apply the customizations you need in your component or page. To be able to test it, enable the cache in your local environment by adding devCache:true.

Shopify Hydrogen custom cache

Sub-request

Shopify Hydrogen sub-request custom cache

Full-page

Shopify Hydrogen full-page custom cache

Enjoy it in your Shopify Hydrogen implementation 😉

Course Shopify Headless with Hydrogen

Top comments (1)

Collapse
 
elightwalk profile image
Elightwalk Technology

Great! Create a high-performance e-commerce store that offers a seamless experience, keeping your customers engaged and satisfied. Get in touch with us today to understand how our proficiency in Shopify Hydrogen can elevate your e-commerce platform!