Renerick / htmx-signalr

htmx extension for interacting with ASP.NET Core SignalR connections directly from HTML
MIT License
39 stars 5 forks source link

Htmx Headers #13

Open Nairda015 opened 3 months ago

Nairda015 commented 3 months ago

Maybe we can add support for htmx headers by sending object instead of string

public class HtmxHeaders  //This will be added to library with fluent api or sth
{
    [JsonPropertyName("HX-Target")]
    public string Target { get; set; }
    [JsonPropertyName("HX-Push-Url")]
    public string PushUrl { get; set; }
}
var headers = new HtmxHeaders
{
  Target = "game",
  PushUrl = $"game/{log.GameId.ToString()}"
};

await hubContext.Clients
    .Group(log.GameId.Value)
    .SendAsync("game-started", message, new { HEADERS = headers }, ct);

this code will send

{
  "type": 1,
  "target": "game-started",
  "arguments": [
    "<div>Something</div>",
    {
      "headers": {
        "HX-Target": "game",
        "HX-Push-Url": "game/c-BsMYCRPQ"
      }
    }
  ]
}

Question is if this will break something and if this will be hard to handle in js?

For comparison this is message sent from the client to server

{
  "arguments": [
    {
      "playerId": "Walter.Bednar",
      "HEADERS": {
        "HX-Request": "true",
        "HX-Trigger": null,
        "HX-Trigger-Name": null,
        "HX-Target": "game",
        "HX-Current-URL": "http://192.168.0.10:5104/"
      }
    }
  ],
  "target": "newGame",
  "type": 1
}
Renerick commented 3 months ago

I thought about implementing it, but htmx doesn't expose a convenient method to handle all response headers automatically, so it would require copying and pasting headers processing from htmx core. I discarded this idea at the time, but thinking about it, maybe it's not that bad actually

The real issue is lack of history related methods in public or internal API, which are unlikely to be exposed in htmx 1.0, but I'll bring it up with the team