imacrayon / alpine-ajax

An Alpine.js plugin for building server-powered frontends.
https://alpine-ajax.js.org
MIT License
642 stars 13 forks source link

Update the URL when form GET requests are made #19

Closed imacrayon closed 11 months ago

imacrayon commented 1 year ago

A form issuing a GET request will change the query string in the current URL. It would be good if AJAX-enabled forms would also update the URL so pages work the same way with or without JavaScript.

vjanssens commented 1 year ago

This would be great to have included, but how could this be done in the meanwhile?

imacrayon commented 1 year ago

If you listen for the ajax:success event it contains details about the server response in $event.detail. You should be able to use that info to update the URL:

<form id="search_form" x-init x-target action="/search" @ajax:success="window.history.pushState({}, '', $event.detail.url)">