Skip to content

ClientRouter: <video muted> loses its muted state after navigation in Chrome, so script play() is audible #18152

Closed

Description

@theajlabs

Astro Info

Astro                    v7.3.5
Vite                     v8.3.1
Node                     v26.5.0
System                   macOS (arm64)
Package Manager          npm
Output                   static
Adapter                  none
Integrations             none

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

Chrome (Firefox not tested)

Describe the Bug

Since 7.2.1 (#17603), reifyMediaElements() in packages/astro/src/transitions/swap-functions.ts rebuilds every <video> and <audio> after a swap with document.createElement() followed by setAttribute() for each attribute.

In Chrome stable, a muted attribute added with setAttribute() does not mute the element. Blink only applies it when the parser creates the element (html_media_element.cc, the AttributeModificationReason::kByParser check); the behaviour the current HTML spec describes (whatwg/html#12389) is behind the MediaElementMutedDefaultState runtime flag, which is still experimental. So after a ClientRouter navigation, video.hasAttribute('muted') is true but video.muted is false.

That turns into audible sound on any page that starts muted video from a script (a carousel, play on scroll): the click that triggered the navigation gives the page sticky activation, so Chrome allows the unmuted play(). The same markup on a full page load is muted.

Steps with the repro, in Chrome 153.0.8010.53 on macOS:

  1. Open /video directly: muted attribute: true, video.muted: true.
  2. Go to / and click the link to /video: muted attribute: true, video.muted: false.

Adding this line after the attribute copy in reifyMediaElements() fixes it in the repro (step 2 then shows video.muted: true):

if (media.hasAttribute('muted')) (fresh as HTMLMediaElement).muted = true;

Workaround in user land: set video.muted = true on astro:page-load for every video that should be muted.

What's the expected result?

A <video muted> is still muted after a ClientRouter navigation, as it is on a full page load, so a script calling play() on it stays silent.

Link to Minimal Reproducible Example

https://github.com/theajlabs/astro-video-muted-repro

Participation

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

Activity

  1. added
    triage: in progress
    on Sep 28, 2026
  2. astro-factory commented on Sep 28, 2026

    @astro-factory
    Contributor

    Triage complete

    • Prepare the workspace: ready
    • Install dependencies: complete
    • Build the project: complete
    • Reproduce the issue: confirmed
    • Diagnose the cause: high confidence
    • Verify the diagnosis: bug
    • Attempt a fix: verified
    • Publish the result: set triage: fix pending

    • Reproduced: Yes — confirmed in Chrome 154 using the provided reproduction repo
    • Exploration: Yes — View branch
    • Unit Test: No — this is a Chrome-specific browser behavior; added an e2e test (packages/astro/e2e/view-transitions.test.ts) using Playwright with Chromium instead
    • Priority: P4: important. This is a regression from 7.2.1 (Fix video and audio elements broken after view transition navigation #17603) that silently breaks <video muted> on every ClientRouter navigation in Chrome/Chromium — the dominant browser. Any site using muted autoplay video (carousels, play-on-scroll) will unexpectedly play audio after navigation.

    The root cause is in reifyMediaElements() in packages/astro/src/transitions/swap-functions.ts (line 230). When recreating media elements after a ClientRouter swap, all attributes are copied via setAttribute(). In Chrome, setAttribute('muted', '') on a document.createElement()-created element sets defaultMuted = true but does not sync the muted IDL property — Chrome only does that when the HTML parser creates the element. So after navigation, video.hasAttribute('muted') is true but video.muted is false.

    I found a potential fix for this issue. The fix adds one line after the attribute copy loop to explicitly set fresh.muted = true when the muted attribute is present, mirroring what the HTML parser does on a full page load. Verified that both direct navigation and ClientRouter navigation now correctly report video.muted: true.

    Try this fix

    You can test this fix right now without waiting for a release:

    # astro
    npm i https://pkg.pr.new/astro@83667bd

    If this fixes your issue, please leave a comment letting us know (for example
    "confirmed, this fixes it"). A pull request will then be opened to get it merged.

    Full Triage Report

    Issue Title

    ClientRouter: <video muted> loses its muted state after navigation in Chrome, so script play() is audible

    Reproduction

    Successfully reproduced using the provided reproduction repo against current main with Chrome 154.

    • Direct navigation to /video: muted attribute: true, video.muted: true ✅
    • ClientRouter navigation from / to /video: muted attribute: true, video.muted: false ❌

    Root Cause

    File: packages/astro/src/transitions/swap-functions.ts
    Function: reifyMediaElements() (lines 225–235)
    Introduced by: PR #17603 (Astro 7.2.1), commit 722eed64c

    reifyMediaElements() was added to fix #17601 (inert media from DOMParser). It recreates each <video>/<audio> via document.createElement() and copies attributes with setAttribute(). In Chrome, setAttribute('muted', '') on a script-created element sets defaultMuted = true but leaves video.muted = false. The HTML parser normally syncs both, but Chrome's AttributeModificationReason::kByParser check in Blink means this only happens for parser-created elements. The spec-conformant behavior is behind an experimental flag (MediaElementMutedDefaultState, see whatwg/html#12389).

    Affected Code Path

    1. User clicks a link → ClientRouter intercepts
    2. New page fetched and parsed via DOMParser
    3. swapBodyElement() → reifyMediaElements() recreates media elements
    4. setAttribute('muted', '') copies the attribute but not the IDL property
    5. Script calls play() → video plays with sound (page has sticky activation from the click)

    Fix

    Added one line in reifyMediaElements() after the attribute copy loop:

    if (fresh.hasAttribute('muted')) (fresh as HTMLMediaElement).muted = true;

    Verification

    • Before fix: ClientRouter nav → video.muted: false ❌
    • After fix: ClientRouter nav → video.muted: true ✅
    • Direct navigation: unchanged, still correct ✅

    Tests

    • E2E test added: packages/astro/e2e/view-transitions.test.ts — "reified <video muted> preserves the muted IDL property after navigation"
    • Test fixture: packages/astro/e2e/fixtures/view-transitions/src/pages/video-muted.astro

    Changeset

    • .changeset/wet-times-doubt.md — astro: patch

    Git diff

    --- a/packages/astro/src/transitions/swap-functions.ts
    +++ b/packages/astro/src/transitions/swap-functions.ts
    @@ -230,6 +230,10 @@ function reifyMediaElements(root, liveMedia) {
     			fresh.setAttribute(attr.name, attr.value);
     		}
     		fresh.innerHTML = media.innerHTML;
    +		// Chrome only syncs the muted IDL property from the content attribute when the
    +		// HTML parser creates the element; setAttribute('muted', ...) on a script-created
    +		// element leaves video.muted === false. https://github.com/withastro/astro/issues/18152
    +		if (fresh.hasAttribute('muted')) (fresh as HTMLMediaElement).muted = true;
     		media.replaceWith(fresh);
     	}
     }

    This report was made by an LLM. The analysis may be wrong, and the potential fix might not work, but is intended as a starting point for exploring the issue.

  3. added
    - P4: important
    pkg: astro
    triage: fix pending
    and removed
    triage: in progress
    on Sep 28, 2026
  4. matthewp commented on Sep 28, 2026

    @matthewp
    Contributor

    Bot, I can confirm this fixes it.

  5. added
    triage: fix verified
    and removed
    triage: fix pending
    on Sep 28, 2026
  6. astro-factory commented on Sep 28, 2026

    @astro-factory
    Contributor

    The fix has been verified! A pull request has been created: #18163

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

    - P4: importantpkg: astrotriage: fix verified

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions