Same name in other branches
  1. 5.0.x advagg_mod/src/Asset/DeferJs.php \Drupal\advagg_mod\Asset\DeferJs::defer()
  2. 6.0.x advagg_mod/src/Asset/DeferJs.php \Drupal\advagg_mod\Asset\DeferJs::defer()
  3. 8.x-3.x 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);
}