#Third party cookie solution script with ClientRouter transitions not working after navigation

4 messages · Page 1 of 1 (latest)

fading siren
#

Hi, I'm trying to inject in the <head> tag of the document a third party script that creates a banner and a button to handle GDPR cookie management (Iubenda is the name of the service, but it acts exactly like Cookiebot or any other tool to manage accessibility etc.).

<script type="text/javascript" src="https://embeds.iubenda.com/widgets/XXXjs"></script>

When adding the script tag to the head of the site, it works on first page load (the cookie banner pops up, and also the little widget to edit the consent is showing), but after navigating to other pages with view transitions (ClientRouter) all the html elements disappear, like the script is not re-run.

I tried with data-astro-rerun, is:inline and also injecting the whole inline script with an astro page load listener around it, with no luck.

I confirm that disabling ClientRouter and returning to standard page navigation "solves" the problem, and all the injected widgets like the banner and the edit consent little button are shouwing correctly across all pages.

Is there anyone that could help?

I wonder if this problem would also occur with any other third party script that injects something into the DOM.

Many thanks!

sacred dawn
#

Hi @fading siren 👋,

you already found the knobs and switches.

Often, you need to reinitialize scripts after client router navigation as the navigation changes the DOM and that might break the scripts internal assumption about the page content and its own state.

Adding data-astro-rerun to the script tag will indeed re-execute it after navigation (if it is included and marked on both pages). But re-execution does not guarantee that the script is able to properly reinitialize its state.

You need to analyze the contents of the script to see what it does and how to fix it after navigation. Then you can use a listener to astro:after-swap to initiate the fixing after the swap.

fading siren
#

Yes, this is the key... I've already analyzed the Iubenda (cookie solution) script and seems that it does not handle properly re-initialize (like the Posthog one for example, that is properly working)

I will further go deep into the script and see what I can do

sacred dawn