The Chosen library is currently being attached to every page (via hook_page_attachments()), even though most pages don't even have select elements on them. This causes unnecessary downloads for clients, which contributes to a site feeling slow for end users.

Drupal 8 does a good job of correctly bubbling attachments, so worst case you can just attach to each select element. (If not for the "whatever jQuery selector you want" feature, you could even narrow that down further so that you were only attaching to the elements that were actually using Chosen.)

CommentFileSizeAuthor
#4 2693649-04.patch3.73 KBjhedstrom
#2 2693649-02.patch3.75 KBjhedstrom

Comments

kevin.dutra created an issue. See original summary.

jhedstrom’s picture

Status: Active » Needs review
StatusFileSize
new3.75 KB

This patch depends on #2696483: Remove redundant 'chosen_' prefix to all settings, so won't apply automatically or won't pass tests until that is committed.

Because of the option to use chosen globally (which is typically how I've used this module in the past), the library must be added for every select element. Because of the ability to attach libraries and settings to an element in D8 though, we can as suggested above, remove the global hook_page_attachments(), and only attach the library on pages that have a select element.

Status: Needs review » Needs work

The last submitted patch, 2: 2693649-02.patch, failed testing.

jhedstrom’s picture

Status: Needs work » Needs review
StatusFileSize
new3.73 KB

New patch since the previous one had missed the change to chosen_disabled_themes.

Status: Needs review » Needs work

The last submitted patch, 4: 2693649-04.patch, failed testing.

nagy.balint’s picture

Status: Needs work » Needs review
nagy.balint’s picture

Cant we use some static cache, and if this code already ran once we don't need to run it again?

Because it will just add the same attachment and the same drupalSettings anyways.

Otherwise the patch seems to be fine.

jhedstrom’s picture

Cant we use some static cache, and if this code already ran once we don't need to run it again?

We could, but the core AttachedAssets service ensures a library isn't added more than once.

jhedstrom’s picture

And actually, now that I'm thinking about it more, using a static could be dangerous here since render arrays may be cached, and if we don't attach the library to each instance that needs it, there could be a situation where the library doesn't properly get loaded.

ademarco’s picture

Status: Needs review » Reviewed & tested by the community

Patch #4 worked very well for me, thanks! Marking as RTBC.

  • nagy.balint committed 6565968 on 8.x-2.x authored by jhedstrom
    Issue #2693649 by jhedstrom, kevin.dutra: Chosen library is included in...
nagy.balint’s picture

Status: Reviewed & tested by the community » Fixed

Thanks!

Status: Fixed » Closed (fixed)

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