4

I have a site in which there are 2 states: unpublished and published.

When in the published state, images are served from my CDN with caching set for a month. Once published, no changes are allowed to the file so I'm never worried about it.

However, when in the unpublished state, I want to force the page to refresh every time it is viewed. User makes an update, previews their content to see the new file which has been hard refreshed to see the most recent updates.

How can I force a hard refresh based on whether or not the page is in a published or unpublished state?

tllewellyn
  • 713
  • 2
  • 5
  • 28
  • 1
    similar idea with caching of images - http://stackoverflow.com/questions/728616/disable-cache-for-some-images. By adding a randomly generated query string, ie. `?version=` to your page url, it should prevent caching. Alternately, other answers uses HTTP responses - http://stackoverflow.com/a/728685/689579 or doing the query string in javascript - http://stackoverflow.com/a/6116854/689579 – Sean Jun 28 '15 at 15:55
  • Sean, this did the trick, thanks! Not sure how to mark your comment as the answer, perhaps since I still have low rep...? – tllewellyn Jun 28 '15 at 16:08
  • I will add it as an answer so you can mark it. – Sean Jun 28 '15 at 16:09
  • 1
    There u go ttl don't spend all the points in 1 place ;) – Drew Jun 28 '15 at 17:13
  • @Drew Pierce I was wondering what was going on... Thanks, cheers! – tllewellyn Jun 28 '15 at 17:15
  • Now delete that csv u rascal – Drew Jun 28 '15 at 17:16
  • Already done, even cleared it out of the recycling bin! ;) – tllewellyn Jun 28 '15 at 17:17
  • Looks like the post was deleted, all is good in the world now. – tllewellyn Jun 28 '15 at 17:18

2 Answers2

2

Using the similar idea with caching of images -Disable cache for some images you can do this using

(1) By adding a randomly generated query string, ie.?version=<?php echo time();?> to your page url - https://stackoverflow.com/a/729623/689579
could also be done using javascript - https://stackoverflow.com/a/6116854/689579

(2) Use HTTP headers (in php)

header("Pragma-directive: no-cache");
header("Cache-directive: no-cache");
header("Cache-control: no-cache");
header("Pragma: no-cache");
header("Expires: 0");

https://stackoverflow.com/a/728685/689579

(3) Use meta HTTP headers

<meta Http-Equiv="Cache-Control" Content="no-cache">
<meta Http-Equiv="Pragma" Content="no-cache">
<meta Http-Equiv="Expires" Content="0">
<meta Http-Equiv="Pragma-directive: no-cache">
<meta Http-Equiv="Cache-directive: no-cache">

https://stackoverflow.com/a/14031623/689579

Community
  • 1
  • 1
Sean
  • 12,379
  • 3
  • 26
  • 44
0

I dont know if im getting what you are looking for exactly but maybe something like this?

$state = "unpublished";

function updatePage($state)
{
   if($state == "unpublished")
   {
   $page = $_SERVER['PHP_SELF'];
   header("Refresh: $sec; url=$page");
   }
}

call this function whenever any update is done.

john
  • 1