Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

refactor(terminal-connection): streamline auto-connection logic and improve component readiness checks

Andras Bacsai7df6890920e2
diff --git a/app/Livewire/Project/Shared/ExecuteContainerCommand.php b/app/Livewire/Project/Shared/ExecuteContainerCommand.php
index 9891808b3e..40004354f3 100644
--- a/app/Livewire/Project/Shared/ExecuteContainerCommand.php
+++ b/app/Livewire/Project/Shared/ExecuteContainerCommand.php
@@ -168,11 +168,10 @@ class ExecuteContainerCommand extends Component
$this->connectionStatus = 'Waiting for terminal to be ready...';
}
- #[On('initializeTerminalConnection')]
public function initializeTerminalConnection()
{
// Only auto-connect if containers are loaded and we haven't attempted before
- if (! $this->containersLoaded || $this->autoConnectAttempted) {
+ if (! $this->containersLoaded || $this->autoConnectAttempted || $this->isConnecting) {
return;
}
diff --git a/resources/views/livewire/project/shared/execute-container-command.blade.php b/resources/views/livewire/project/shared/execute-container-command.blade.php
index f40a1c82f0..768680ec32 100644
--- a/resources/views/livewire/project/shared/execute-container-command.blade.php
+++ b/resources/views/livewire/project/shared/execute-container-command.blade.php
@@ -106,36 +106,27 @@
<script>
let autoConnectionAttempted = false;
- // Wait for terminal WebSocket to be ready before attempting auto-connection
- function tryAutoConnection() {
+ // Simple auto-connection that waits for component stability
+ function initializeConnection() {
if (autoConnectionAttempted) return;
+ autoConnectionAttempted = true;
- const terminalContainer = document.getElementById('terminal-container');
- if (!terminalContainer) return;
-
- // Check if Alpine component is initialized and WebSocket is ready
- if (terminalContainer._x_dataStack &&
- terminalContainer._x_dataStack[0] &&
- terminalContainer._x_dataStack[0].isWebSocketReady()) {
-
- autoConnectionAttempted = true;
- $wire.dispatchSelf('initializeTerminalConnection');
- }
+ // Add delay to ensure all components are fully rendered
+ setTimeout(() => {
+ $wire.call('initializeTerminalConnection');
+ }, 1000);
}
- // Listen for terminal WebSocket ready event
- document.addEventListener('terminal-websocket-ready', tryAutoConnection);
-
- // Fallback polling in case event is missed
- function pollForTerminalReady() {
- if (!autoConnectionAttempted) {
- tryAutoConnection();
- setTimeout(pollForTerminalReady, 300);
- }
- }
+ // Wait for everything to be ready before auto-connecting
+ document.addEventListener('DOMContentLoaded', () => {
+ setTimeout(initializeConnection, 500);
+ });
- // Start polling after a short delay to allow Alpine to initialize
- setTimeout(pollForTerminalReady, 200);
+ // Fallback for SPA navigation
+ document.addEventListener('livewire:navigated', () => {
+ autoConnectionAttempted = false;
+ setTimeout(initializeConnection, 500);
+ });
</script>
@endscript
</div>