Jump to content

Adding a comment to markup for cached pages


ethanbeyer
 Share

Recommended Posts

Not sure if this is possible - but I'd like to add something like this to cached pages via Template Cache:

<!-- Page Cache Generated 2018-05-22 @ 12:45:44 UTC / Expires 2018-05-29 @ 12:45:44 -->

I thought there might be a way to see if a cache file existed for the page in question, and if it did not, to include that comment in the render->cacheFile->save().

Does anyone know?

Link to comment
Share on other sites

I haven't used template cache because I use ProCache instead, but wouldn't it simply be a matter of including the current date/time inside an HTML comment in your template files (or _main.php if using delayed output)? When a cached version is served you will see the time that was current when the markup was generated.

Link to comment
Share on other sites

In theory, yes - but the largest problem with that approach is that the timestamp is always there. This is mostly for my OCD brain, but I essentially wanted a sanity check in the source that would quickly show that a page was or was not cached, and when.

Link to comment
Share on other sites

12 hours ago, ethanbeyer said:

the largest problem with that approach is that the timestamp is always there

If you only want to output the timestamp comment if the page has template cache enabled you could do a conditional like this:

if($page->template->cache_time > 0) {
    // Output your timestamp comment
}

There are some other cache-related properties present in Template objects that you can explore with Tracy Debugger in case they are useful to your goal.

bd($page->template);

 

  • Like 1
Link to comment
Share on other sites

13 hours ago, Robin S said:

If you only want to output the timestamp comment if the page has template cache enabled you could do a conditional like this:


if($page->template->cache_time > 0) {
    // Output your timestamp comment
}

At the risk of sounding very pedantic, this condition would result in the timestamp for any page with template cache enabled, which is not what I'm looking for.

I only want the timestamp to appear on cached versions of the page - so if I am logged in, and am served a page that doesn't give cached pages to logged-in users, I don't want the timestamp (or maybe I would have a different comment that said this page was generated now and is not cached, etc).

On the other hand, if I was not logged in, and looked at the source for the same page and was served the Cached version, I'd expect to see the timestamp.

Essentially, I am looking to inject the comment ONLY at the time of the creation of the cached page, not on normal render - so this makes me wonder if there's a hook I could grab before page render to determine whether or not the page render was being performed in order to create a CacheFile.

Does this make sense?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...