Files
9router/src/lib/initCloudSync.js
T

31 lines
967 B
JavaScript
Raw Normal View History

2026-02-21 16:42:46 +07:00
import initializeApp from "@/shared/services/initializeApp";
2026-01-05 09:58: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() {
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();
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);
} finally {
g.inProgress = null;
2026-01-05 09:58:59 +07:00
}
return g.initialized;
})();
return g.inProgress;
2026-01-05 09:58:59 +07:00
}
// Auto-initialize at runtime only, not during next build.
// Defer to next tick so HTTP server can accept connections before heavy init runs.
2026-03-17 16:12:25 +07:00
if (process.env.NEXT_PHASE !== "phase-production-build") {
setImmediate(() => {
ensureAppInitialized().catch(console.log);
});
2026-03-17 16:12:25 +07:00
}
2026-01-05 09:58:59 +07:00
2026-02-21 16:42:46 +07:00
export default ensureAppInitialized;