Verified Commit 7f0aa96a authored by Dave Long's avatar Dave Long
Browse files

refactor: #3571351 Only get the css/js query string when necessary

By: catch
(cherry picked from commit 3c07b176)
parent 2fcaa734
Loading
Loading
Loading
Loading
Loading
+4 −9
Original line number Diff line number Diff line
@@ -29,12 +29,6 @@ public function __construct(
  public function render(array $css_assets) {
    $elements = [];

    // A dummy query-string is added to filenames, to gain control over
    // browser-caching. The string changes on every update or full cache
    // flush, forcing browsers to load a new copy of the files, as the
    // URL changed.
    $query_string = $this->assetQueryString->get();

    // Defaults for LINK and STYLE elements.
    $link_element_defaults = [
      '#type' => 'html_tag',
@@ -52,11 +46,12 @@ public function render(array $css_assets) {
        // For file items, output a LINK tag for file CSS assets.
        case 'file':
          $element['#attributes']['href'] = $this->fileUrlGenerator->generateString($css_asset['data']);
          // Only add the cache-busting query string if this isn't an aggregate
          // file.
          // For unaggregated assets, add a query string to force edge/browser
          // cache invalidation. This query string is updated after each full
          // cache clear.
          if (!isset($css_asset['preprocessed'])) {
            $query_string_separator = str_contains($css_asset['data'], '?') ? '&' : '?';
            $element['#attributes']['href'] .= $query_string_separator . $query_string;
            $element['#attributes']['href'] .= $query_string_separator . $this->assetQueryString->get();
          }
          break;

+6 −11
Original line number Diff line number Diff line
@@ -40,13 +40,6 @@ public function __construct(
  public function render(array $js_assets) {
    $elements = [];

    // A dummy query-string is added to filenames, to gain control over
    // browser-caching. The string changes on every update or full cache
    // flush, forcing browsers to load a new copy of the files, as the
    // URL changed. Files that should not be cached get the request time as a
    // query-string instead, to enforce reload on every page request.
    $default_query_string = $this->assetQueryString->get();

    // Defaults for each SCRIPT element.
    $element_defaults = [
      '#type' => 'html_tag',
@@ -71,12 +64,14 @@ public function render(array $js_assets) {
          break;

        case 'file':
          $query_string = $js_asset['version'] == -1 ? $default_query_string : 'v=' . $js_asset['version'];
          $query_string_separator = str_contains($js_asset['data'], '?') ? '&' : '?';
          $element['#attributes']['src'] = $this->fileUrlGenerator->generateString($js_asset['data']);
          // Only add the cache-busting query string if this isn't an aggregate
          // file.
          if (!isset($js_asset['preprocessed'])) {
            // For unaggregated assets, add a either the library version or a
            // default query string to force edge/browser cache invalidation.
            // This query string is updated after each full cache clear or when
            // the library version changes.
            $query_string = $js_asset['version'] == -1 ? $this->assetQueryString->get() : 'v=' . $js_asset['version'];
            $query_string_separator = str_contains($js_asset['data'], '?') ? '&' : '?';
            $element['#attributes']['src'] .= $query_string_separator . ($js_asset['cache'] ? $query_string : $this->time->getRequestTime());
          }
          break;
+1 −1
Original line number Diff line number Diff line
@@ -127,7 +127,7 @@ protected function doTestNodePageAdministrator(): void {
    }, 'administratorNodePage');

    $expected = [
      'QueryCount' => 296,
      'QueryCount' => 295,
      'CacheGetCount' => 283,
      'CacheGetCountByBin' => [
        'config' => 66,
+1 −1
Original line number Diff line number Diff line
@@ -52,7 +52,7 @@ protected function testFrontPageColdCache(): void {
    $this->assertSession()->pageTextContains('Umami');

    $expected = [
      'QueryCount' => 205,
      'QueryCount' => 204,
      'CacheGetCount' => 249,
      'CacheSetCount' => 252,
      'CacheDeleteCount' => 0,
+1 −1
Original line number Diff line number Diff line
@@ -55,7 +55,7 @@ protected function testNodePageColdCache(): void {
    $this->assertSession()->pageTextContains('quiche');

    $expected = [
      'QueryCount' => 225,
      'QueryCount' => 224,
      'CacheGetCount' => 241,
      'CacheSetCount' => 238,
      'CacheDeleteCount' => 0,
Loading