-
-
Notifications
You must be signed in to change notification settings - Fork 418
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug]: Consent not showing with Astro View Transitions #728
Comments
The plugin is not an Astro component so it's expected that it doesn't integrate with the View Transition API. Astro is not aware of the html markup generated by the plugin, since it's dynamically injected on runtime. You need to somehow make the generated html and event listeners persist across pages. The plugin sets its classes on the top This is how I would do it;
<div transition:persist id="cc-container">
... content of plugin
</div> The plugin will then be configured to generate the markup inside that container: CookieConsent.run({
root: '#cc-container',
// ...
});
document.addEventListener('astro:before-preparation', event => {
window._htmlClassName = window.document.documentElement.className;
});
document.addEventListener('astro:before-swap', event => {
event.newDocument.documentElement.className = window._htmlClassName;
}); Here is a quick demo on StackBlitz. |
Thanks @orestbida ! I was hitting my head against the wall at this point, turns out restoring the Thanks a lot for taking the time to show the implementation. Edit:Seems it's not as "simple". 2 things:
As a permanent solution I just went with disabling page interaction until the cookie consent is either accepted or denied by the user: https://cookieconsent.orestbida.com/reference/configuration-reference.html#disablepageinteraction This allows you to maintain view transitions in Astro while also ensuring the consent is dealt with before the user navigates the page. It's a clean and easy solution in my opinion, without having to go through the hussle of all of this. In any other case, @orestbida if you have any suggestion for solving the second point since I'm not that "Astro savvy" please feel free to do so for anyone who wants to implement it anyways. |
Not sure, |
Huh! Something might be going on with my implementation. I'll keep investigating and letting you know if I find what it is. |
@orestbida solved! Over at the Astro Discord they helped me figure it out. Apparently <div transition:persist="find-me-on-the-other-side" id="cc-container"> You can ensure the persist clause isn't affected by other transition names. Not sure why your sample code worked and mine didn't, but maybe it's useful in that awesome StackBlitz of yours. |
Expected Behavior
The cookie consent banner still shows when the user changes page in an Astro app that uses view transitions.
Current Behavior
The consent loads just fine the first time the site gets loaded but then just doesn't appear again after a page change in Astro when using view transitions. If view transitions is removed, the cookie consent works just fine.
Steps to reproduce
astro:page-swap
event listener to re-execute the script after the user navigates to another page.astro
component like so:a. See how the event listener triggers with a console log
b. See how the banner doesn't come up again
Proposed fix or additional info.
Tried many ways to fix this:
astro:page-load
instead.js
or.astro
and importing the script in multiple ways, by usingimport
,src
, etc.None of these worked.
Please note that libraries such as AOS (animate on scroll) do work just fine by following this method. See: https://webreaper.dev/posts/astro-aos-view-transitions/
So it must be some kind of incompatibility between the Cookie Consent package and Astro with view transitions.
Version
3.0.1
On which browser do you see the issue?
Microsoft Edge
The text was updated successfully, but these errors were encountered: