NervJS / nerv

A blazing fast React alternative, compatible with IE8 and React 16.

Home Page:https://nerv.aotu.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Violation] Added non-passive event listener to a scroll-blocking <some> event. Consider marking event handler as 'passive' to make the page more responsive. See <URL>

forconz opened this issue · comments

我的项目有引用nervjs,在开发一个表单页面的时候发现300多个Violation,多个交互操作下来,高达几千个Violation提示,这个有点吓人了
QQ截图20190930133734

出现这个提示
[Violation] Added non-passive event listener to a scroll-blocking event. Consider marking event handler as 'passive' to make the page more responsive. See

代码主要是这个js文件导致的
webpack:///node_modules/_nervjs@1.4.6@nervjs/dist/index.esm.js?963e
代码为:
node.addEventListener(parseEventName(eventName), eventHandler, false);

由于passive对象只在Chrome浏览器中支持,所以这里需要做一个兼容处理
把代码
node.addEventListener(parseEventName(eventName), eventHandler, false);
替换为:

    var passiveSupported = false;
    try {
        var options = Object.defineProperty({}, "passive", {
        get: function() {        
            passiveSupported = true;
        }
        });
        window.addEventListener("test", null, options);
    }
    catch(err) {}

    console.log(`eventName=${eventName}`);
    if(eventName==='touchstart' || eventName==='ontouchmove')
        node.addEventListener(parseEventName(eventName), eventHandler, passiveSupported ? { passive: true } : false);
    else
        node.addEventListener(parseEventName(eventName), eventHandler, false);

再看Chrome调试会发现,Chrome已经不再出现让人头大的提醒。

然后再去看微信开发者工具,神奇的发现几百个 [Violation] Added non-passive event listener to a scroll-blocking event. Consider marking event handler as 'passive' to make the page more responsive. See
这样的提示,消失了~~

万能的码主,verbose终于消失了...

Passive event listeners
https://github.com/WICG/EventListenerOptions/blob/gh-pages/explainer.md

[小程序之菜鸟填坑-1]Passive Event Listeners —— 被动事件监听器
https://blog.csdn.net/qq_33331407/article/details/84395242