From 21fcf6169bd84f2eb1e38fa76f85998ac1b6030c Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Fri, 6 Oct 2023 22:55:54 +0200 Subject: [PATCH] docs: add partials documentation (#1878) --- docs/canary/concepts/partials.md | 198 +++++++++++++++++++++++++ docs/toc.ts | 1 + www/static/docs/fresh-partial-docs.png | Bin 0 -> 129914 bytes 3 files changed, 199 insertions(+) create mode 100644 docs/canary/concepts/partials.md create mode 100644 www/static/docs/fresh-partial-docs.png diff --git a/docs/canary/concepts/partials.md b/docs/canary/concepts/partials.md new file mode 100644 index 00000000..ae2a4034 --- /dev/null +++ b/docs/canary/concepts/partials.md @@ -0,0 +1,198 @@ +--- +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 +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 { AppProps } from "$fresh/server.ts"; ++ import { Partial } from "$fresh/runtime.ts"; + + export default function App({ Component }: AppProps) { + 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. + +> ℹ️ 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. + +> ℹ️ Passing `f-client-nav={false}` disables client side navigation and partials +> for the current elements and the elements below it. + +### 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 ++ 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 +export default defineRoute(async (req, ctx) => { + const content = await loadContent(props.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 partials/docs/[id].tsx +import { defineRoute, RouteConfig } from "$fresh/server.ts"; +import { Partial } from "$fresh/runtime.ts"; + +// 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 defineRoute(async (req, ctx) => { + const content = await loadContent(props.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 ``, `