f-update-url

This commit is contained in:
2026-11-11 17:16:21 -03:00
parent 3866b47741
commit 8c552d05d1
3 changed files with 100 additions and 3 deletions
@@ -26,6 +26,7 @@ import { INTERNAL_PREFIX, PARTIAL_SEARCH_PARAM } from "../../constants.ts";
export const PARTIAL_ATTR = "f-partial";
export const METHOD_ATTR = "f-method";
export const UPDATE_URL_ATTR = "f-update-url";
class NoPartialsError extends Error {}
@@ -341,9 +342,16 @@ document.addEventListener("submit", async (e) => {
};
}
// GET and POST update the URL by default, other methods don't
const updateUrl = e.submitter?.getAttribute(UPDATE_URL_ATTR) ??
el.getAttribute(UPDATE_URL_ATTR);
const shouldNavigate = updateUrl === null
? !isCustomMethod
: updateUrl !== "false";
try {
await withViewTransition(async () => {
await fetchPartials(actionUrl, partialUrl, true, init);
await fetchPartials(actionUrl, partialUrl, shouldNavigate, init);
});
} finally {
if (indicator !== undefined) {
+63 -1
View File
@@ -2128,7 +2128,12 @@ Deno.test({
<Partial name="foo">
<p class="init">init</p>
</Partial>
<button type="submit" class="update" formmethod="DELETE">
<button
type="submit"
class="update"
formmethod="DELETE"
f-update-url="true"
>
delete
</button>
</form>
@@ -2150,6 +2155,63 @@ Deno.test({
},
});
Deno.test({
name: "partials - submit form f-update-url",
fn: async () => {
const app = testApp()
.delete("/items/1", renderMethodPartial)
.post("/partial", renderMethodPartial)
.get("/", (ctx) => {
return ctx.render(
<Doc>
<div f-client-nav>
<form>
<input name="name" value="foo" />
<Partial name="foo">
<p class="init">init</p>
</Partial>
<button
type="submit"
class="delete"
formaction="/items/1"
formmethod="DELETE"
>
delete
</button>
<button
type="submit"
class="post"
formaction="/partial"
formmethod="POST"
f-update-url="false"
>
post
</button>
</form>
</div>
</Doc>,
);
});
await withBrowserApp(app, async (page, address) => {
await page.goto(address, { waitUntil: "load" });
await page.locator(".init").wait();
// Custom methods leave the URL alone by default
await page.locator(".delete").click();
await page.locator(".done-DELETE-foo").wait();
let pathname = await page.evaluate(() => window.location.pathname);
expect(pathname).toEqual("/");
// f-update-url="false" opts POST out of updating the URL
await page.locator(".post").click();
await page.locator(".done-POST-foo").wait();
pathname = await page.evaluate(() => window.location.pathname);
expect(pathname).toEqual("/");
});
},
});
Deno.test({
name: "partials - submit form with formmethod HEAD is sent as GET",
fn: async () => {