From 6bef35b406d51bd48d078d5d07f17bc39a3ce274 Mon Sep 17 00:00:00 2001 From: Nariman Jelveh Date: Fri, 3 May 2024 20:21:26 -0700 Subject: [PATCH] Fix issue with passive event listeners via jQuery --- src/UI/UIDesktop.js | 2 +- src/initgui.js | 31 ++++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/UI/UIDesktop.js b/src/UI/UIDesktop.js index 92d3bf82..59b9a497 100644 --- a/src/UI/UIDesktop.js +++ b/src/UI/UIDesktop.js @@ -972,7 +972,7 @@ async function UIDesktop(options){ } } - $(el_desktop).on('mousedown touchstart', function(e){ + $(el_desktop).on('mousedown touchstart', { passive: true }, function(e){ // dimiss touchstart on regular devices if(e.type==='taphold' && !isMobile.phone && !isMobile.tablet) return; diff --git a/src/initgui.js b/src/initgui.js index e2a20832..16bab2e6 100644 --- a/src/initgui.js +++ b/src/initgui.js @@ -68,6 +68,35 @@ const launch_services = async function () { } }; +// This code snippet addresses the issue flagged by Lighthouse regarding the use of +// passive event listeners to enhance scrolling performance. It provides custom +// implementations for touchstart, touchmove, wheel, and mousewheel events in jQuery. +// By setting the 'passive' option appropriately, it ensures that default browser +// behavior is prevented when necessary, thereby improving page scroll performance. +// More info: https://stackoverflow.com/a/62177358 +if(jQuery){ + 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 }); + } + }; +} + window.initgui = async function(){ let url = new URL(window.location); url = url.href; @@ -84,7 +113,7 @@ window.initgui = async function(){ // Print the version to the console puter.os.version() .then(res => { - const deployed_date = new Date(res.deploy_timestamp).toLocaleString(); + const deployed_date = new Date(res.deploy_timestamp); console.log(`Version: ${(res.version)} | Server: ${(res.location)} | Deployed: ${(deployed_date)}`); }) .catch(error => {