Skip to content

Moving @import statements breaks CSS rule order #60

Closed

Description

@brandonpayton

When we are concatenating CSS files A, B, and C, the following code can break CSS rule order by importing the dependencies of B ahead of the dependencies of A and by importing the dependencies of C ahead of the dependencies of A and B.

page-optimize/service.php

Lines 222 to 241 in 86fdbba

// Move the @import rules on top of the concatenated output.
// Only @charset rule are allowed before them.
if ( false !== strpos( $buf, '@import' ) ) {
$buf = preg_replace_callback(
'/(?P<pre_path>@import\s+(?:url\s*\()?[\'"\s]*)(?P<path>[^\'"\s](?:https?:\/\/.+\/?)?.+?)(?P<post_path>[\'"\s\)]*;)/i',
function ( $match ) use ( $dirpath ) {
global $pre_output;
if ( 0 !== strpos( $match['path'], 'http' ) && '/' != $match['path'][0] ) {
$pre_output .= $match['pre_path'] . ( $dirpath == '/' ? '/' : $dirpath . '/' ) .
$match['path'] . $match['post_path'] . "\n";
} else {
$pre_output .= $match[0] . "\n";
}
return '';
},
$buf
);
}

This breaks the expected CSS cascade by changing rule order. When there is a conflict, latter rules of equal specificity win, so by changing the rule order, we change which rule wins.

I see a couple of options here. They are not mutually exclusive.

  1. Only allow the first file in a concat group to have @import rules
  2. If all @import rules reference files on the current site, remove the import rules and inline the contents of the referenced files. If the imports reference external resources, do not inline, and if it is not the first file in the concat group, start a new concat group with this file.

Activity

  1. brandonpayton commented on Jul 23, 2021

    @brandonpayton
    MemberAuthor

    This is a tough one to tackle because we only know about @import statements when generating concatenated style output. Maybe we can update the concat service to do one of the following:

    1. Use @import statements that refer back to the concat service to combine concatenated CSS and @import statements in a way that preserves CSS load order.
    2. Inline things referred to by @import

    Number 2 probably should not be done because it is possible servers may provide dynamic content depending on the request.

    Number 1 is doable, but it will most likely increase resource load time.

  2. removed their assignment
    on Feb 3, 2026
  3. mreishus commented on Feb 5, 2026

    @mreishus
    Contributor

    This should be fixed in #99 (test) and #100 (fix). I went with the simple fix of breaking concat boundaries on stylesheets containing @import. They can only be the first sheet in a concat group, and it will make a new group if needed.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions