#654·GSAP

`gsap.matchMedia()`: `revert()` / `kill()` never remove the MediaQueryList listeners (leak)

Author: ferdydurqueCreated Sep 21, 2026Updated Sep 21, 2026

Summary

For every condition passed to gsap.matchMedia().add(), gsap attaches _onMediaChange to a new MediaQueryList. Nothing ever removes that listener: revert() and kill() take the context out of _media, but leave the listener on the MediaQueryList. The browser keeps a MediaQueryList alive while it has a listener, so each create/revert cycle leaks one listener, and its MediaQueryList, per condition.

With one long-lived matchMedia() this doesn't matter. It adds up wherever matchMedia is created and reverted repeatedly, which is the recommended pattern for components: React useGSAP(), a Vue onUnmounted() revert, or sites with page transitions (swup, Barba) that revert a page's animations on every navigation. I found it with a listener-count leak check on a swup site: eight components using gsap.matchMedia() added eight listeners per navigation, indefinitely.

Reproduction

A standalone page. It wraps window.matchMedia only to count change listeners that are added and never removed:

xml
<!doctype html>
<html>
<body>
<pre id="out"></pre>
<script>
  // Count "change" listeners added to MediaQueryLists and never removed.
  let live = 0;
  const nativeMatchMedia = window.matchMedia.bind(window);
  window.matchMedia = (query) => {
    const mql = nativeMatchMedia(query);
    const { addListener, removeListener, addEventListener, removeEventListener } = mql;
    mql.addListener = function (fn) { live++; return addListener.call(this, fn); };
    mql.removeListener = function (fn) { live--; return removeListener.call(this, fn); };
    mql.addEventListener = function (t, fn, o) { if (t === 'change') live++; return addEventListener.call(this, t, fn, o); };
    mql.removeEventListener = function (t, fn, o) { if (t === 'change') live--; return removeEventListener.call(this, t, fn, o); };
    return mql;
  };
</script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/gsap.min.js"></script>
<script>
  const run = (label, cycle) => {
    live = 0;
    for (let i = 0; i < 100; i++) cycle();
    document.getElementById('out').textContent += `${label}: ${live}\n`;
  };

  run('mm.revert()', () => {
    const mm = gsap.matchMedia();
    mm.add('(min-width: 600px)', () => {});
    mm.revert();
  });

  run('mm.kill()', () => {
    const mm = gsap.matchMedia();
    mm.add('(min-width: 600px)', () => {});
    mm.kill();
  });

  run('inside a reverted gsap.context()', () => {
    gsap.context(() => {
      gsap.matchMedia().add('(min-width: 600px)', () => {});
    }).revert();
  });

  run('two conditions, mm.revert()', () => {
    const mm = gsap.matchMedia();
    mm.add({ wide: '(min-width: 600px)', reduced: '(prefers-reduced-motion: reduce)' }, () => {});
    mm.revert();
  });
</script>
</body>
</html>

Actual — listeners still attached after 100 create/revert cycles:

mm.revert(): 100
mm.kill(): 100
inside a reverted gsap.context(): 100
two conditions, mm.revert(): 200

Chrome's own counter agrees, without the wrapper: after forced garbage collection, Memory.getDOMCounters() (DevTools protocol) reports jsEventListeners: 500.

Expected — 0 in every case: reverting or killing a MatchMedia removes the listeners it added.

Cause

In src/gsap-core.js (checked on master @ 13e2b79):

  • MatchMedia.add() does mq.addListener ? mq.addListener(_onMediaChange) : mq.addEventListener("change", _onMediaChange) for each condition, but keeps no reference to mq.
  • MatchMedia.kill() → Context.kill(revert, true). The matchMedia branch only splices the context out of _media. No removeListener / removeEventListener for _onMediaChange exists anywhere in the file.

Suggested fix

Keep each context's MediaQueryLists, and remove the listener in the matchMedia branch of Context.kill(). Only MatchMedia.kill() / revert() reach that branch. The temporary c.revert() that _onMediaChange does on a breakpoint change passes no flag, so a live matchMedia keeps listening.

diff
 				if (mq) {
 					_media.indexOf(context) < 0 && _media.push(context);
 					(cond[p] = mq.matches) && (active = 1);
 					mq.addListener ? mq.addListener(_onMediaChange) : mq.addEventListener("change", _onMediaChange);
+					(context._mqs || (context._mqs = [])).push(mq);
 				}
diff
 		if (matchMedia) {
 			let i = _media.length;
 			while (i--) {
 				_media[i].id === this.id && _media.splice(i, 1);
 			}
+			this._mqs && this._mqs.forEach(mq => mq.removeListener ? mq.removeListener(_onMediaChange) : mq.removeEventListener("change", _onMediaChange));
+			this._mqs = null;
 		}

I applied the same change to dist/gsap.js 3.15.0 and tested it in Chrome:

  • The repro above reports 0 for all four cases, and jsEventListeners is 0.
  • Behaviour is unchanged. I compared stock and patched on a (min-width: 600px) query while resizing the viewport 800 → 400 → 800: it matches on load, reverts below the breakpoint, matches again above it, and runs the cleanup function each time. After mm.revert() nothing fires any more.

Workaround

Listen yourself, and create or revert a plain gsap.context():

javascript
const query = window.matchMedia('(prefers-reduced-motion: reduce)');
let ctx = null;
const apply = () => {
  ctx?.revert();
  ctx = query.matches ? null : gsap.context(() => { /* animations */ });
};
apply();
query.addEventListener('change', apply);

// teardown
query.removeEventListener('change', apply);
ctx?.revert();

Environment

  • gsap 3.15.0 from npm (latest at the time of writing) and the jsDelivr build; same code on master @ 13e2b79
  • Chrome 152.0.7977.83, macOS. I only tested Chrome, but no code path removes the listener, so it shouldn't depend on the browser.