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; return;
} }
const action = e.submitter?.getAttribute(PARTIAL_ATTR) ?? const rawPartialUrl = e.submitter?.getAttribute(PARTIAL_ATTR) ??
e.submitter?.getAttribute("formaction") ?? e.submitter?.getAttribute("formaction") ??
el.getAttribute(PARTIAL_ATTR) ?? el.action; el.getAttribute(PARTIAL_ATTR) ?? el.action;
const rawActionUrl = e.submitter?.getAttribute("formaction") ?? el.action;
if (action !== "") { if (rawPartialUrl !== "") {
e.preventDefault(); 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; let init: RequestInit | undefined;
@@ -250,13 +252,13 @@ document.addEventListener("submit", async (e) => {
// TODO: Looks like constructor type for URLSearchParam is wrong // TODO: Looks like constructor type for URLSearchParam is wrong
// deno-lint-ignore no-explicit-any // deno-lint-ignore no-explicit-any
const qs = new URLSearchParams(new FormData(el) as 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 { } else {
init = { body: new FormData(el), method: lowerMethod }; init = { body: new FormData(el), method: lowerMethod };
} }
maybeUpdateHistory(url); maybeUpdateHistory(actionUrl);
await fetchPartials(url, init); await fetchPartials(partialUrl, init);
} }
} }
}); });
+3
View File
@@ -1484,6 +1484,9 @@ Deno.test({
await page.locator(".update").click(); await page.locator(".update").click();
await page.locator(".done-foo").wait(); await page.locator(".done-foo").wait();
const pathname = await page.evaluate(() => window.location.pathname);
expect(pathname).toEqual("/foo");
}); });
}, },
}); });