|
3 | 3 | * This is called once at app initialization, outside of React components. |
4 | 4 | */ |
5 | 5 | export function setupVersionSkewHandler() { |
6 | | - let isReloading = false; |
7 | | - |
8 | | - console.log("[Version Skew Handler] Setting up handlers"); |
9 | | - |
10 | | - // Listen for Vite preload errors (chunk loading failures) |
11 | | - window.addEventListener("vite:preloadError", (event: Event) => { |
12 | | - console.log("[Version Skew Handler] vite:preloadError event:", event); |
13 | | - |
14 | | - if (!isReloading) { |
15 | | - isReloading = true; |
16 | | - console.warn("[Version Skew Handler] Vite preload error detected - reloading application"); |
17 | | - |
18 | | - // Small delay to ensure the error is logged |
19 | | - setTimeout(() => { |
20 | | - window.location.reload(); |
21 | | - }, 100); |
22 | | - } |
23 | | - }); |
24 | | - |
25 | | - // Handle unhandled promise rejections from dynamic imports |
26 | | - window.addEventListener("unhandledrejection", (event: PromiseRejectionEvent) => { |
27 | | - const error = event.reason; |
28 | | - console.log("[Version Skew Handler] Unhandled rejection:", error); |
29 | | - |
30 | | - // Check if this is a chunk loading error |
31 | | - if ( |
32 | | - !isReloading && |
33 | | - error instanceof Error && |
34 | | - (error.message?.includes("Failed to fetch dynamically imported module") || |
35 | | - error.message?.includes("dynamically imported module") || |
36 | | - error.message?.includes("Failed to import")) |
37 | | - ) { |
38 | | - isReloading = true; |
39 | | - console.warn("[Version Skew Handler] Version skew detected - reloading application", error); |
40 | | - |
41 | | - // Force a full page reload |
42 | | - setTimeout(() => { |
43 | | - window.location.reload(); |
44 | | - }, 100); |
45 | | - |
46 | | - // Prevent the default error handling |
47 | | - event.preventDefault(); |
48 | | - } |
49 | | - }); |
50 | | - |
51 | | - // Override window.onerror to catch synchronous errors |
52 | | - const originalOnError = window.onerror; |
53 | | - window.onerror = function (message, source, lineno, colno, error) { |
54 | | - console.log("[Version Skew Handler] Window error:", { message, source, error }); |
55 | | - |
56 | | - if ( |
57 | | - !isReloading && |
58 | | - typeof message === "string" && |
59 | | - (message.includes("Failed to fetch dynamically imported module") || |
60 | | - message.includes("dynamically imported module") || |
61 | | - message.includes("Failed to import")) |
62 | | - ) { |
63 | | - isReloading = true; |
64 | | - console.warn("[Version Skew Handler] Window error detected as version skew - reloading"); |
65 | | - |
66 | | - setTimeout(() => { |
67 | | - window.location.reload(); |
68 | | - }, 100); |
69 | | - |
70 | | - return true; // Prevent default error handling |
71 | | - } |
72 | | - |
73 | | - // Call original handler if exists |
74 | | - if (originalOnError) { |
75 | | - return originalOnError.call(this, message, source, lineno, colno, error); |
76 | | - } |
77 | | - return false; |
78 | | - }; |
| 6 | + // let isReloading = false; |
| 7 | + // console.log("[Version Skew Handler] Setting up handlers"); |
| 8 | + // // Listen for Vite preload errors (chunk loading failures) |
| 9 | + // window.addEventListener("vite:preloadError", (event: Event) => { |
| 10 | + // console.log("[Version Skew Handler] vite:preloadError event:", event); |
| 11 | + // if (!isReloading) { |
| 12 | + // isReloading = true; |
| 13 | + // console.warn("[Version Skew Handler] Vite preload error detected - reloading application"); |
| 14 | + // // Small delay to ensure the error is logged |
| 15 | + // setTimeout(() => { |
| 16 | + // window.location.reload(); |
| 17 | + // }, 100); |
| 18 | + // } |
| 19 | + // }); |
| 20 | + // // Handle unhandled promise rejections from dynamic imports |
| 21 | + // window.addEventListener("unhandledrejection", (event: PromiseRejectionEvent) => { |
| 22 | + // const error = event.reason; |
| 23 | + // console.log("[Version Skew Handler] Unhandled rejection:", error); |
| 24 | + // // Check if this is a chunk loading error |
| 25 | + // if ( |
| 26 | + // !isReloading && |
| 27 | + // error instanceof Error && |
| 28 | + // (error.message?.includes("Failed to fetch dynamically imported module") || |
| 29 | + // error.message?.includes("dynamically imported module") || |
| 30 | + // error.message?.includes("Failed to import")) |
| 31 | + // ) { |
| 32 | + // isReloading = true; |
| 33 | + // console.warn("[Version Skew Handler] Version skew detected - reloading application", error); |
| 34 | + // // Force a full page reload |
| 35 | + // setTimeout(() => { |
| 36 | + // window.location.reload(); |
| 37 | + // }, 100); |
| 38 | + // // Prevent the default error handling |
| 39 | + // event.preventDefault(); |
| 40 | + // } |
| 41 | + // }); |
| 42 | + // // Override window.onerror to catch synchronous errors |
| 43 | + // const originalOnError = window.onerror; |
| 44 | + // window.onerror = function (message, source, lineno, colno, error) { |
| 45 | + // console.log("[Version Skew Handler] Window error:", { message, source, error }); |
| 46 | + // if ( |
| 47 | + // !isReloading && |
| 48 | + // typeof message === "string" && |
| 49 | + // (message.includes("Failed to fetch dynamically imported module") || |
| 50 | + // message.includes("dynamically imported module") || |
| 51 | + // message.includes("Failed to import")) |
| 52 | + // ) { |
| 53 | + // isReloading = true; |
| 54 | + // console.warn("[Version Skew Handler] Window error detected as version skew - reloading"); |
| 55 | + // setTimeout(() => { |
| 56 | + // window.location.reload(); |
| 57 | + // }, 100); |
| 58 | + // return true; // Prevent default error handling |
| 59 | + // } |
| 60 | + // // Call original handler if exists |
| 61 | + // if (originalOnError) { |
| 62 | + // return originalOnError.call(this, message, source, lineno, colno, error); |
| 63 | + // } |
| 64 | + // return false; |
| 65 | + // }; |
79 | 66 | } |
0 commit comments