post-redirect-gethtmx

Post/Redirect/Get needed if you submit HTML fragments?


In the past I used the Post/Redirect/Get pattern:

Is this still needed if you submit html fragments via htmx?


Solution

  • By and large no, you will not need to implement the PRG pattern.

    Since htmx uses AJAX for most interactions, there is no request sitting in the browser history, and hitting refresh will not re-submit a POST (or DELETE or whatever).

    That said, htmx trys to be compatible with the PRG pattern, and tries to update the URL if a redirect occurs by detecting the :

    https://github.com/bigskysoftware/htmx/blob/1d4c79490e491813ffb780354ec5df6d080b1e09/src/htmx.js#L2146

    https://github.com/bigskysoftware/htmx/blob/1d4c79490e491813ffb780354ec5df6d080b1e09/src/htmx.js#L1851

    If you do something like inline editing:

    https://htmx.org/examples/click-to-edit/

    The point becomes moot to a large extent, since you can have the edit UI at the same URL as the view URL.