Skip to main content

Cached pages and ABs on cached pages

How to identify cached pages​

1. Using a browser:​

Network request for cached page Notice the cf-cache-status. Cached pages have that. It will usually be HIT, unless this is a completely new page or the cache was just flushed.

2. Using code​

Look for the method used for displaying the page - found under <comtroller_name>_controller.rb, def <action_name> eg:

def show
debug = false
headers['Cache-Control'] = ENV['STATIC_PAGE_TTL']

What does cached pages mean?​

When a page is cached using cloudflare here is what happens,​

  1. When user 1 (Charlie) goes to https://www.powr.io/tutorials/how-to-add-instagram-feed-app-to-your-bigcartel-site, cloudflare sends that request to POWr.
  2. POWr generates the HTML and sends it back.
  3. Then if user 2 (Mary) goes to the same url, cloudflare will take the same HTML POWr had created to Charlie and send it to Mary. POWr never receives that request. This is how we can ensure speed and less load on our servers, makes google and SEO happy and leads to more organic traffic. Makes all of us happy.

Why should we not add user/session specific code to cache pages?​

Suppose we were running an AB where everyone name Charlie gets a free cookie from POWr and everyone named Mary receives a cupcake and everyone else gets nothing. If the page is cached, not just Mary but everyone including Charlie receives a cookie. That would be a lot of cookies that we'd be giving away for free and it would all result in bad data.

Bad analogies aside, everyone in the AB would end up in the same bucket as the first user who hit first hit the page.

- if headers['Cache-Control'].blank?
= render partial: 'layouts/javascript_params.html'

How to add user/session related logic to cached pages? If not already present create a controller-action.js file All a function to it.

window.after_signin_callback = () => {}

anything you need to happen that assumes current user is present should be done inside of that function. This will also add other variables like host, is_eu etc that are session/user specific.

Here is an example MR by Anuarbek that adds an AB for homepage- https://gitlab.com/powr/powr/merge_requests/2729/diffs

Here is my attempt at a Be Like meme -