Caching: Force fresh content for logged-in users (potentially by adding a query variable to the URL)

If a logged-out user visits a page, and then revisit it when logged-in, it will show the cached version. There’s a closed question similar to this with an answer that suggests to set Cache Control to no-cache. This will affect logged-out users, which defeats the purpose of caching in the first place.

The problem

When using page/browser caching with T3 Total Cache or likely other caching plugins, if you have already visited a page when logged out, you will be presented with the same logged-out version of the page, unless if you refresh the page.

You can reproduce this issue by:

  1. Have caching enabled. Then, in incognito mode, visit a post.

  2. Login to the site and then manually type the URL of that post into the address bar. The logged-out version of the page should appear.

This is obviously an issue you have any features available for logged-in users only (like comments, voting etc)…

Potential solution

The only potential solution I have is to add a query variable to the URL on every request. For example by adding a random value:

$key = rand();
$new_url = esc_url( add_query_arg( 'foo', $key ) );

I know how to manually add this variable to single queries but instead of altering every single instant of links, is there a way how to force this to every URL for logged-in users?

Note: I’m not sure if this is the ideal approach to solve this problem (it feels quite desperate) so feel free to post an answer with a better solution.

2 s
2

Since you already have the “Don’t cache pages for logged in users” option activated, what about selecting cache with validation under Cache Control Policy for “HTML” content?

Explanation: Essentially, the “don’t cache pages for logged in users” option should already be sending a no-cache header for your logged users only.

If a user has a cached page in its browser (as a non-logged user) and then tries to access the same page when logged in, the cache with validation option inside your “Cache Control Policy” setting for HTML contents should be sending the must-revalidate tag.

Hence, a logged in user would send no-cache,must-revalidate, always requesting a fresh page.

This is pretty much what @gmazzap explained with headers – but from a W3 Total Cache configuration perspective.

Leave a Comment