- Issue created by @eduardo morales alberti
- Status changed to Needs work
10 months ago 3:20pm 23 January 2024 - π·πΊRussia niklan Russia, Perm
I'm facing the same issue. I don't understand the current logic. If the library doesn't want to be preprocessed, why is it automatically minified by default? In that case it makes no sense in
preprocess: false
.I believe the issue lies in the logic implemented in two methods:
\Drupal\Core\Asset\JsCollectionOptimizerLazy::optimizeGroup
and\Drupal\Core\Asset\JsCollectionOptimizer::optimize
.The
\Drupal\Core\Asset\JsCollectionOptimizer::optimize
respects asset grouppreprocess
status:if (!$js_group['preprocess']) { $uri = $js_group['items'][0]['data']; $js_assets[$order]['data'] = $uri; }
The
\Drupal\Core\Asset\JsCollectionOptimizerLazy::optimizeGroup
function optimizes the group regardless of any conditions. It doesn't have a condition and simply moves on to the optimization process.This method should looks like:
public function optimizeGroup(array $group): string { $data = ''; $current_license = FALSE; // No preprocessing, single JS asset: just use the existing URI. if ($group['type'] === 'file' && !$group['preprocess']) { $data = file_get_contents($group['items'][0]['data']); } else { foreach ($group['items'] as $js_asset) { // Ensure license information is available as a comment after // optimization. if ($js_asset['license'] !== $current_license) { $data .= "/* @license " . $js_asset['license']['name'] . " " . $js_asset['license']['url'] . " */\n"; } $current_license = $js_asset['license']; // 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); } // Append a ';' and a newline after each JS file to prevent them from // running together. $data .= ";\n"; } } // Remove unwanted JS code that causes issues. return $this->optimizer->clean($data); }->optimizer->clean($data); }
- Status changed to Needs review
5 months ago 10:25am 6 June 2024 - π·πΊRussia niklan Russia, Perm
I created the MR with the fix and it works with the google_tag module. However, I'm still not sure about this part of the code:
$data = file_get_contents($group['items'][0]['data']);
. The same code occurs in the methodDrupal\Core\Asset\JsCollectionOptimizer::optimize
. It seems that there may be more than one 'item' in the group, and in such a case, some content may be missing. Need some advice here is that safe or not. - π·πΊRussia niklan Russia, Perm
It also fails when using libraries with external dependencies, but with a slightly different exception.
Only file JavaScript assets can be optimized.
E.g. of such library:
example: version: 1 remote: https://example.com license: name: Example url: https://example.com/license gpl-compatible: true js: js/init.js: { } //example.com/script.js: { preprocess: false }
The suggested solution won't work in this case because the type here is external, which is handled in the method
\Drupal\Core\Asset\JsCollectionOptimizer::optimize
, but not in the method\Drupal\Core\Asset\JsCollectionOptimizerLazy::optimizeGroup
. But for now, Iβm out of ideas on how to solve this issue for external libraries. The result of the lazy method should be optimized JavaScript.For those who have encountered this problem, I suggest explicitly setting the following in the code:
{preprocess: false, minified: true}
for such JavaScript files.It's also worth mentioning that the solution from MR and
Drupal\Core\Asset\JsCollectionOptimizer::optimize
does not include injecting library license information. - Status changed to Needs work
5 months ago 2:11pm 12 June 2024 - πΊπΈUnited States smustgrave
Can the issue summary be updated to include any proposed solution and other relevant sections from the issue template.
Previously was tagged for tests may be good to get those written and may help guide the fix.
- π©πͺGermany Anybody Porta Westfalica
I can confirm this message keeps on filling our logs on larger projects.
- πΊπΈUnited States jackfoust
It appears this is also affecting the Stripe module.
- πΊπΈUnited States firewaller
We're seeing this too. Its odd that it would skip "minified" but not "preprocess = false" here if the optimize function is just going to throw an exception for "preprocess = false": https://git.drupalcode.org/project/drupal/-/blob/11.x/core/lib/Drupal/Co...