Browse documentation

Turbo Integration

When Symfony\UX\Turbo\TurboBundle is available, the response listener renders each queued alert as a <turbo-stream action="alert"> fragment.

What happens on the server

For main requests that are not redirects:

  • text/vnd.turbo-stream.html responses, and text/html responses to Turbo-Frame requests, get the rendered @SweetAlert/turbo/alert.html.twig streams (appended for turbo-stream responses, inserted inside the requested <turbo-frame> for frame responses; if the frame is not in the response, the alerts stay queued)
  • JSON object responses receive an extra ux_sweet_alert field containing an array of alert objects (not set if the key already exists)
  • other responses are left alone and the alerts stay queued for the next displayable response

Turbo-stream and Turbo-Frame responses

On turbo-stream responses the generated stream is appended to the body; on Turbo-Frame responses it is inserted right after the opening tag of the requested frame:

  <turbo-stream action="alert">
    <template>{"title":"Saved","icon":"success"}</template>
</turbo-stream>

JSON and fetch flows

The bundled Stimulus controller wraps window.fetch when Turbo stream support is available. It detects two cases:

  • text/vnd.turbo-stream.html responses
  • application/json responses containing a ux_sweet_alert array

Turbo streams are injected into the DOM so Turbo can execute the custom alert action; JSON alerts are fired directly.

Minimal controller example

  use Pentiminax\UX\SweetAlert\AlertManagerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;

public function __invoke(AlertManagerInterface $alertManager): JsonResponse
{
    $alertManager->info(
        title: 'Background sync finished',
        text: 'Your cache is fresh again.'
    );

    return $this->json(['status' => 'ok']);
}

The resulting JSON response still contains your original payload, plus the extra ux_sweet_alert key added by the listener.

Why it matters

This approach lets the same PHP API work for:

  • full-page HTML responses
  • Turbo-enabled fetch calls
  • JSON endpoints used by interactive dashboards or admin screens