We are transitioning over to a new forum platform. Please post new issues there. Existing threads will be dealt with here unless you choose to move them over. Visit the new forum

Forum

Thread tagged as: Question

Perch collection cache issue with cookies

I'm passing a cookie value to filter a Perch Collection.

This works fine, like below:

perch_collection('Some Collection', [
    'filter' => 'name',
    'match' => 'in',
    'value' => $some_cookie_value,
    'template' => 'some_template.html',
]);

The problem I'm facing is that I have to refresh the page for this to properly display the results. This is presumably because of some Perch page caching. Should I be facing this problem? I can see on the previous page that the cookie has been set, if I look in the Application tab of Chrome DevTools. If so, Can I do something to call a Perch Collection uncached?

FYI If I disable the browser cache then this works fine, but with the browser cache enabled I need to reload pages for the results to be displayed correctly.

Jay George

Jay George 2 points

  • 2 years ago

In doing some more testing I've noticed that just echo'ing out the php variable $some_cookie_value on the new page does not show the new values, even though I can see the values in the cookie on the previous page. Though if I refresh again on the new page they show.

So… this isn't actually Perch specific, it's more that the caching of pages is an anti-pattern to using cookies.

There must be a way around this, just my naivety in dealing with cookies that's tripping me up. If anyone has any pointers I would be grateful.

In researching I've found that there is a way to disable the cache.

header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Pragma: no-cache"); // HTTP/1.0
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");

This is obviously undesirable most of the time but I wonder if it's ok on certain pages that are displaying a collection based on cookie information.

For example, on the site I'm building there is a "favourites" feature where users can mark certain items as favourite, then when they click "favourites" a collection is output but filtered based on the list of favourites stored in the users' cookie.

I'm currently thinking that such a "favourites" page would be a good candidate for disabling the cache with the above code.

Thoughts? Is this totally the wrong way to go about things? If so, what's a better alternative?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Does it work if you try it?

The php way of disabling cache works, yes.

I know that cookies are checked in some official Perch things such as the Members App. Is there a better way to do this rather than just disabling the cache for filtered collections?

Drew McLellan

Drew McLellan 2638 points
Perch Support

It's not really a Perch issue - you should do whatever works for your project.