Forum
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.
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.
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?
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?
It's not really a Perch issue - you should do whatever works for your project.