Skip to content

[v6] Styles for Vue Components set with :deep are not applied during Dev when using ClientRouter #15898

Closed

Description

@amxmln

Astro Info

Astro                    v6.0.4
Vite                     v7.3.1
Node                     v22.22.0
System                   Linux (x64)
Package Manager          npm
Output                   static
Adapter                  none
Integrations             @astrojs/vue (v6.0.0)

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

Vue components typically have scoped styles. Sometimes, it is necessary to "break into" those scoped styles from a parent component to change things. For this, Vue provides the :deep-selector.

After upgrading to Astro v6, these styles set via the :deep-selector are no longer applied when running in dev mode with the <ClientRouter /> Astro component active. They appear (delayed) after the first load and a hard refresh, but navigating between pages does not apply them correctly.

Removing <ClientRouter /> fixes the issue. It doesn't seem like there is a correlation between components being hydrated with a client: directive or not, the issue persists in both cases.

After the site is built, the issue does not occur.

What's the expected result?

Styles applied with :deep are consistently applied on client-side navigations. Behaviour during dev matches that after build.

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-5h86mej1?file=src%2Fcomponents%2FComponentWithScopedStyles.vue

Participation

  • I am willing to submit a pull request for this issue.

Activity

  1. martrapp commented on Mar 25, 2026

    @martrapp
    Member

    @astrobot-houston would would be so kind to PTAL, your last run didn't complete.
    :deep() works with the client router for client:only but fails when used with client:load in dev mode.
    Vite's client code in dev mode translates

    &:deep(p) {
     color: white;
    }
    

    into

    &[data-v-50f77a40] p {
        color: white;
    }
    

    but that transform is only triggered on the initial load, not during the client router's soft navigation.

    find out, how we can trigger the transform in devmode during astros view transitions

  2. astrobot-houston commented on Mar 25, 2026

    @astrobot-houston
    Contributor

    I was able to reproduce this issue. In dev mode, Vite injects <style data-vite-dev-id="..."> elements whose textContent is later transformed by HMR — for example, Vue's :deep(p) becomes [data-v-xxx] p. When the ClientRouter performs a soft navigation, the persistedHeadElement function in swap-functions.ts does not recognize these Vite dev styles by their data-vite-dev-id attribute, so the already-transformed <style> element is discarded and replaced with the raw (untransformed) version from the new document. This causes :deep() styles to stop working after the first client-side navigation.

    I was able to fix this issue. The fix adds a match for <style> elements by their data-vite-dev-id attribute in the persistedHeadElement function, so the already-HMR-transformed style element is preserved across ClientRouter soft navigations instead of being replaced by the raw version. View Suggested Fix

    Priority P3: minor bug. This affects users who combine Vue scoped styles using :deep() with Astro's <ClientRouter /> in dev mode only — production builds are unaffected. Workarounds include performing a hard refresh or temporarily removing the ClientRouter during development.

    Full Triage Report

    No report.md was generated by previous triage steps. The following analysis was derived from the issue description, maintainer comments, and the fix branch.

    Reproduction

    The issue reproduces when using Vue components with :deep() scoped styles alongside Astro's <ClientRouter /> in dev mode. Styles appear correctly on initial hard load but are lost after soft (client-side) navigations.

    Root Cause

    In dev mode, Vite injects <style data-vite-dev-id="..."> elements into the document head. These styles are initially raw CSS that gets transformed by Vite's HMR client — for Vue components, :deep(p) is rewritten to [data-v-xxx] p.

    During a ClientRouter soft navigation, the function persistedHeadElement in packages/astro/src/transitions/swap-functions.ts determines which <head> elements to preserve vs. replace. It had no logic to match Vite dev styles by their data-vite-dev-id attribute, so the already-transformed <style> was replaced with the raw version fetched from the new page. The raw version had not yet been processed by Vite's HMR, so the :deep() selector remained untransformed and did not match any elements.

    Fix

    The fix (1 file changed, 10 insertions) adds a new matching case in persistedHeadElement (packages/astro/src/transitions/swap-functions.ts:189-198):

    if (el.tagName === 'STYLE') {
        const viteDevId = el.getAttribute('data-vite-dev-id');
        if (viteDevId) {
            return newDoc.head.querySelector(`style[data-vite-dev-id="${viteDevId}"]`);
        }
    }

    This ensures that Vite dev style elements are matched by their stable data-vite-dev-id, preserving the already-HMR-transformed content across soft navigations. Production builds are unaffected since data-vite-dev-id is only present in dev mode.

    This report was made by an LLM. Mistakes happen, check important info.

  3. added
    - P3: minor bug
    pkg: astro
    pkg: vue
    and removed
    triage: needs triage
    on Mar 25, 2026
  4. amxmln commented on Apr 17, 2026

    @amxmln
    Author

    This issue now happens again since v6.1.5 when #16242 reverted some of the changes of #16089.

    I think this should be reopened.

  5. martrapp commented on Apr 17, 2026

    @martrapp
    Member

    Hi @amxmln 👋,

    thank you for the heads up! I'm currently reworking the fix in the light of #16334.

    Could you do me a favour and tell me the data-vite-dev-ids of the styles that do not work for you?

  6. amxmln commented on Apr 17, 2026

    @amxmln
    Author

    Sure thing! I think it might be related to the use of preprocessors, as my minimal example above works fine if upgraded to the latest Astro version, but starts breaking once a CSS preprocessor such as SASS is added:

    Breaking: data-vite-dev-id="/home/projects/github-5h86mej1/src/components/ComponentWithScopedStyles.vue?vue&type=style&index=0&scoped=50f77a40&lang.scss"

    Working: data-vite-dev-id="/home/projects/github-5h86mej1/src/components/ComponentWithScopedStyles.vue?vue&type=style&index=0&scoped=50f77a40&lang.css"

  7. martrapp commented on Apr 17, 2026

    @martrapp
    Member

    Great, the new version doesn't insist on the file extension, see #16334 (comment)
    So scss and others should work fine, too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    - P3: minor bugpkg: astropkg: vue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions