Problem/Motivation

The media-library.ui.js library in the default_admin theme is being added to the page before jQuery, one of its dependencies. Screenshot when JS is not aggregated:
media-library-ui.js first

This is showing up as a browser console error.
jQuery is not defined

When JS is set to be aggregated, the browser console error still appears.

This is occurring because the weight for media-library.ui.js is set to -1 in default_admin.libraries.yml. This weight can be safely removed, and it should be in preparation for #3467488: Deprecate support for per-file weight in libraries API.

In addition, there are two other libraries (default_admin/init, default_admin/scroll_sync) with weights that should be removed. Removing those can be done here as well.

Steps to reproduce

  1. Install Drupal with standard profile
  2. Install Media Library (and Media) modules
  3. Add a new content type
  4. Add a media field to the content type
  5. Create a node of the new type and observe the JS error in the browser console

Proposed resolution

Remove weights from JS file library definitions in the admin theme.

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Issue fork drupal-3634443

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

godotislate created an issue. See original summary.

godotislate’s picture

Issue summary: View changes

godotislate’s picture

Status: Active » Needs review

Removed the -1 weight from default_admin media-library.ui.js, and this makes the JS console error go away in manual testing. I added a kernel test to confirm that media-library.ui.js is added to the page after its dependencies. This test does not explicitly cover the JS console error, nor the behavior when JS files are aggregated. I think this is OK, especially since it means we don't have to add another FJ test to CI run time, but I'm amenable to making a change if wanted.

The weight and library actually were added originally to claro in #3023311: Modal dialog style update, and I don't see any explanation why the weight was set. Then subsequently in #3556948: Merging Gin as Admin theme, the library was copied over from claro to the admin theme.

I'm haven't looked into whether there's a more general bug to be addressed if setting a negative weight on a JS library causes it to be added before its dependencies. Explicit weights were removed from most of core in #3568157: Remove explicit weight from CSS files in core.libraries.yml, and so I think it's correct to do here as well. This change also reduces the number of aggregates in the Umami performance tests.

catch’s picture

I'm haven't looked into whether there's a more general bug to be addressed if setting a negative weight on a JS library causes it to be added before its dependencies.

I think this was the original behaviour too (didn't 100% confirm but pretty sure), except we've removed a lot of weights from core libraries, so dependencies that previously also had negative weights now don't.
catch’s picture

godotislate’s picture

@berdir asked on Slack whether this ticket could be expanded to remove the all the remaining weights for JS files in default_admin.libraries.yml.

Aside from media-library.ui.js, the relevant libraries are:

init:
  header: true
  js:
    migration/js/init.js: { minified: false, preprocess: false, weight: -99 }
  dependencies:
    - core/drupalSettings

and

scroll_sync:
  js:
    migration/js/scroll_sync.js: { minified: true, weight: -1 }

tl;dir: Updated the MR to include removing those weights to see how the tests go.

For init.js, the weight is set to -99 so that it is added to the page as early as possible (to set dark mode, etc.). I'm not finding any other core libraries that have header: true, other than drupal.touchevents-test, though, so I think it should be fine to remove the weight. If contrib or custom libraries also have header: true JS libraries that block init.js loading ASAP, the either the affected site can add a custom library_info_alter hook to set the other libraries either to run after default_admin/init or set to load async. Or issues can be filed in the respective contrib modules to update their library definitions as such.

The core/drupalSettings dependency for default_admin/init is a bit strange though, because there is no code in init.js that uses drupalSettings. The dependency was introduced in #3323066: Correctly declare library dependencies to core/drupalSettings, but it's not clear from there why the dependency was added to init specifically. We can probably remove that I think, either here or in a follow up.

For scroll_sync.js, this is used only by default_admin/tableheader, so removing the weight should probably be fine. One note is that it was added to Gin in #3293369: Table header overflow issue for multiple user role on permissions page., and it appears to a third-party library, but there's no documentation of where the library came from. We may want to open a follow up for that, because the JS is minified and otherwise can't be maintained.

godotislate’s picture

Title: media-library.ui.js added to page before jQuery » Remove weights from Admin theme JS library files
Issue summary: View changes

The tests, pass so retitled the issue and updated IS.

smustgrave’s picture

Does this not need to use the before/after feature now?

godotislate’s picture

No, the dependency chain is enough on its own.

smustgrave’s picture

This reproducible all the time? I tried following the steps but no error.

godotislate’s picture

100% reproducible for me.

smustgrave’s picture

Weird I had to fully delete and restart my ddev but yup I'm seeing the error.

What about system.admin and admin.jquery.ui

berdir’s picture

Status: Needs review » Reviewed & tested by the community

Those are css files, lets limit this to JS. Not sure why #3619512: Remove custom weights from default_admin.libraries.yml didn't remove them, maybe there we added/moved after that?

Not a JS expert, but I think this makes sense. Thought about "before" too, if it weren't a header library I think it would make sense to have a before core/drupal so we make sure it's before everything else, but given that header JS is rare I agree this is enough.

  • catch committed 1415c7dd on 12.0.x
    fix: #3634443 Remove weights from Admin theme JS library files
    
    By:...

  • catch committed eb7b8671 on main
    fix: #3634443 Remove weights from Admin theme JS library files
    
    By:...

  • catch committed 00871bed on 11.5.x
    fix: #3634443 Remove weights from Admin theme JS library files
    
    By:...

  • catch committed f1668a3b on 11.x
    fix: #3634443 Remove weights from Admin theme JS library files
    
    By:...
catch’s picture

Version: main » 11.5.x-dev
Status: Reviewed & tested by the community » Fixed

Committed/pushed to main and 12.0.x, then I resolved the merge conflict for 11.x in a new MR and cherry-picked to 11.5.x, thanks!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

catch’s picture

godotislate’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.