浏览代码

Handle mouseoverListener’s event.target.closest sometimes not existing

Alexandre Dieulot 2 年之前
父节点
当前提交
00249af912
共有 1 个文件被更改,包括 9 次插入0 次删除
  1. 9 0
      instantpage.js

+ 9 - 0
instantpage.js

@@ -124,6 +124,15 @@ function mouseoverListener(event) {
     return
   }
 
+  if (!('closest' in event.target)) {
+    // Without this check sometimes an error “event.target.closest is not a function” is thrown, for unknown reasons
+    // That error denotes that `event.target` isn’t undefined. My best guess is that it’s the Document.
+
+    // Details could be gleaned from throwing such an error:
+    //throw new TypeError(`instant.page non-element event target: timeStamp=${~~event.timeStamp}, type=${event.type}, typeof=${typeof event.target}, nodeType=${event.target.nodeType}, nodeName=${event.target.nodeName}, viewport=${innerWidth}x${innerHeight}, coords=${event.clientX}x${event.clientY}, scroll=${scrollX}x${scrollY}`)
+
+    return
+  }
   const linkElement = event.target.closest('a')
 
   if (!isPreloadable(linkElement)) {