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

Created on 18 July 2025, about 1 month 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

  • Issue created by @herved
  • @herved opened merge request.
  • πŸ‡§πŸ‡ͺBelgium herved

    Opened MR
    For context, I suspect we may have an infrastructure issue somewhere, possibly the reverse proxy serving stale pages.
    Still, if that is the case, drupal should not fill the logs with errors. The changes here will log BadRequestHttpException warnings just like other invalid asset requests.

  • πŸ‡§πŸ‡ͺBelgium herved

    Tests and steps to reproduce still todo.
    But any feedback is welcome.

  • πŸ‡§πŸ‡ͺBelgium herved

    Static patch for composer.

  • πŸ‡¨πŸ‡¦Canada joelpittet Vancouver

    @herved Thanks so much for jumping on this β€” I’m seeing the same thing in my logs, so really appreciate you taking this on!

    RE The mention in the proposed solution

    This would also cover πŸ› Assets paths in CSS no longer rewritten when aggregation is enabled Active which is a consequence of the change in #3414173.

    I wonder if it might be helpful to keep the scope of that one in its own issue, just to keep things a bit more focused? I realize they are related (because this JS one is where I copied from in πŸ› Website error Exception: "Only file CSS assets can be optimized" Active ) but painting them with the same brush is what brought us here in the first place.

  • πŸ‡§πŸ‡ͺBelgium herved

    Hi @joelpittet, I know I combined 2 issues into 1 here, which may not be ideal, but they are tighly coupled.
    In my understanding, considering this issue here, and the changes I propose, external files would and should never enter the ::optimizeGroup method. So does it really make sense to check for minified === TRUE && type === external in ::optimizeGroup as you proposed there? Meaning the condition is therefore not needed.

  • πŸ‡¨πŸ‡¦Canada joelpittet Vancouver

    Thanks, @herved β€” you might be right here, especially since you probably have a better handle on the internals of ::optimizeGroup() than I do.

    If you haven’t already, feel free to take a look at the test in πŸ› Assets paths in CSS no longer rewritten when aggregation is enabled Active β€” it might be helpful to reuse or adapt parts of it to confirm whether ::optimizeGroup() is actually called in these cases. Even just running the test on its own could help show the problem is fixed. That kind of check might make the expected behavior a bit clearer.

    I’m still a bit cautious about collapsing the two issues, since that overlap is partly what got us into this situation. But if you feel strongly that they belong together, I won’t block it. I do think the targeted fix in πŸ› Assets paths in CSS no longer rewritten when aggregation is enabled Active should resolve the CSS side of things.

    Really appreciate the work you’re doing here β€” you might end up solving a broader problem in the process! I’m just laser-focused on cleaning up the regression I introduced, and if I expand the scope too much, I’m afraid I’ll cause another one 😬

Production build 0.71.5 2024