class JsOptimizer in Advanced CSS/JS Aggregation 8.3
Same name and namespace in other branches
- 8.4 src/Asset/JsOptimizer.php \Drupal\advagg\Asset\JsOptimizer
The JavaScript Optimizer.
Hierarchy
- class \Drupal\advagg\Asset\AssetOptimizer
- class \Drupal\advagg\Asset\JsOptimizer
Expanded class hierarchy of JsOptimizer
1 string reference to 'JsOptimizer'
1 service uses JsOptimizer
File
- src/
Asset/ JsOptimizer.php, line 12
Namespace
Drupal\advagg\AssetView source
class JsOptimizer extends AssetOptimizer {
/**
* {@inheritdoc}
*/
public function __construct(ConfigFactoryInterface $config_factory, ContainerAwareEventDispatcher $event_dispatcher, CacheBackendInterface $cache) {
$this->extension = 'js';
parent::__construct($config_factory, $event_dispatcher, $cache);
}
/**
* {@inheritdoc}
*/
protected function addDnsPrefetch(array $asset) {
// Check if Google Ad Manager and add DNS prefetch.
$prefetch = $this
->testForGoogleAdManager($asset['data']);
// Check if Google Analytics and add DNS prefetch.
$prefetch += $this
->testForGoogleAnalytics($asset['data']);
return $prefetch;
}
/**
* {@inheritdoc}
*/
protected function fixType(array &$asset) {
// Default asset type to file if not set/invalid.
if (!in_array($asset['type'], [
'file',
'external',
'settings',
])) {
$asset['type'] = 'file';
}
$path = $asset['data'];
if ($asset['type'] === 'external') {
// If type is external but path doesn't start with http, https, or //
// change it to file.
if (stripos($path, 'http') !== 0 && stripos($path, '//') !== 0) {
$asset['type'] = 'file';
}
elseif (stripos($path, $this->basePath) !== FALSE && !$this->config
->get('js.preserve_external')) {
$asset['type'] = 'file';
$asset['group'] = JS_LIBRARY;
$asset['every_page'] = TRUE;
$asset['weight'] = -40000;
$asset['data'] = substr($asset['data'], stripos($asset['data'], $this->basePath) + $this->basePathLen);
}
}
elseif ($asset['type'] === 'file' && (stripos($path, 'http') === 0 || stripos($path, '//') === 0)) {
$asset['type'] = 'external';
}
}
/**
* Test if the provided path is from Google Ad Manager and add DNS entries.
*
* @param string $path
* The path to check.
*
* @return array
* Array of prefetch domains if file is from Google Ad Manager.
*/
private function testForGoogleAdManager($path) {
$prefetch = [];
if (strpos($path, '/google_service.') == FALSE) {
return $prefetch;
}
// Domains in the google_service.js file.
$prefetch[] = 'https://csi.gstatic.com';
$prefetch[] = 'https://pubads.g.doubleclick.net';
$prefetch[] = 'https://partner.googleadservices.com';
$prefetch[] = 'https://securepubads.g.doubleclick.net';
// Domains in the google_ads.js file.
$prefetch[] = 'https://pagead2.googlesyndication.com';
// Other domains that usually get hit.
$prefetch[] = 'https://cm.g.doubleclick.net';
$prefetch[] = 'https://tpc.googlesyndication.com';
return $prefetch;
}
/**
* Test if the provided path is from Google Analytics and add DNS entries.
*
* @param string $path
* The path to check.
*
* @return array
* Empty array or an array to prefetch if file is from Google Analytics.
*/
private function testForGoogleAnalytics($path) {
$prefetch = [];
if (strpos($path, 'GoogleAnalytics') == FALSE && strpos($path, 'google-analytics') == FALSE) {
return $prefetch;
}
$prefetch[] = 'https://ssl.google-analytics.com';
$prefetch[] = 'https://stats.g.doubleclick.net';
return $prefetch;
}
/**
* {@inheritdoc}
*/
protected function optimizeFile(array &$asset, array $data) {
$asset_event = new AssetOptimizationEvent($data['contents'], $asset, $data);
$this->eventDispatcher
->dispatch(AssetOptimizationEvent::JS, $asset_event);
$contents = $asset_event
->getContent();
$asset = $asset_event
->getAsset();
// If file contents are unaltered return FALSE.
if ($contents === $data['contents'] && !$this->gZip) {
return FALSE;
}
return $this
->writeFile($contents, $data['cid']);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AssetOptimizer:: |
protected | property | Whether or not to brotli compress assets. | |
AssetOptimizer:: |
protected | property | The AdvAgg cache. | |
AssetOptimizer:: |
protected | property | Config level of caching of assets. | |
AssetOptimizer:: |
protected | property | The cache time. | |
AssetOptimizer:: |
protected | property | A config object for the advagg configuration. | |
AssetOptimizer:: |
protected | property | Array of domains to prefetch. Copied to $GLOBALS for later use. | |
AssetOptimizer:: |
protected | property | Event Dispatcher service. | |
AssetOptimizer:: |
protected | property | Asset type (css or js). | |
AssetOptimizer:: |
protected | property | Config to control fixing the asset type (file, external). | 2 |
AssetOptimizer:: |
protected | property | Whether or not to gzip assets. | |
AssetOptimizer:: |
protected | function | Convert http:// to https://. | |
AssetOptimizer:: |
protected | function | Converts absolute paths to be protocol relative paths. | |
AssetOptimizer:: |
public static | function | Generate an htaccess file in the optimized asset dirs to improve serving. | |
AssetOptimizer:: |
protected | function | Get how long to cache an asset. Varies on cache level setting. | |
AssetOptimizer:: |
public | function | Process a core asset array. | |
AssetOptimizer:: |
protected | function | Given a filename calculate various hashes, gather meta data then optimize. | |
AssetOptimizer:: |
protected | function | Determine if settings and available PHP modules allow brotli-ing assets. | |
AssetOptimizer:: |
protected | function | Determine if settings and available PHP modules allow GZipping assets. | |
AssetOptimizer:: |
public static | function | Stable sort for CSS and JS items. | |
AssetOptimizer:: |
protected | function | The filename for the CSS or JS optimized file is the cid. | |
JsOptimizer:: |
protected | function |
Extract any domains to prefetch DNS. Overrides AssetOptimizer:: |
|
JsOptimizer:: |
protected | function |
Checks for and if found fixes incorrectly set asset types. Overrides AssetOptimizer:: |
|
JsOptimizer:: |
protected | function |
Perform any in-place optimization & pass to event for further optimization. Overrides AssetOptimizer:: |
|
JsOptimizer:: |
private | function | Test if the provided path is from Google Ad Manager and add DNS entries. | |
JsOptimizer:: |
private | function | Test if the provided path is from Google Analytics and add DNS entries. | |
JsOptimizer:: |
public | function |
Constructs the Optimizer object. Overrides AssetOptimizer:: |