diff --git a/docs/latest/advanced/partials.md b/docs/latest/advanced/partials.md index e5070d34..c7f5a1c5 100644 --- a/docs/latest/advanced/partials.md +++ b/docs/latest/advanced/partials.md @@ -210,15 +210,42 @@ The `POST` button above only submits as a partial because the form has an Keep the following in mind when using other methods: +- **The URL doesn't change:** Unlike `GET` and `POST`, these methods leave the + URL in the address bar as it is. See + [Controlling the URL](#controlling-the-url). - **Redirects:** To redirect after a `DELETE`, `PATCH` or `PUT`, respond with status `303 See Other`. With a `301` or `302`, `fetch()` sends the request to - the new URL again with the same method. + the new URL again with the same method. The partials from the redirect target + are applied, but the URL only changes to it if you set `f-update-url="true"`. - **Without JavaScript:** If JavaScript isn't loaded, the browser doesn't recognize the method and submits the form as a `GET` request instead, with the fields in the URL. If a form has to work without JavaScript, use `POST`. - **CSRF protection:** The [CSRF middleware](/docs/plugins/csrf) treats these methods like `POST`, so they get the same checks. +### Controlling the URL + +When a `GET` or `POST` form is submitted as a partial, Fresh replaces the URL in +the address bar with the form's action URL, or with the URL it was redirected +to. Other methods leave the URL as it is, so a `DELETE` to `/items/1` doesn't +leave `/items/1` in the address bar. + +To change this, set `f-update-url` on the form or on the submit button. The +button's value wins if both have one. + +- `f-update-url="true"` updates the URL, for any method +- `f-update-url="false"` leaves the URL as it is, for any method + +```tsx +
+ + +
; +``` + +> [info]: Use the strings `"true"` and `"false"`. Preact leaves the attribute +> out when you pass the boolean `{false}`, so Fresh falls back to the default. + ### Sending requests from buttons A ` @@ -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( + +
+
+ + +

init

+
+ + +
+
+
, + ); + }); + + 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 () => {