Skip to content

[Bug] Compatibility issue with Astro 5 + ViewTransitions (Client Side Router) #814

Description

@DiegoSerra

Expected Behavior

The cookie consent banner should persist across page navigations or correctly re-initialize when the user navigates to a new page using Astro's Client Side Router (SPA mode). Event listeners on buttons (Accept/Reject) should remain active after navigation.

Current Behavior

When using Astro 5 with ViewTransitions enabled (<ClientRouter />):

  1. The banner appears correctly on the initial page load.
  2. Upon navigating to another page via a link, Astro swaps the <body> content.
  3. The banner either disappears completely (because it was injected into the old body) or remains visible (if persisted) but becomes unresponsive because its event listeners were attached to elements that no longer exist or the library's internal state is lost.
  4. transition:persist does not solve the issue reliably for this external library as it manipulates the DOM directly outside of the framework's component tree.

Steps to reproduce

  1. Create a new Astro 5 project.
  2. Enable the Client Router (add import { ClientRouter } from 'astro:transitions'; and <ClientRouter /> in the layout).
  3. Install and initialize vanilla-cookieconsent in the main layout/script.
  4. Start the dev server and open the homepage (Banner appears).
  5. Click any internal link to navigate to a subpage.
  6. Result: The banner vanishes or the buttons stop reacting to clicks.

Proposed fix or additional info.

According to this analysis on Astro 5 architecture, the issue stems from the library using document.body.appendChild

Version

3.1.0

On which browser do you see the issue?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingtriageyet to be reviewed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions