277

I am using hammer for dragging and it is getting choppy when loading other stuff, as this warning message is telling me.

Handling of 'touchstart' input event was delayed for X ms due to main thread being busy. Consider marking event handler as 'passive' to make the page more responsive.

So I tried to add 'passive' to the listener like so

Hammer(element[0]).on("touchstart", function(ev) {
  // stuff
}, {
  passive: true
});

but I'm still getting this warning.

Sonicd300
  • 1,950
  • 1
  • 16
  • 22
Matt
  • 33,328
  • 25
  • 83
  • 97

9 Answers9

308

For those receiving this warning for the first time, it is due to a bleeding edge feature called Passive Event Listeners that has been implemented in browsers fairly recently (summer 2016). From https://github.com/WICG/EventListenerOptions/blob/gh-pages/explainer.md:

Passive event listeners are a new feature in the DOM spec that enable developers to opt-in to better scroll performance by eliminating the need for scrolling to block on touch and wheel event listeners. Developers can annotate touch and wheel listeners with {passive: true} to indicate that they will never invoke preventDefault. This feature shipped in Chrome 51, Firefox 49 and landed in WebKit. For full official explanation read more here.

See also: What are passive event listeners?

You may have to wait for your .js library to implement support.

If you are handling events indirectly via a JavaScript library, you may be at the mercy of that particular library's support for the feature. As of December 2019, it does not look like any of the major libraries have implemented support. Some examples:

Martin
  • 5,714
  • 2
  • 21
  • 41
Anson Kao
  • 5,256
  • 4
  • 28
  • 37
  • 21
    what about ionic libraries? – abhit Dec 15 '16 at 10:17
  • What about ngTouch? – arqam Mar 06 '17 at 10:49
  • 12
    I am calling `preventDefault()` - Is it possible to suppress this warning? – maja May 29 '17 at 06:51
  • 2
    @maja no - https://stackoverflow.com/questions/44060131/zone-js-violation-warnings-on-console-in-angular-project-only-on-chrome/44339214#44339214 – Zze Jun 09 '17 at 00:47
  • Does **[Violation] Added non-passive event listener to a scroll-blocking 'mousewheel' event. Consider marking event handler as 'passive' to make the page more responsive.** block an extension from being used? – SuperUberDuper Jul 11 '17 at 09:59
  • 19
    Google Maps JavaScript API version 3 also generates these warnings. Issue is being tracked at https://issuetracker.google.com/issues/63211698. (Kind of ironic, considering that Google Chrome is warning about violations that Google Maps JavaScript API generates.) – Jochem Schulenklopper Aug 02 '17 at 07:41
  • 2
    @JochemSchulenklopper, yap, ironic, Google Charts API is also giving these warnings. – João Pimentel Ferreira Aug 12 '17 at 22:10
  • there is an chrome update, warning is still shown but console.warn and console.error works now after the violation message – mtizziani Apr 20 '18 at 08:12
  • 12
    to suppress this warning you can ` addEventListener('touchstart', this.callPassedFuntion, { passive: false })` – Shlomi Schwartz Jun 13 '18 at 08:07
  • 2
    @maja The warning is only issued if you don't specify `passive`. By default, the browser listens for `preventDefault` but it would be a big improvement if it didn't. So, it will listen, but it will let you know: hey, you could improve your app here. If not, let me know you can't (by stating: `passive: false`). – tao Sep 23 '18 at 23:49
  • Where to put `document.addEventListener('touchstart', function (event) { event.preventDefault(); }, { passive: true });` – Bandham Manikanta Jun 03 '19 at 11:03
  • Anson Kao "Developers can annotate touch and wheel listeners with {passive: true} to indicate that they will never invoke preventDefault." WHERE and WHAT SYNTAX? – Cymro Jun 09 '20 at 19:08
  • Angular now claims to support passive event listeners, including making the scroll event passive. It is described how to enable passive scrolling here: [angular.io/guide/user-input#source-code](https://angular.io/guide/user-input#source-code) – Devin Carpenter Oct 19 '20 at 16:06
22

The following library resolved the issue.
Simply add this code to your project.

<script type="text/javascript" src="https://unpkg.com/default-passive-events"></script>

If you need more information visit this library.

Shanaka Rathnayaka
  • 2,462
  • 1
  • 23
  • 23
16

This hides the warning message:

jQuery.event.special.touchstart = {
  setup: function( _, ns, handle ) {
      this.addEventListener("touchstart", handle, { passive: !ns.includes("noPreventDefault") });
  }
};
AndreyP
  • 2,510
  • 1
  • 29
  • 17
Iván Rodríguez
  • 447
  • 4
  • 10
  • 11
    Wouldnt the goal to be to stop the actual event? I wouldn't want to hide the message until I have handled the issue. – yardpenalty.com Feb 27 '19 at 16:06
  • 1
    I think it is a jquery library issue. I think developers will must fix it. But if you will get it, let me know how to do it please. Thank you so much. – Iván Rodríguez Feb 27 '19 at 18:23
  • For sure Ivan! Yes it is. Hey, now I am curious... I am using d3 plugin and I am getting like 2300 violations. Maybe your code will help! I will keep you posted! – yardpenalty.com Feb 27 '19 at 19:03
  • 1
    @yardpenalty.com, no, stopping the event is not the goal! The warning states you have placed your listener without specifying whether it might or might not prevent default behavior on the event. If you have cases where you want to call `preventDefault()`, you should specify `passive: false`. If not, specify `passive: true`. You only get the warning if you don't specify either. If you specify `passive: true` and `preventDefault()` gets called, it results in an error and default is not prevented. Specifying `passive` is not a hack here. It's ***the solution***. It's what the warning asks for! – tao Apr 18 '20 at 03:13
  • @tao thanks for comment. It's been a few years but I will definitely remember the solution in the future! – yardpenalty.com Apr 30 '20 at 02:06
8

For those stuck with legacy issues, find the line throwing the error and add {passive: true} - eg:

this.element.addEventListener(t, e, !1)

becomes

this.element.addEventListener(t, e, { passive: true} )
Alfred Wallace
  • 1,741
  • 1
  • 14
  • 32
8

For jquery-ui-dragable with jquery-ui-touch-punch I fixed it similar to Iván Rodríguez, but with one more event override for touchmove:

jQuery.event.special.touchstart = {
    setup: function( _, ns, handle ) {
        this.addEventListener('touchstart', handle, { passive: !ns.includes('noPreventDefault') });
    }
};
jQuery.event.special.touchmove = {
    setup: function( _, ns, handle ) {
        this.addEventListener('touchmove', handle, { passive: !ns.includes('noPreventDefault') });
    }
};
AndreyP
  • 2,510
  • 1
  • 29
  • 17
6

The warning is only issued if you don't specify passive.

To suppress this warning you can:

addEventListener('touchstart', this.callPassedFuntion, { 
  passive: false 
})

By default, the browser listens for preventDefault. So, it will listen, but it will let you know: hey, you could improve your app here. If not, let me know you can't (by stating: passive: false).

I've created this answer from the comments of the accepted answer, which could be harder to find for anyone trying to find this.

ssten
  • 1,848
  • 1
  • 16
  • 28
2

I think in addition to touch-based events you can add scroll-based fixes so to prevent google page score from flagging it for Desktop vs Mobile:

// Passive event listeners (Tw0 slight variations in setting the flag)

jQuery.event.special.touchstart = {
    setup: function( _, ns, handle ) {
        this.addEventListener("touchstart", handle, { passive: !ns.includes("noPreventDefault") });
    }
};
jQuery.event.special.touchmove = {
    setup: function( _, ns, handle ) {
        this.addEventListener("touchmove", handle, { passive: !ns.includes("noPreventDefault") });
    }
};
jQuery.event.special.wheel = {
    setup: function( _, ns, handle ){
        this.addEventListener("wheel", handle, { passive: true });
    }
};
jQuery.event.special.mousewheel = {
    setup: function( _, ns, handle ){
        this.addEventListener("mousewheel", handle, { passive: true });
    }
};
Riad N Haddad
  • 101
  • 1
  • 4
1

I found a solution that works on jQuery 3.4.1 slim

After un-minifying, add {passive: true} to the addEventListener function on line 1567 like so:

t.addEventListener(p, a, {passive: true}))

Nothing breaks and lighthouse audits don't complain about the listeners.

Mark Lancaster
  • 194
  • 2
  • 6
-1

I have faced same problem. Then I used this

<script type="text/javascript" src="https://unpkg.com/default-passive-events"></script>

And hopefully your issues will be solved.