OnLoadWebDocument even running only once not after refreshing the page?

Hello I still having issues with the :face_with_symbols_over_mouth: cache headers, just one quick question hope somebody can guide me in case I’m wrong.

My problem is simple:

I’m adding a header in my html, a simple Last-Modified: DATE-HERE.

plugin OnLoadWebDocument

code:

<?php
$id = $modx->resource->get('id');
$page = $modx->getObject('modResource', $id);
$editedon = $page->get('editedon');
$strtotime = strtotime($editedon);
$editedonDate = gmdate("D, d M Y H:i:s", $strtotime) . " GMT";

header("Last-Modified: $editedonDate");

I tested in an incognito window it works fine, I can see it on developer tools/network BUT
if I press F5 and refresh the page, it’s gone!

WHY??? :exploding_head:

Is there a system event that is executed when the page refresh or something like that?
I mean to make the code persistent and not just go away to the underworld?
Am I doing something wrong?

Thank you very much. :+1:

After you add them, the headers are part of the original request, but reloading the page is a new request. You might look at OnHandleRequest.

BTW, why not this instead of your first four lines?

$editedon = $modx->resource->get('editedon');

@bobray mate thanks for your help. 2 things:

  1. OnHandleRequest didn’t work to set the headers on refresh
  2. You are 100% right regarding $editedon = $modx->resource->get('editedon'); but used with OnHandleRequest brakes the whole site including the manager.

Any ideas?
Thank you very much!

The event “OnHandleRequest” is invoked before the MODX code determined which resource to load for this request. Therefore $modx->resource is not yet available in this event.

1 Like

I have no experience messing with headers. If your goal is to convince the browser the page has been modified, is there any way you can add the editedon date to the URL as part of a query string?

https://mypage.com? &ed=01/03/2023

That is what I thought @halftrainedharry , thanks mate!

But I just have one big doubt and I hope you can help me with this.

If you se my first post I’m running the headers on OnLoadWebDocument
That is working fine only on the first time the website is loaded, if you press F5 to refresh the headers from the plugin just disappear (I’m caching everything but the HTML file).

BUT…

If I’m logged into the manager the headers are there all the time even after refreshing the page, so if I test them everything looks fine but once the site is testes on an incognito window or after log out from the manager no more headers after refresh.

So how can I make the headers Persistent if I’m not logged in the manager?

Thanks!

I can’t reproduce this.

I created the plugin with the code you posted above and tested it.
The header is always present, no matter if I’m logged in, logged out or use a private browser window.


The event OnLoadWebDocument should run on every (frontend) request.
If you’re logged in or not shouldn’t make a difference.

1 Like

Thanks @halftrainedharry
I’ll check what’s going on here, then.

Appreciate your help mate, as always!
Cheers!