@@ -82,21 +82,25 @@ export const usePreventLeave = ({
82
82
// check when page is about to be changed
83
83
useEffect ( ( ) => {
84
84
function isAnchorOfCurrentUrl ( currentUrl : string , newUrl : string ) {
85
- const currentUrlObj = new URL ( currentUrl )
86
- const newUrlObj = new URL ( newUrl )
87
- // Compare hostname, pathname, and search parameters
88
- if (
89
- currentUrlObj . hostname === newUrlObj . hostname &&
90
- currentUrlObj . pathname === newUrlObj . pathname &&
91
- currentUrlObj . search === newUrlObj . search
92
- ) {
93
- // Check if the new URL is just an anchor of the current URL page
94
- const currentHash = currentUrlObj . hash
95
- const newHash = newUrlObj . hash
96
- return (
97
- currentHash !== newHash &&
98
- currentUrlObj . href . replace ( currentHash , '' ) === newUrlObj . href . replace ( newHash , '' )
99
- )
85
+ try {
86
+ const currentUrlObj = new URL ( currentUrl )
87
+ const newUrlObj = new URL ( newUrl )
88
+ // Compare hostname, pathname, and search parameters
89
+ if (
90
+ currentUrlObj . hostname === newUrlObj . hostname &&
91
+ currentUrlObj . pathname === newUrlObj . pathname &&
92
+ currentUrlObj . search === newUrlObj . search
93
+ ) {
94
+ // Check if the new URL is just an anchor of the current URL page
95
+ const currentHash = currentUrlObj . hash
96
+ const newHash = newUrlObj . hash
97
+ return (
98
+ currentHash !== newHash &&
99
+ currentUrlObj . href . replace ( currentHash , '' ) === newUrlObj . href . replace ( newHash , '' )
100
+ )
101
+ }
102
+ } catch ( err ) {
103
+ console . log ( 'Unexpected exception thrown in LeaveWithoutSaving:isAnchorOfCurrentUrl' , err )
100
104
}
101
105
return false
102
106
}
@@ -135,7 +139,7 @@ export const usePreventLeave = ({
135
139
}
136
140
}
137
141
} catch ( err ) {
138
- console . log ( 'An unexpected exception was thrown in LeaveWithoutSaving:usePreventLeave' , err )
142
+ console . log ( 'Unexpected exception thrown in LeaveWithoutSaving:usePreventLeave' , err )
139
143
}
140
144
}
141
145
0 commit comments