Swap.Htmx vs Vanilla HTMX

What does Swap.Htmx add on top of plain HTMX?

HTMX is the underlying JavaScript library that enables HTML-over-the-wire interactions. It's framework-agnostic.

Swap.Htmx is a .NET library that provides a structured, productive way to build HTMX applications with ASP.NET Core.

What Swap.Htmx Adds

🎯

SwapResponse Builder

Fluent API instead of manual HX-* headers

💾

SwapState

Server-side state without JavaScript

Event System

[SwapHandler] for decoupled updates

🔄

Auto Layout Skip

HTMX requests skip layout automatically

🧭

<swap-nav>

SPA-like navigation with history

⚙️

Source Generators

Type-safe view & element constants

Code Comparison

Vanilla HTMX + ASP.NET

Response.Headers["HX-Trigger"] = "user:deleted";
Response.Headers["HX-Retarget"] = "#user-" + id;
Response.Headers["HX-Reswap"] = "delete";
// How to wire up side effects?

Swap.Htmx

return SwapResponse()
    .Remove($"#user-{id}")
    .Trigger("user:deleted");
[SwapHandler("user:deleted")]
public IActionResult RefreshCount() => ...

When to Use Vanilla HTMX

  • ✓ Very simple apps (1-2 interactions)
  • ✓ Learning HTMX fundamentals
  • ✓ Non-.NET backends
  • ✓ Maximum header control

When to Use Swap.Htmx

  • ✓ Production .NET apps
  • ✓ Multiple coordinated updates
  • ✓ Complex state management
  • ✓ Team productivity
  • ✓ Real-time features (SSE)