2026-02-21 16:42:46 +07:00
|
|
|
import initializeApp from "@/shared/services/initializeApp";
|
2026-01-05 09:58:59 +07:00
|
|
|
|
2026-05-05 10:32:59 +07:00
|
|
|
// Survive Next.js HMR — module-level flag resets on reload, globalThis persists
|
|
|
|
|
const g = globalThis.__cloudSyncInit ??= { initialized: false, inProgress: null };
|
2026-01-05 09:58:59 +07:00
|
|
|
|
2026-02-21 16:42:46 +07:00
|
|
|
export async function ensureAppInitialized() {
|
2026-05-05 10:32:59 +07:00
|
|
|
if (g.initialized) return true;
|
|
|
|
|
if (g.inProgress) return g.inProgress;
|
|
|
|
|
g.inProgress = (async () => {
|
2026-01-05 09:58:59 +07:00
|
|
|
try {
|
2026-02-21 16:42:46 +07:00
|
|
|
await initializeApp();
|
2026-05-05 10:32:59 +07:00
|
|
|
g.initialized = true;
|
2026-01-05 09:58:59 +07:00
|
|
|
} catch (error) {
|
2026-02-21 16:42:46 +07:00
|
|
|
console.error("[ServerInit] Error initializing app:", error);
|
2026-05-05 10:32:59 +07:00
|
|
|
} finally {
|
|
|
|
|
g.inProgress = null;
|
2026-01-05 09:58:59 +07:00
|
|
|
}
|
2026-05-05 10:32:59 +07:00
|
|
|
return g.initialized;
|
|
|
|
|
})();
|
|
|
|
|
return g.inProgress;
|
2026-01-05 09:58:59 +07:00
|
|
|
}
|
|
|
|
|
|
2026-03-17 16:12:25 +07:00
|
|
|
// Auto-initialize at runtime only, not during next build
|
|
|
|
|
if (process.env.NEXT_PHASE !== "phase-production-build") {
|
|
|
|
|
ensureAppInitialized().catch(console.log);
|
|
|
|
|
}
|
2026-01-05 09:58:59 +07:00
|
|
|
|
2026-02-21 16:42:46 +07:00
|
|
|
export default ensureAppInitialized;
|