Skip to content

Commit

Permalink
Add a guard to prevent access to window.matchMedia when it is undefined.
Browse files Browse the repository at this point in the history
  • Loading branch information
rtibbles committed Oct 17, 2023
1 parent 942d6e2 commit 7fb3079
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/useKResponsiveWindow/MediaQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ export default class MediaQuery {
* @returns {Object} Containing mediaQueryList, eventHandler, and stopListening
*/
startListening() {
//Prevent function execution if Nuxt is server side rendering
if (this.isNuxtServerSideRendering()) {
// Prevent function execution if Nuxt is server side rendering
if (this.isNuxtServerSideRendering() || !window.matchMedia) {
return;
}

Expand All @@ -48,6 +48,10 @@ export default class MediaQuery {
* Stop listening for media query events
*/
stopListening() {
// Prevent function execution if Nuxt is server side rendering
if (this.isNuxtServerSideRendering() || !window.matchMedia) {
return;
}
if (this.mediaQueryList.removeEventListener) {
this.mediaQueryList.removeEventListener('change', this.eventHandler);
} else {
Expand Down

0 comments on commit 7fb3079

Please sign in to comment.