Possible to ignore some parts of pages from cache?

#1
Hello,
Actually, I display some content in frontend only when admins are logged in for example in category page.
In a CategoryController.php, I test a cookie admin
$cookie = new Cookie('psAdmin');
if this cookie is an admin cookie, I assign a variable to smarty.
$this->context->smarty->assign('specialInfo', $infoForAdminOnly);
in the smarty template I display this content if the variable specialInfo is set.
{if !empty($specialInfo)}{$specialInfo}{/if}

Without the LS Cache module, once the admin is not logged, the specialInfo content disappears.
With the LS Cache module, the specialInfo content continues to appear.

So is it possible to ignore these special parts from cache?
I was thinking to use ESI blocks but from what I understand it only works on module parts. Is it correct?
 

serpent_driver

Well-Known Member
#2
ESI works almost everywhere, but the cache plugin for PrestaShop only supports module.

You can exclude a specific user like the admin from caching if the admin has a unique cookie. This completely excludes the admin from caching.

Apache config:
<IfModule Litespeed>
RewriteCond %{HTTP_COOKIE} admin_cookie_name [NC]
RewriteRule .* - [E=Cache-Control:no-cache]
</IfModule>
 
#3
ESI works almost everywhere, but the cache plugin for PrestaShop only supports module.

You can exclude a specific user like the admin from caching if the admin has a unique cookie. This completely excludes the admin from caching.

Apache config:
<IfModule Litespeed>
RewriteCond %{HTTP_COOKIE} admin_cookie_name [NC]
RewriteRule .* - [E=Cache-Control:no-cache]
</IfModule>
Thank you for your reply but I see that the LiteSpeed module generates a specific code in apache conf
Apache config:
<IfModule Litespeed>
CacheLookup on
RewriteEngine on
RewriteCond %{HTTP_COOKIE} !PrestaShop-
RewriteRule .* - [E=Cache-Control:vary=guest]
</IfModule>
I guess 'PrestaShop-' is a cookie used for guests or logged in users. Am I wrong? If it's true It should not show the same content for a logged in user or a guest. Am I wrong?
I saw in the cookies a cookie named 'id_employee' which is present even if I logged out from the administration dashboard so I think the cookies are not deleted after logout. So if they are not deleted can I really rely on them for managing the cache?
Do you know how to find the admin_cookie_name?
 

serpent_driver

Well-Known Member
#4
I guess 'PrestaShop-' is a cookie used for guests or logged in users. Am I wrong? If it's true It should not show the same content for a logged in user or a guest. Am I wrong?
Yes and no. The code you posted depends on settings in cache plugin and is only set if you have Guestmode enabled. If enabled there are 2 different cache copies. One at first request without cookie and the other one after cookie is set. At first request without cookie the shop doesn't check the user which language he has and some other facts. This makes page loading a bit faster, but PrestaShop- cookie hasn't something to do if a user is logged in or not. It is only for Guestmode.

I saw in the cookies a cookie named 'id_employee' which is present even if I logged out from the administration dashboard so I think the cookies are not deleted after logout. So if they are not deleted can I really rely on them for managing the cache?
Do you know how to find the admin_cookie_name?
id_Employee cookie isn't a cookie that set by PrestaShop. As far as I know PrestaShop writes customer status and customer group in PHP session, so there is no way for you to check the admin, because there isn't any special cookie.
 
#5
Thank you so the best solution would be to create a simple module to show a block of text only visible for admins.
I would have something like that
Code:
{hook h="litespeedEsiBegin" m="ps_blockforadmin" field="widget_block" tpl="module:ps_blockforadmin/ps_blockforadmin-text.tpl"}
{widget_block name="ps_blockforadmin"}
{include 'module:ps_blockforadmin/ps_blockforadmin-text.tpl'}
{/widget_block}
{hook h="litespeedEsiEnd"}
Do you think it can work?
 
#7
I created a module myadminblocks.
It is very simple.
I have a php file for the module (Below it's only an extract)
PHP:
class MyAdminBlocks extends Module implements WidgetInterface {
   private $templateFile = "module:myadminblocks/myadminblocks.tpl";
    //...
    public function getWidgetVariables($hookName, array $configuration)
    {
        $cookie = new Cookie('psAdmin', '', (int)Configuration::get('PS_COOKIE_LIFETIME_BO'));
        
        $isAdmin = false;
        if (isset($cookie->id_employee) && $cookie->id_employee) {
            $isAdmin = in_array($cookie->profile, array(1,6));
        }
        
        return array(
            'isAdmin' => $isAdmin,
            );
    }
    
    public function renderWidget($hookName, array $configuration)
    {
        $this->smarty->assign($this->getWidgetVariables($hookName, $configuration));
        return $this->fetch($this->templateFile);
    }   
}
I have a template file (myadminblocks/myadminblocks.tpl) with the following content
Code:
{if $isAdmin}
<p>ESI Admin BLOCK</p>
{/if}
In the product pages I added the following content
Code:
{hook h="litespeedEsiBegin" m="myadminblocks" field="widget_block" tpl="module:myadminblocks/myadminblocks.tpl"}
{widget_block name="myadminblocks"}
{include 'module:myadminblocks/myadminblocks.tpl'}
{/widget_block}
{hook h="litespeedEsiEnd"}
I installed the module then I created an ESI block from the Litespeed configuration page.

It's a little better. Now if I am logged in Chrome I see the special block for admins and I go to another browser or if I am in private mode I don't see this special block.
But If I logout from the administration dashboard I continue to see this special block.

id_employee is a cookie set by PS.
Just to be sure I created a simple PHP file at the root of my website to test it with the following content
PHP:
<?php
require_once dirname(__FILE__).'/config/config.inc.php';
$cookie = new Cookie('psAdmin', '', (int)Configuration::get('PS_COOKIE_LIFETIME_BO'));

if (isset($cookie->id_employee) && $cookie->id_employee) {
    echo 'ID Employee: '.$cookie->id_employee;
} else {
    echo 'No employee logged in.';
}
When I log in the administration dashboard then I visit this page I see 'ID employee: 2'.
When I log out, I see 'No employee logged in'.
So I conclude that I can check this cookie (in the PHP code).

But it seems that the method renderWidget(...) is not called each time.

Maybe I can create a cookie 'admin_cookie_name' as you suggested so I would be able to exclude the admin from caching but my problem is that I don't know how to delete this cookie once the user has logged out. Is there any hook allowing me to do that?
Or is there something I can configure in the esi block configuration page?
 

serpent_driver

Well-Known Member
#8
Maybe I can create a cookie 'admin_cookie_name' as you suggested so I would be able to exclude the admin from caching
This is the way I recommend.

but my problem is that I don't know how to delete this cookie once the user has logged out. Is there any hook allowing me to do that?
I can answer this question only generally. I know much about LScache, but don't know everything about details how each cache plugin works, because every LiteSpeed cache plugin is different. If you are able to set a cookie only for the admin it doesn't matter who or if a user is logged in or not. If it is admin the rule is no-cache and only this matters.
 
#9
This is the way I recommend.



I can answer this question only generally. I know much about LScache, but don't know everything about details how each cache plugin works, because every LiteSpeed cache plugin is different. If you are able to set a cookie only for the admin it doesn't matter who or if a user is logged in or not. If it is admin the rule is no-cache and only this matters.
I will set the cookie in the admin controller.

Thank you for your help!
 
#10
Finally, I tried it but it's acting weird.

I added the code in suggested in .htaccess file after the Litespeed generated code.
Code:
<IfModule LiteSpeed>
CacheLookup on
RewriteEngine on
RewriteCond %{HTTP_COOKIE} !PrestaShop-
RewriteRule .* - [E=Cache-Control:vary=guest]
</IfModule>
### LITESPEED_CACHE_END
<IfModule Litespeed>
RewriteCond %{HTTP_COOKIE} admin_cookie_name [NC]
RewriteRule .* - [E=Cache-Control:no-cache]
</IfModule>
This is my scenario:
I log in the PS admin dashboard with Chrome. I purge all the cache.
In another browser tab (in Chrome), I go to a page where a content for admin is visible.
I go to another browser (Firefox) and I go to the same page => I see the same content as the admin in Chrome.
I logout from admin dashboard. I go to Firefox. I refresh => I continue to see the same content.
In Chrome, I refresh the page => The content for admin disappears
In Firefox, the content has disappeared.

It seems that when the administrator is logged in and visits a page then the cache (for the visited page) is cleared for all users not only for him.
Is that how it should work?
 
Top