Problem/Motivation
An error is thrown, when a library is defined using "preprocess: false, but ommiting "minified":
example:
version: 1
remote: https://example.com
license:
name: Example
url: https://example.com/license
gpl-compatible: true
js:
js/script.js: { preprocess: false }
Error:
The website encountered an unexpected error. Please try again later.
Exception: Only file JavaScript assets with preprocessing enabled can be optimized. in Drupal\Core\Asset\JsOptimizer->optimize() (line 34 of core/lib/Drupal/Core/Asset/JsOptimizer.php).
Drupal\Core\Asset\JsCollectionOptimizerLazy->optimizeGroup() (Line: 183)
Drupal\system\Controller\AssetControllerBase->deliver()
call_user_func_array() (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 592)
Drupal\Core\Render\Renderer->executeInRenderContext() (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext() (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 181)
Symfony\Component\HttpKernel\HttpKernel->handleRaw() (Line: 76)
Symfony\Component\HttpKernel\HttpKernel->handle() (Line: 58)
Drupal\Core\StackMiddleware\Session->handle() (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle() (Line: 53)
Asm89\Stack\Cors->handle() (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle() (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle() (Line: 36)
Drupal\Core\StackMiddleware\AjaxPageState->handle() (Line: 55)
Drupal\http_headers_cleaner\Middleware\HttpHeadersCleanerMiddleware->handle() (Line: 51)
Drupal\Core\StackMiddleware\StackedHttpKernel->handle() (Line: 704)
Drupal\Core\DrupalKernel->handle() (Line: 19)
This means, that the core forces the "minified" key for libraries not being preprocessed.
Further Information:
The problem seems to be
If the asset is not minified then it calls to optimize.
\Drupal\Core\Asset\JsCollectionOptimizer::optimize
// Optimize this JS file, but only if it's not yet minified.
if (isset($js_asset['minified']) && $js_asset['minified']) {
$data .= file_get_contents($js_asset['data']);
}
else {
$data .= $this->optimizer->optimize($js_asset);
}
But if the preprocess is set to false, it throws an error, so the optimizers force the asset to be minified, and it gives a 500 Error when tries to load the asset.
\Drupal\Core\Asset\JsOptimizer::optimize
if (!$js_asset['preprocess']) {
throw new \Exception('Only file JavaScript assets with preprocessing enabled can be optimized.');
}
Example from google_tag.libraries.yml
gtag:
js:
js/gtag.js: { preprocess: false }
drupalSettings:
gtag:
tagId: null
otherIds: []
events: []
dependencies:
- core/drupalSettings
Steps to reproduce
- Install and configure the Google Tag Module
- Visit different pages to Trigger Google Tag
- Look at the recent log messages and see the error
Proposed resolution
Check if the js asset group should be optimized at all, before optimizing it.