Understanding Cookies in LiteSpeed Cache for WordPress

The relationship between cookies and caching can be easily misunderstood. When you talk about “caching cookies” or “not caching cookies,” it's not the cookies themselves that are being cached or not cached. It's the pages of the site that are being cached or not cached based on whether a user has those cookies stored. Cookies, generally, are ignored unless you specify otherwise.

Cookies become important when they effect the user experience in some way.

If a cookie must be set or read by WordPress, then it has to be excluded from cache. And, if the cookies are set on your site (i.e. they are not set somewhere before arriving at your site), then you will also have to exclude the page that sets the cookie's value.

Example

Your site is part of an affiliate network. When a user arrives at example.com/afilliate_home an aff-example cookie is set. As they navigate the site, the cookie is updated with tracking information.

In this case, the cookie aff-example must be added to the Do Not Cache Cookies list in Settings > Excludes, and ^/afilliate_home must be added to Do Not Cache URIs list on the same page. (For more on the Excludes page, see the LSCWP Excludes Settings wiki.)

If, in this example, the cookie was set at example.com/afilliate_home, but then never referenced again, you would not have to exclude the cookie from cache.

Alternately, if the cookie was set offsite somewhere, but was used for tracking as the visitor wandered around your site, then you would have to exclude the cookie from cache, but you wouldn't have to exclude the ^/afilliate_home URI.

Sometimes cookies can be used to indicate important information about the user to WordPress, to help determine what content should be shown to the user. In these cases, you can use the cookies to create cache varies. When LSCache varies on a cookie, it caches separate public versions of the pages of the site, based on the value of the cookie.

Example

Your WP site is a shop, and you have special pricing for your friends that is activated when they visit example.com/friends_home. That page sets a myfriend cookie, and from that point on, every page they visit in your shop shows pricing that is 20% less than normal. When a visitor without the myfriend cookie looks at the pages in your shop, they see regular prices.

Because the cookie is set on the example.com/friends_home page, that URI will need to be excluded from cache as described above.

There are two ways to deal with the cookie itself:

  • You could do as the previous example, and exclude it from cache. That's the easiest way, but it means your friends will always have uncached content, and that's not an ideal experience for them.
  • You could create a cache vary based on the myfriend cookie. This creates two separate variations in cache for every page: one for people with the myfriend cookie, and one for people without it. This option is slightly more advanced, as it requires you to edit your .htaccess file, like so.

You can learn more about cookies and cache varies in our Developer's Guide to Cache Vary.

  • Admin
  • Last modified: 2018/07/20 16:17
  • by Lisa Clarke