f-update-url
This commit is contained in:
@@ -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
|
||||
<form action="/search" f-update-url="false">
|
||||
<input name="q" />
|
||||
<button type="submit">Search</button>
|
||||
</form>;
|
||||
```
|
||||
|
||||
> [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 `<button>` with an `f-partial` attribute also triggers a partial request on
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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 () => {
|
||||
|
||||
Reference in New Issue
Block a user