[regression] Since #1014086 generated CSS assets have absolute URLs without varying by url.site cache context

Created on 14 April 2023, over 1 year ago
Updated 29 April 2023, over 1 year ago

Problem/Motivation

\Drupal\Core\Asset\AssetResolver::getCssAssets() computes the CSS assets to serve for the given AttachedAssetsInterface $assets, $optimize, LanguageInterface $language = NULL. The cache ID it uses is

$cid = 'css:' . $theme_info->getName() . ':' . $language->getId() . Crypt::hashBase64(serialize($libraries_to_load)) . (int) $optimize;

Its logic does

    if ($optimize) {
      $css = \Drupal::service('asset.css.collection_optimizer')->optimize($css, $libraries_to_load, $language);
    }
    $this->cache->set($cid, $css, CacheBackendInterface::CACHE_PERMANENT, ['library_info']);

That definitely also does not vary by url.site cache context.

Yet in case $optimize === TRUE, it generates absolute file URLs. This means it will serve the CSS assets for every site in a single-site-with-multiple-domains situation from whichever domain generated this CSS aggregate first!

The root cause: the above results in a call to \Drupal\Core\Asset\CssCollectionOptimizerLazy::optimize() (introduced by 🐛 Stampedes and cold cache performance issues with css/js aggregation Fixed ) which does:

        $css_assets[$order]['data'] = $this->fileUrlGenerator->generateAbsoluteString($uri) . '?' . UrlHelper::buildQuery($query);

and indeed generates absolute URLs:

This is the regression.

I discovered it while updating the CDN contrib module for Drupal 10.1 in 📌 10.1.x compatibility: tests are failing against Drupal 10.1.x due to upstream changes Fixed , and for a reason I could not figure out, the CDN module wouldn't rewrite CSS aggregates to be served from a CDN … because Drupal core stopped generating root-relative asset URLs since 🐛 Stampedes and cold cache performance issues with css/js aggregation Fixed due to the above bug:

Steps to reproduce

Proposed resolution

Generate relative URLs.

Remaining tasks

User interface changes

None.

API changes

None.

Data model changes

None.

Release notes snippet

N/A

🐛 Bug report
Status

Fixed

Version

10.1

Component
Asset library 

Last updated 2 days ago

No maintainer
Created by

🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺

Live updates comments and jobs are added and updated live.
  • Regression

    It restores functionality that was present in earlier versions.

  • Contributed project blocker

    It denotes an issue that prevents porting of a contributed project to the stable version of Drupal due to missing APIs, regressions, and so on.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024