Skip to content

A full-featured CRM built with React, shadcn/ui, and Supabase.

move lazyLoad logic into its own file

ThieryMichel780882aba783
diff --git a/registry.json b/registry.json
index 7b98a23d..25abfda5 100644
--- a/registry.json
+++ b/registry.json
@@ -864,6 +864,10 @@
{
"path": "src/lib/toSlug.ts",
"type": "registry:lib"
+ },
+ {
+ "path": "src/lib/lazyImportWithReload.ts",
+ "type": "registry:lib"
}
]
}
diff --git a/src/components/atomic-crm/deals/index.ts b/src/components/atomic-crm/deals/index.ts
index 251bc16d..d220b60e 100644
--- a/src/components/atomic-crm/deals/index.ts
+++ b/src/components/atomic-crm/deals/index.ts
@@ -1,32 +1,6 @@
-import * as React from "react";
+import { lazyImportWithReload } from "@/lib/lazyImportWithReload";
-/**
- * Lazy-load with a full page reload on chunk load failure.
- * After a new deploy, the old service worker pre-cache may be replaced
- * while the page still holds old chunk references. A reload picks up
- * the new HTML + new SW cache, resolving the mismatch.
- * A sessionStorage guard prevents infinite reload loops.
- */
-function lazyWithReload<T extends React.ComponentType<any>>(
- importFn: () => Promise<{ default: T }>,
- chunkName: string,
-) {
- return React.lazy(() =>
- importFn().catch(() => {
- const key = `chunk-reload:${chunkName}`;
- if (!sessionStorage.getItem(key)) {
- sessionStorage.setItem(key, "1");
- window.location.reload();
- }
- // If we already reloaded once this session, surface the error
- return Promise.reject(
- new Error(`Failed to load chunk "${chunkName}" after reload`),
- );
- }),
- );
-}
-
-const DealList = lazyWithReload(() => import("./DealList"), "DealList");
+const DealList = lazyImportWithReload(() => import("./DealList"), "DealList");
export default {
list: DealList,
diff --git a/src/lib/lazyImportWithReload.ts b/src/lib/lazyImportWithReload.ts
new file mode 100644
index 00000000..61cc9e98
--- /dev/null
+++ b/src/lib/lazyImportWithReload.ts
@@ -0,0 +1,28 @@
+import React from "react";
+
+/**
+ * Lazy-load with a full page reload on chunk load failure.
+ * After a new deploy, the old service worker pre-cache may be replaced
+ * while the page still holds old chunk references. A reload picks up
+ * the new HTML + new SW cache, resolving the mismatch.
+ * A sessionStorage guard prevents infinite reload loops.
+ * Fow when we are offline with no cached version, the error is surfaced as usual.
+ */
+export function lazyImportWithReload<T extends React.ComponentType<unknown>>(
+ importFn: () => Promise<{ default: T }>,
+ chunkName: string,
+) {
+ return React.lazy(() =>
+ importFn().catch(() => {
+ const key = `chunk-reload:${chunkName}`;
+ if (!sessionStorage.getItem(key)) {
+ sessionStorage.setItem(key, "1");
+ window.location.reload();
+ }
+ // If we already reloaded once this session, surface the error
+ return Promise.reject(
+ new Error(`Failed to load chunk "${chunkName}" after reload`),
+ );
+ }),
+ );
+}