Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
The polyfill added in #1815602: Introduce a polyfill for matchMedia is no longer needed by any of the browsers supported by Drupal core.
See: https://caniuse.com/#feat=matchmedia
Proposed resolution
Mark the asset library deprecated in core.libraries.yml.
Remaining tasks
- Write patch
- Test against supported browsers to ensure no change in behavior
User interface changes
API changes
Data model changes
Release notes snippet
The core/matchmedia and core/matchmedia.addListener asset libraries are marked deprecated, to be removed in Drupal 9.
A contrib module was created to provide these libraries for sites which still need to support older browsers.
Comment | File | Size | Author |
---|---|---|---|
#17 | 3086369-17.patch | 1.95 KB | lauriii |
#8 | interdiff.txt | 1.68 KB | zrpnr |
#5 | interdiff_2-5.txt | 2.64 KB | zrpnr |
#5 | 3086369-5.patch | 1.93 KB | zrpnr |
#2 | 3086369-2.patch | 2.79 KB | zrpnr |
Comments
Comment #2
zrpnrThis can be removed from libraries files and leave all the JavaScript unchanged, since the polyfill adds matchMedia to the window object.
Removed dependency on core/matchmedia and core/matchmedia.addEventListener from:
Checked IE using a responsive image so that picturefill would be loaded, and with resizing the toolbar to make sure the events fired.
Added the draft change record for the deprecation message.
Comment #4
zrpnrI talked with @xjm and @lauriii and they suggested adding a contrib module to replace this library, I added https://www.drupal.org/project/matchmedia
This is identical to the core library except I added
header: true
just to ensure it would load at the top of the page so any other files would come back to it. It attaches the libraries while the module is active vs current core which has selective dependencies. I thought the simpler approach worked in the contrib since if it's necessary it's better to have it on all the time, but I'm happy to make changes if that doesn't seem right to any reviewers.Comment #5
zrpnrJust like in #3086379: Deprecate html5shiv and remove usages in core this patch is more conservative and just adds the deprecation mark to
matchmedia
andmatchmedia.addListener
.These asset libraries are not used directly in tests however they are attached by
vertical-tabs
,toolbar
and others which do have tests, so I suppressed the deprecation warnings.Comment #6
Wim LeersThis patch looks great :)
But I am concerned about https://git.drupalcode.org/project/matchmedia/blob/8.x-1.x/matchmedia.mo.... It should not be attaching the
matchmedia/matchmedia
on all pages.(Because we try to be thoughtful about front-end performance and load things only when they’re necessary. That was one of the big perf improvements in Drupal 8 core compared to 7 actually 🙂Zero JS for anon users instead of lots of unused and hence useless JS.)
I have two remarks about https://git.drupalcode.org/project/matchmedia/blob/8.x-1.x/matchmedia.mo...
hook_library_info_alter()
is currently deleting other "copies" ofmatchmedia
. I think that should specifically only deletecore/matchmedia
+core/matchmedia.addListener
, not do it for any extension.hook_library_info_alter()
could scan all library definitions and make it swapcore/matchmedia
formatchmedia/matchmedia
🙂 I'm not sure we truly want that, but it's definitely possible. Because today in Drupal 8,matchMedia.js
is also only loaded if an asset library declares a dependency oncore/matchmedia
, and this would then make it automatically switch tomatchmedia/matchmedia
. So that'd automatically also address the "load on all pages" concern.And one remark about https://git.drupalcode.org/project/matchmedia/blob/8.x-1.x/matchmedia.li...
Looks like #4 introduced this. That is only necessary when you don't use the asset library system as intended: the asset library system in Drupal 8 specifically gained a dependency system, to ensure JS files are loaded in the right order. By stopping the loading on all pages and only loading it when it's a dependency, we can also remove that 🥳
Comment #7
zrpnrThanks for looking this over @Wim Leers!
#6.1 I kept the part that unsets the library but changed the check for the extension core only.
#6.2 This makes sense, I think the changes I made to the module address this - scanning over a library dependencies to replace
core/matchmedia
orcore/matchmedia.addListener
.Updated link to the previous commit matchmedia.module
Current commit matchmedia.module
Also removed the
header: true
which I added to brute-force put this ahead of other libraries.Comment #8
zrpnrI realized I could post an interdiff instead of links to gitlab.
Comment #9
lauriiiThis library should be always loaded as a dependency because it doesn't do anything by itself. For that reason I think the solution in #7 is correct 👍.
It seems like we're referencing to a non-existing library in the libraries.yml file: https://git.drupalcode.org/project/matchmedia/blob/8.x-1.x/matchmedia.li.... The library on the dependency should be
matchmedia/matchmedia
.Also adding a tag for a release manager review since @xjm mentioned that this should be reviewed by a release manager.
Comment #10
zrpnrFixed the incorrect camelCasing.
https://git.drupalcode.org/project/matchmedia/commit/000c28a784e98967f07...
Comment #11
zrpnrComment #12
zrpnrComment #13
lauriiiThe folder name is camelCased so that change should be reverted from https://git.drupalcode.org/project/matchmedia/commit/000c28a784e98967f07....
Comment #14
zrpnrI changed case in the wrong place! Thanks for catching that, I reverted and fixed the dependency.
https://git.drupalcode.org/project/matchmedia/commit/e42cb046d49bf656c66...
Comment #15
lauriiiThe patch and the contrib project both look good! Confirmed that the deprecation warning message points to the right change record and tested manually that when I visit page with vertical tabs, the core matchmedia library has been replaced with one from the contrib project. 👏
Comment #16
xjmI went to commit this but it does not apply, probably because it's modifying the same lines at the end of the trait that
html5shiv
did. Feel free to set it directly back to RTBC after rerolling.Comment #17
lauriii👨🎨
Comment #21
xjmCommitted and pushed to 9.0.x, 8.9.x, and 8.8.x, and published the change record. Thanks!