"Only file JavaScript/CSS assets can be optimized" errors in logs

Created on 18 July 2025, 2 months ago

Problem/Motivation

We started seeing a lot of errors in logs after a deployment which contained only the drupal core update (10.4.7 -> 11.2.2). The error is as follows:

Exception: Only file JavaScript assets can be optimized. in Drupal\Core\Asset\JsOptimizer->optimize() (line 31 of core/lib/Drupal/Core/Asset/JsOptimizer.php).
Drupal\Core\Asset\JsCollectionOptimizerLazy->optimizeGroup() (Line: 192)
Drupal\system\Controller\AssetControllerBase->deliver()
call_user_func_array() (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 622)
Drupal\Core\Render\Renderer->executeInRenderContext() (Line: 121)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext() (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 183)
Symfony\Component\HttpKernel\HttpKernel->handleRaw() (Line: 76)
Symfony\Component\HttpKernel\HttpKernel->handle() (Line: 53)
Drupal\Core\StackMiddleware\Session->handle() (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle() (Line: 28)
Drupal\Core\StackMiddleware\ContentLength->handle() (Line: 32)
Drupal\big_pipe\StackMiddleware\ContentLength->handle() (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle() (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle() (Line: 53)
Drupal\Core\StackMiddleware\AjaxPageState->handle() (Line: 51)
Drupal\Core\StackMiddleware\StackedHttpKernel->handle() (Line: 715)
Drupal\Core\DrupalKernel->handle() (Line: 19)

It seems this is the javascript equivalent of what was reported in πŸ› Website error Exception: "Only file CSS assets can be optimized" Active for CSS.
These errors seem to come from the fact that Drupal is getting requests from old aggregates (possibly browser cache or other means?).
I noticed that the delta query parameter especially defines which group of assets the controller will be attempt to deliver and optimize in \Drupal\system\Controller\AssetControllerBase::deliver.
In my case, after deploy, these requests contain a delta that corresponds to a type: external group which is then passed to \Drupal\Core\Asset\JsOptimizer::optimize, which eventually throws the exception and fills the logs.

Steps to reproduce

TODO

Proposed resolution

Make \Drupal\system\Controller\AssetControllerBase::deliver more resilient to such stale asset requests. We can check if $group['type'] !== 'file' || $group['preprocess'] === FALSE and return a 400 error (bad request). This essentially ensures the delta query param is valid, like other params are checked ATM.

This issue also relates to πŸ› Assets paths in CSS no longer rewritten when aggregation is enabled Active which is a consequence of the change in #3414173. See [3414173-37] (comments 37-39). So I propose to solve that here as well as it touches the same parts.
I propose to revert the code introduced in #3414173 which would still maintain its original goal.
And that would also cover πŸ› Assets paths in CSS no longer rewritten when aggregation is enabled Active

Remaining tasks

TODO

User interface changes

None

Introduced terminology

None

API changes

None

Data model changes

None

Release notes snippet

None

πŸ› Bug report
Status

Active

Version

11.0 πŸ”₯

Component

asset library system

Created by

πŸ‡§πŸ‡ͺBelgium herved

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024