-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
useMatchMedia: cache queries #61000
useMatchMedia: cache queries #61000
Conversation
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Size Change: +78 B (0%) Total Size: 1.74 MB
ℹ️ View Unchanged
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good but there are unit test problems with mocking (mock-match-media
package)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps we could clean the cache before every test?
diff --git a/packages/compose/src/hooks/use-media-query/index.js b/packages/compose/src/hooks/use-media-query/index.js
index 598ab006ecb..a9f759a58de 100644
--- a/packages/compose/src/hooks/use-media-query/index.js
+++ b/packages/compose/src/hooks/use-media-query/index.js
@@ -3,7 +3,7 @@
*/
import { useMemo, useSyncExternalStore } from '@wordpress/element';
-const matchMediaCache = new Map();
+export const matchMediaCache = new Map();
/**
* A new MediaQueryList object for the media query
diff --git a/packages/compose/src/hooks/use-media-query/test/index.js b/packages/compose/src/hooks/use-media-query/test/index.js
index 88b2c1b0a3b..ffe7a897688 100644
--- a/packages/compose/src/hooks/use-media-query/test/index.js
+++ b/packages/compose/src/hooks/use-media-query/test/index.js
@@ -7,7 +7,7 @@ import { matchMedia, setMedia, cleanup } from 'mock-match-media';
/**
* Internal dependencies
*/
-import useMediaQuery from '../';
+import useMediaQuery, { matchMediaCache } from '../';
const TestComponent = ( { query } ) => {
const queryResult = useMediaQuery( query );
@@ -23,6 +23,7 @@ describe( 'useMediaQuery', () => {
setMedia( {
width: '960px',
} );
+ matchMediaCache.clear();
} );
afterEach( () => {
@tyxla That might work as well, but I opted for not clearing all the mocked media queries state instead, so we don't need to clear the cache and it will test it at the same time. |
Even better. Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀
What?
As seen in #60546 (among others, such as #57133),
matchMedia
is the most expensive part of these media queries. Let's cache it. Usually the number of different media queries is very small, so I wouldn't worry about the cache getting out of hand.Why?
How?
Testing Instructions
Testing Instructions for Keyboard
Screenshots or screencast