Skip to content

Media queries are moved outside their cascade layers in the production CSS build #11567

@lobovskiy

Description

@lobovskiy

Have you read the Contributing Guidelines on issues?

Prerequisites

  • I'm using the latest version of Docusaurus.
  • I have tried the npm run clear or yarn clear command.
  • I have tried rm -rf node_modules yarn.lock package-lock.json and re-installing packages.
  • I have tried creating a repro with https://new.docusaurus.io.
  • I have read the console error message carefully (if applicable).

Description

I’m using cascade layers, which are enabled by default in v3.9.2 (future: { v4: true } in the config).
I noticed that in the production build, CSS media queries are extracted out of their cascade layers.
In dev mode, the styles look like this:

@layer docusaurus.theme-classic {
  /* some styles here */

  @media (min-width: 997px) {
    /* media query styles here */
  }
}

But in the built CSS bundle they become:

@layer docusaurus.theme-classic {
  /* some styles here */
}

@media (min-width: 997px) {
  /* media query styles here */
}

The media queries are no longer nested inside the cascade layer.
This also happens to imported third-party stylesheets, which breaks the intended cascade order.

Reproducible demo

No response

Steps to reproduce

  1. Create a new Docusaurus project: npx create-docusaurus@latest my-website classic
  2. Navigate into the project directory: cd my-website
  3. Start the development server: npm start
  4. Open the site in a browser and inspect the applied CSS in DevTools (Sources panel).
    Observe: media queries are correctly nested inside their corresponding cascade layers.
  5. Build the production bundle: npm run build
  6. Serve the built site: npm run serve
  7. Open the site again in a browser and inspect the built CSS in DevTools (Sources panel).
    Observe: all media queries have been extracted out of the cascade layers and appear at the root level of the stylesheet.

Expected behavior

Media queries should remain nested inside their respective cascade layers in the production CSS bundle, exactly as they appear during development. The cascade layer structure should be preserved for both local and third-party stylesheets.

Actual behavior

During the production build, all media queries are extracted out of their cascade layers and moved to the root level of the compiled CSS. As a result, the cascade layer ordering is broken, affecting both project styles and imported third-party stylesheets.

Your environment

  • Public source code: N/A (bug reproducible in a fresh project)
  • Public site URL: N/A
  • Docusaurus version used: 3.9.2 (with future: { v4: true } enabled by default)
  • Environment name and version (e.g. Chrome 89, Node.js 16.4): all browsers, Node.js 22.14.0
  • Operating system and version (e.g. Ubuntu 20.04.2 LTS): Windows 11, macOS 26.2

Self-service

  • I'd be willing to fix this bug myself.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugAn error in the Docusaurus core causing instability or issues with its execution

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions