--- description: | Partials allow areas of a page to be updated without causing the browser to reload the page. They enable optimized fine grained UI updates and can be used to do client-side navigation. --- Partials allow areas of the page to be updated with new content by the server without causing the browser to reload the page. They make your website feel more app-like because only the parts of the page that need to be updated will be updated. ## Enabling partials Partials are enabled by adding a `f-client-nav` attribute to an HTML element and wrapping one or more areas in the page with a ``-component. The quickest way to get started is to enable partials for every page in `routes/_app.tsx` by making the following changes. ```diff routes/_app.tsx import { define } from "../utils.ts"; + import { Partial } from "sweet/runtime"; export default define.page(function App({ Component }) { return ( My Fresh app - + + + ); }); ``` By adding the `f-client-nav` attribute, we enable partials for every element beneath the ``-tag. To mark an area of the page as a partial we wrap it with a ``-component with a unique name. Behind the scenes, when the user clicks an ``-tag, Fresh fetches the new page and only pulls out the relevant content out of the HTML response. When it finds a matching partial area it will update the content inside the partial. > [info]: The `name` prop of the `` component is expected to be unique > among Partials. That's how Fresh knows which parts of the response need to go > on the current page. > [info]: Passing `f-client-nav={false}` disables client side navigation for all > elements below the current node. ### Optimizing partial requests By default, with `f-client-nav` set, Fresh fetches the full next page and only picks out the relevant parts of the response. We can optimize this pattern further by only rendering the parts we need, instead of always rendering the full page. This is done by adding the `f-partial` attribute to a link. ```diff routes/_app.tsx - Routes + Routes ``` When the `f-partial` attribute is present, Fresh will navigate to the page URL defined in the `href` attribute, but fetch the updated UI from the URL specified in `f-partial` instead. This can be a highly optimized route that only delivers the content you care about. Let's use a typical documentation page layout as an example. It often features a main content area and a sidebar of links to switch between pages of the documentation (marked green here). ![A sketched layout of a typical documentation page with the sidebar on the left composed of green links and a main content area on the right. The main content area is labeled as Partial docs-content](/docs/fresh-partial-docs.png) The code for such a page (excluding styling) might look like this: ```tsx routes/docs/[id].tsx import { define } from "../../utils.ts"; export default define.page(async (ctx) => { const content = await loadContent(ctx.params.id); return (
{content}
); }); ``` An optimal route that only renders the content instead of the outer layout with the sidebar might look like this respectively. ```tsx routes/partials/docs/[id].tsx import { define } from "../utils.ts"; import { Partial } from "sweet/runtime"; // We only want to render the content, so disable // the `_app.tsx` template as well as any potentially // inherited layouts export const config: RouteConfig = { skipAppWrapper: true, skipInheritedLayouts: true, }; export default define.page(async (ctx) => { const content = await loadContent(ctx.params.id); // Only render the new content return ( {content} ); }); ``` By adding the `f-partial` attribute we tell Fresh to fetch the content from our newly added `/partials/docs/[id].tsx` route. ```diff routes/docs/[id].tsx ``` With this in place, Fresh will navigate to the new page when clicking any of the two links and _only_ load the content rendered by our optimized partial route. > Currently, `f-partial` is scoped to ``, ` nothing submitted yet ); }); ``` A form that uses another method is always submitted as a partial request. It doesn't need an `action` or `f-partial` attribute. Without them, the request goes to the current page. You can write the method in any case: Fresh converts it to uppercase, so `formmethod="patch"` reaches your `PATCH` handler. The `POST` button above only submits as a partial because the form has an `action` attribute. Without it, `POST` would cause a full page load, while the `DELETE` and `PATCH` buttons would still work as partials. 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 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 ` ))}
); } export default define.page(async () => { const todos = await getTodos(); return (
); }); ``` When `f-method` is set to something other than `GET`, the request body is `multipart/form-data` and contains: - the button's own `name` and `value`, if it has a `name` - every field of the surrounding form, if the button is a `type="button"` inside a `` A `GET` button doesn't send any data. Like with forms, you can write the method in any case. To ask for confirmation first, call `preventDefault()` on the click event in an island. Fresh won't send the request when the event was cancelled. ```tsx islands/DeleteButton.tsx export function DeleteButton(props: { id: string }) { return ( ); } ``` [Loading indicators](#loading-indicators) work on these buttons too, and the same advice about redirects applies. Buttons with `f-partial` rely on JavaScript, so they do nothing if JavaScript isn't loaded. ## Sending multiple Partials at the same time A neat aspect of partials in Fresh is that a response can return as many partials as desired. That way you can update multiple unrelated areas on your page in one single HTTP response. A scenario where this is useful are online shops for example. ```tsx routes/partials/cart.tsx export default function AddToCartPartial() { return ( <> {/* Render the new cart item here */}

Total: {totalPrice} €

); } ``` Both partials will be applied to the current page. ## Replacement mode By default the whole content inside a partial will be replaced, but there are scenarios where you want to prepend or append new content instead. This can be achieved by adding the `mode` prop to a `Partial` component. - `replace` - Swap out the content of the existing partial (default) - `prepend` - Insert the new content before the existing content - `append` - Insert the new content after the existing content Personally, we’ve found that the `append` mode is really useful when you have an UI which displays log messages or similar list-like data. ```tsx routes/log.tsx export default function LogView() { const lines = getNewLogLines(); return ( {lines.map((line) => { return
  • {line}
  • ; })}
    ); } ``` > [warn]: When using `prepend` or `append` mode, you **must** add a `key` prop > to the `` component. Without it, Preact cannot distinguish new > children from existing ones, leading to subtle rendering bugs. Fresh will log > a warning if it detects a missing key on an append/prepend partial. ## View Transitions Partial updates can be animated using the browser's [View Transitions API](/docs/advanced/view-transitions). Add `f-view-transition` alongside `f-client-nav` to enable smooth animated transitions between pages with zero JavaScript animation code. ## Loading indicators When a partial request is in flight, you may want to show a loading spinner or disable a button. Fresh supports this through the `_freshIndicator` property. Attach an object with a `value` property to any element that triggers a partial navigation. Fresh will set `value` to `true` when the request starts and back to `false` when it completes (or fails). ```tsx import { useSignal } from "@preact/signals"; function NavLink() { const loading = useSignal(false); return (
    { if (el) el._freshIndicator = loading; }} > {loading.value ? "Loading..." : "Go"} ); } ``` This works for links, forms, and submit buttons. For form submissions, Fresh checks the submitter element (e.g. the clicked button) first, then falls back to the form element itself. This lets you show per-button indicators when a form has multiple submit buttons. ```tsx import { useSignal } from "@preact/signals"; function MyForm() { const saving = useSignal(false); return ( {/* indicator is on the button, not the form */} ); } ``` > [info]: Any object with a mutable `value` property works — Preact signals are > the most convenient choice because they automatically re-render the component > when the value changes. ## Bypassing or disabling Partials If you want to exempt a particular element from triggering a partial request like on a particular link, form or button, you can opt out of it by setting `f-client-nav={false}` on the element or one of the ancestor elements. ```tsx routes/_app.tsx {/* This will cause a partial navigation */} With partials {/* This WONT cause a partial navigation */} No partials {/* This WONT cause a partial navigation on any elements below */}
    No partials
    ; ``` Whenever an element is clicked Fresh checks if it has the `f-client-nav` attribute and if it is set to `true`. If the element itself doesn't have such an attribute, it will check if any of the ancestor elements has it. If an element was found with a truthy `f-client-nav` attribute a partial request will be triggered. If there is no such attribute or if it's set to `false`, no partial request will occur.