🚀 DEPLOYMENT FLOW
Complete Deployment Process
Trigger: User clicks "Preview" button
Step-by-step:
Pre-deployment Validation
- Check files exist in generatedFilesMap
- Verify no generation in progress
- Get or create sessionId
Sandbox Instance Check
- If no sandboxInstanceId: create new instance
- If exists: check health status
- If unhealthy: reset session, create new instance
Create Instance (if needed)
→ createInstance(templateName, projectName, webhookUrl) ← { instanceId, url, status } → Save instanceId to stateFile Synchronization
→ Collect all files from generatedFilesMap → Format as { path, content, encoding: 'utf-8' }[] → writeFiles(instanceId, files, "Deploy generated code") ← { success: true, filesWritten: 42 }Package.json Sync
→ Check if package.json changed → If changed: executeCommands(['npm install']) → Wait for completion (timeout: 60s) → Cache new package.json in stateBootstrap Commands (if needed)
→ Execute commandsHistory (previously run user commands) → Validates/filters dangerous commands → Runs: npm install, setup scripts, etc.Start Dev Server
→ Already running from instance creation → Or trigger via command if stopped → Monitor startup logsHealth Check Loop
→ setInterval(30s): ping sandbox → Check status endpoint → If unhealthy: log warning, may resetReturn Preview URL
→ Send URL to frontend via WebSocket → User can open in iframe or new tab → App is live and interactive
Redeployment (Incremental Updates)
When files change after initial deploy:
Diff Detection
- Compare file hashes in generatedFilesMap
- Only sync changed files
Partial Sync
→ writeFiles(instanceId, [changedFiles]) ← Hot reload triggered automaticallyNo Full Rebuild
- Dev server hot reloads changes
- Fast iteration (< 1s typically)
Deployment Errors & Recovery
Common errors:
Timeout (60s)
- Cause: npm install too slow, network issues
- Recovery: Reset sessionId, retry with fresh instance
Instance Not Found
- Cause: Container crashed or evicted
- Recovery: Create new instance, redeploy all files
Command Execution Failed
- Cause: Invalid package.json, dependency conflicts
- Recovery: Show error to user, allow editing
Health Check Failed
- Cause: Dev server crashed, port conflict
- Recovery: Reset session on next deploy attempt