Skip to content

Add checks on component setup to avoid runtime errors #246

@bombillazo

Description

@bombillazo

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch [email protected] for the project I'm working on.

Sometimes the component failed to load since not all conditions to handle the component are met. These changes add checks to avoid runtime errors.

Here is the diff that solved my problem:

diff --git a/node_modules/nprogress/nprogress.js b/node_modules/nprogress/nprogress.js
index b23b300..d99e2d6 100644
--- a/node_modules/nprogress/nprogress.js
+++ b/node_modules/nprogress/nprogress.js
@@ -240,11 +240,14 @@
       spinner && removeElement(spinner);
     }
 
+    if(!parent){
+      parent   = document.querySelector('body')
+    }
+
     if (parent != document.body) {
       addClass(parent, 'nprogress-custom-parent');
     }
-
-    parent.appendChild(progress);
+    parent?.appendChild(progress);
     return progress;
   };
 
@@ -433,7 +436,8 @@
     if (hasClass(oldList, name)) return; 
 
     // Trim the opening space.
-    element.className = newList.substring(1);
+    if(element)
+      element.className = newList.substring(1);
   }
 
   /**
@@ -460,7 +464,7 @@
    */
 
   function classList(element) {
-    return (' ' + (element.className || '') + ' ').replace(/\s+/gi, ' ');
+    return (' ' + (element?.className || '') + ' ').replace(/\s+/gi, ' ');
   }
 
   /**

This issue body was partially generated by patch-package.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions