You are here

public function DeferJs::defer in Advanced CSS/JS Aggregation 8.4

Same name and namespace in other branches
  1. 8.3 advagg_mod/src/Asset/DeferJs.php \Drupal\advagg_mod\Asset\DeferJs::defer()

Add defer attribute to script tags.

Parameters

string $content: The response content.

Return value

string Updated content.

File

advagg_mod/src/Asset/DeferJs.php, line 71

Class

DeferJs
Add defer tag to scripts.

Namespace

Drupal\advagg_mod\Asset

Code

public function defer($content) {

  // Admin Toolbar 8x fails when deferred.
  foreach ($this->skipList as $cid) {
    if (strstr($content, $cid)) {
      return $content;
    }
  }

  // Only defer local scripts.
  if ($this->deferType === 2) {
    $pattern = '/<script src="\\/[a-zA-Z0-0].*"/';
  }
  else {
    $pattern = '/<script src=".*"/';
  }
  return preg_replace_callback($pattern, [
    $this,
    'callback',
  ], $content);
}