elleracompany / craft-cookie-consent

GDPR-Compliant cookie consent banner for Craft CMS
Other
6 stars 5 forks source link

AMP templates - load asset js / css only when hook is set #48

Closed cookie10codes closed 4 years ago

cookie10codes commented 4 years ago

Hi!

We're using your plugin for our cookie consent with the asset javascript and css. We are now added AMP to our pages so removed the hook for the AMP templates. But the problem here is that the javascript and css are always been added to the DOM.

Is there any possibility to only add them when the hook is set?

Thanks for checking this out

Kind regards, Stefanie

jellingsen commented 4 years ago

Hi!

This might be hard, as we have no way of knowing if the hook is present in the twig file while loading the plugin.

I would recommend turning off the loading of JS and CSS in the plugin site settings, and load them manually if the request is not AMP.

You can do this with

if(!$isAmp)
{
        Event::on(Plugins::class, Plugins::EVENT_AFTER_LOAD_PLUGINS, function(Event $e) {
            Craft::$app->view->registerAssetBundle("elleracompany\\cookieconsent\\JSAssets");
            Craft::$app->view->registerAssetBundle("elleracompany\\cookieconsent\\CSSAssets");
        });
}

or even in your main twig layout file, but remember to also have the body tags as discussed here.

Hopefully this is a satisfactory solution for you.