Skip to content

Commit

Permalink
Fixed JS helpers causing wrong url to be shown in the url bar when a …
Browse files Browse the repository at this point in the history
…form is submitted

It was e.g. showing /CreatePost instead of /NewPost. Now when the user refreshes the page, the `GET /CreatePost` request will fail and trigger an error. Instead the url should not have been updated to begin with.
  • Loading branch information
mpscholten committed Apr 17, 2023
1 parent ac2bddb commit f1ffc95
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/IHP/static/helpers.js
Expand Up @@ -196,10 +196,11 @@ window.submitForm = function (form, possibleClickedButton) {
for (var pair of formData.entries()) {
url.searchParams.set(pair[0], pair[1]);
}
urlPathnameWithQuery += url.search; // Append the query parameters submitted via the form
}
urlPathnameWithQuery += url.search; // Append the query parameters submitted via the form

var responseUrlPathName = new URL(request.responseURL).pathname
var responseUrl = new URL(request.responseURL);
var responseUrlPath = responseUrl.pathname + responseUrl.search;

if (window.Turbolinks) {
var snapshot = new Turbolinks.Snapshot(
Expand All @@ -210,7 +211,7 @@ window.submitForm = function (form, possibleClickedButton) {
Turbolinks.clearCache();
if (urlPathnameWithQuery !== formAction) {
history.pushState({ turbolinks: true }, '', request.responseURL);
} else if (urlPathnameWithQuery !== responseUrlPathName) {
} else if (urlPathnameWithQuery !== responseUrlPath) {
history.replaceState({}, "", request.responseURL)
};
var turbolinkLoadEvent = new CustomEvent('turbolinks:load');
Expand All @@ -223,7 +224,7 @@ window.submitForm = function (form, possibleClickedButton) {
window.onpopstate = function (event) {
window.location.reload();
};
} else if (urlPathnameWithQuery !== responseUrlPathName) {
} else if (urlPathnameWithQuery !== responseUrlPath) {
history.replaceState({}, "", request.responseURL)
};

Expand Down

0 comments on commit f1ffc95

Please sign in to comment.