Skip to content

CSS shows up twice when Critical CSS Enabled #26498

Closed
@brightonmike

Description

@brightonmike

Preconditions (*)

  1. 2.3.4

Steps to reproduce (*)

  1. Enable use_css_critical_path

Expected result (*)

  1. The stylesheets are only loaded once

Actual result (*)

  1. styles-m.css, styles-l.css and print.css styles show up multiple times in developer tools.

I have traced the problem to the following method:
beforeSendResponse in module-theme/Controller/Result/AsyncCssPlugin.php

This line appears to be re-inserting the original style tags:

$content = str_replace('</body', implode("\n", $cssMatches) . "\n</body", $content);

If you comment out this line, the HTML is as expected, with each style element existing only once and preventing double download of stylesheets.

I think there's some confusion over the implementation of the following technique:

<link rel="preload" as="style" media="all" onload="this.onload=null;this.rel='stylesheet'"href="...en_US/css/styles-m.css">

I would expect that the purpose of the onload method is that rather than needing one <link> to preload and a separate <link> at the bottom of the HTML to then achieve the affect of asynchronously loading the CSS, this behaviour can be achieved with just one <link> element. After onload event has fired, the rel attribute swaps and applies the CSS.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Component: ThemeFixed in 2.4.xThe issue has been fixed in 2.4-develop branchIssue: Clear DescriptionGate 2 Passed. Manual verification of the issue description passedIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedIssue: Format is validGate 1 Passed. Automatic verification of issue format passedIssue: Ready for WorkGate 4. Acknowledged. Issue is added to backlog and ready for developmentPriority: P3May be fixed according to the position in the backlog.Progress: doneReported on 2.3.4Indicates original Magento version for the Issue report.Reproduced on 2.4.xThe issue has been reproduced on latest 2.4-develop branchSeverity: S2Major restrictions or short-term circumventions are required until a fix is available.Triage: Dev.ExperienceIssue related to Developer Experience and needs help with Triage to Confirm or Reject it

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions