fix: wrong url after partial form submission (#2495)

Fixes https://github.com/denoland/fresh/issues/2409
This commit is contained in:
Marvin Hagemeister
2024-06-05 08:30:28 +02:00
committed by GitHub
parent a9b552945a
commit f8b60ddd15
2 changed files with 11 additions and 6 deletions
+8 -6
View File
@@ -234,14 +234,16 @@ document.addEventListener("submit", async (e) => {
return;
}
const action = e.submitter?.getAttribute(PARTIAL_ATTR) ??
const rawPartialUrl = e.submitter?.getAttribute(PARTIAL_ATTR) ??
e.submitter?.getAttribute("formaction") ??
el.getAttribute(PARTIAL_ATTR) ?? el.action;
const rawActionUrl = e.submitter?.getAttribute("formaction") ?? el.action;
if (action !== "") {
if (rawPartialUrl !== "") {
e.preventDefault();
const url = new URL(action, location.href);
const partialUrl = new URL(rawPartialUrl, location.href);
const actionUrl = new URL(rawActionUrl, location.href);
let init: RequestInit | undefined;
@@ -250,13 +252,13 @@ document.addEventListener("submit", async (e) => {
// TODO: Looks like constructor type for URLSearchParam is wrong
// deno-lint-ignore no-explicit-any
const qs = new URLSearchParams(new FormData(el) as any);
qs.forEach((value, key) => url.searchParams.set(key, value));
qs.forEach((value, key) => partialUrl.searchParams.set(key, value));
} else {
init = { body: new FormData(el), method: lowerMethod };
}
maybeUpdateHistory(url);
await fetchPartials(url, init);
maybeUpdateHistory(actionUrl);
await fetchPartials(partialUrl, init);
}
}
});