Module Lifecycle
Initialization, authentication, runtime, and shutdown phases of an Insolitum Universe module.
Module Lifecycle
Every module goes through distinct phases from load to teardown.
Phase 1: Loading
The Shell renders an <iframe> pointing to your module's deploy_url:
Your Next.js app boots, loads layout.tsx, and mounts ShellAuthProvider.
Phase 2: Authentication
The ShellAuthProvider handles this automatically:
- On mount, reads
org_idfrom URL query parameters - Sends
REQUEST_AUTH_SESSIONtowindow.parentviapostMessage - Shell responds with
AUTH_SESSIONcontaining JWT tokens - Provider calls
supabase.auth.setSession()with the tokens - Sets
isAuthenticated = true, makesuserandorganizationIdavailable
Timeout fallback: If no response arrives within 5 seconds, isLoading is set to false to prevent infinite loading states.
Phase 3: Runtime
Once authenticated, your module can:
- Query data via
useModuleApi()β all queries are automatically scoped to the current organization - Subscribe to events via
useNats()β real-time telemetry, alerts, and custom events - Navigate the Shell via
navigateShell()β trigger navigation in the parent frame
Phase 4: Navigation
When the user switches to another module or navigates away:
- Shell removes the iframe from DOM
- Your module's React tree unmounts
- Cleanup functions in
useEffecthooks run (Socket.io disconnect, etc.) - Module process stays running on the server (for next load)
Modules should be stateless between sessions. Don't rely on in-memory state persisting across iframe loads. Use Supabase for persistent storage.
Provider Chain
The recommended provider hierarchy for your module's layout.tsx:
ShellAuthProvider must be the outermost provider since all other hooks depend on the auth context.