Ecommerce for Statamic

Statamic Caching

Statamic 1.7.8 brings along a great new feature. Rendered HTML caching.
They also wrote all about it.

Simply put, it saves the final rendered HTML output of your page to a file so next time that page is visited, it doesn’t need to do (almost) any PHP stuff. No template parsing, nothing like that. You can set how long the cached pages last for. It’s wonderful, your sites will fly.

They also have a feature that’s been around a bit longer called HTTP cache expires.
What this one does is cache the page to your browser for a set amount of time.


Don’t use caching for your Bison sites.

Read on to understand why.

The problem

Rendered HTML Caching

Why shouldn’t you use the new HTML caching feature? Let’s explain what’s happening:

  1. When a page is visited for the first time, Statamic will generate the page as normal.
  2. It’ll get saved as a file. It’s basically nothing more than an oldschool static html page.
  3. The next time anyone hits that page, the static html page will be served.

Did you catch that? The next time anyone hits it, the cached page will be served.

This is a big problem when you’re display user-specific information on pages, like the contents of a shopping cart.

Think about this:

  • Jack adds a product to his cart. The cart item count in the header displays 1. Perfect!
  • Fred starts browsing the site. The header shows 1 item in the cart. Wait - Fred never added anything…
  • Jack adds another product to his cart. The header shows 2 items. Good, right?
  • Jack goes to the page he visited a moment ago. The header shows 1 item again. Whaat?

Confused? Exactly. Whatever is generated at that particular moment—for whoever—will be served again—for anyone—for that page.

HTTP Cache Expires

With this version of caching, the page will get saved to your users’ browsers for a predetermined period of time. With the method mentioned before, a page might be cached between users, this version will cache it for just that user.

It’s still a problem because your cart might say one number on one page but another number on another page.

The solution

Unfortunately, in most cases the solution is to just disable caching entirely.
This goes for rendered HTML and HTTP cache expires.

Although, if you’re really set on using the new rendered html caching, you can make sure to have a clear separation of pages that contain user-centric information and pages that don’t. You could then enable caching but use the exclude option to prevent caching pages that contain user-centric data.

  • Remove the cart count from your header (or use AJAX to add it on page load)
  • Remove the ‘Logged in as Fred’ from your header
  • Only display cart data on pages like /cart, /checkout, etc. Exclude these pages from being cached.