Button partials with any methods

This commit is contained in:
2026-11-11 16:56:47 -03:00
parent 8400c019e0
commit 3866b47741
3 changed files with 417 additions and 15 deletions
+41 -15
View File
@@ -25,6 +25,7 @@ import { parse } from "../../jsonify/parse.ts";
import { INTERNAL_PREFIX, PARTIAL_SEARCH_PARAM } from "../../constants.ts";
export const PARTIAL_ATTR = "f-partial";
export const METHOD_ATTR = "f-method";
class NoPartialsError extends Error {}
@@ -112,6 +113,12 @@ function maybeReplaceHistory(nextUrl: URL) {
}
}
function normalizeMethod(raw: string | null | undefined): string {
const method = (raw || "get").toUpperCase();
// Browsers submit invalid methods like HEAD as GET
return method === "HEAD" ? "GET" : method;
}
document.addEventListener("click", async (e) => {
let el = e.target;
if (el && (el instanceof HTMLElement || el instanceof SVGElement)) {
@@ -178,20 +185,38 @@ document.addEventListener("click", async (e) => {
}
} else if (
el && el instanceof HTMLButtonElement &&
(el.type !== "submit" || el.form === null)
(el.type !== "submit" || el.form === null) &&
!e.defaultPrevented
) {
const partial = el.getAttribute(PARTIAL_ATTR);
if (partial === null || !checkClientNavEnabled(el)) return;
// Check if the user opted out of client side navigation.
if (partial === null || !checkClientNavEnabled(el)) {
return;
const method = normalizeMethod(el.getAttribute(METHOD_ATTR));
const partialUrl = new URL(partial, location.href);
let init: RequestInit | undefined;
if (method !== "GET") {
const body = el.form ? new FormData(el.form) : new FormData();
if (el.name) {
body.append(el.name, el.value);
}
init = { method, body };
}
const partialUrl = new URL(
partial,
location.href,
);
await fetchPartials(partialUrl, partialUrl, false);
// deno-lint-ignore no-explicit-any
const indicator = (el as any)._freshIndicator;
if (indicator !== undefined) {
indicator.value = true;
}
try {
await withViewTransition(() =>
fetchPartials(partialUrl, partialUrl, false, init)
);
} finally {
if (indicator !== undefined) {
indicator.value = false;
}
}
}
}
});
@@ -262,14 +287,15 @@ document.addEventListener("submit", async (e) => {
return;
}
const method = e.submitter?.getAttribute("formmethod")?.toLowerCase() ||
el.getAttribute("method")?.toLowerCase() || "get";
const method = normalizeMethod(
e.submitter?.getAttribute("formmethod") || el.getAttribute("method"),
);
if (method === "dialog") {
if (method === "DIALOG") {
return;
}
const isCustomMethod = method !== "get" && method !== "post";
const isCustomMethod = method !== "GET" && method !== "POST";
const hasExplicitPartial = isCustomMethod ||
e.submitter?.hasAttribute(PARTIAL_ATTR) ||
@@ -303,7 +329,7 @@ document.addEventListener("submit", async (e) => {
let init: RequestInit | undefined;
// GET method appends form data via url search params
if (method === "get") {
if (method === "GET") {
// TODO: Looks like constructor type for URLSearchParam is wrong
// deno-lint-ignore no-explicit-any
const qs = new URLSearchParams(new FormData(el, e.submitter) as any);
@@ -311,7 +337,7 @@ document.addEventListener("submit", async (e) => {
} else {
init = {
body: new FormData(el, e.submitter),
method: method?.toUpperCase(),
method,
};
}