2

I am trying to run a piece of code on every page load. I have the following

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="controller_action_predispatch">
        <observer name="csp" instance="MyVendor\MyModule\Observers\MyObserver" shared="false" disabled="false" />
    </event>
</config>

This works besides from when Full Page Cache is enabled. When it is enabled my observer does not run.

Is there a better event I should watch to enable me to run code on every page load regardless of if Full Page Cache is turned on or off?

1

I am not sure if there is a better event for it but you could instead use a plugin.

The FPC plugs into \Magento\Framework\App\FrontControllerInterface::dispatch you could define a plugin in order to execute your method before the FPC kicks in.

The FPC plugin is defined here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.