@rsc-kit/mcp
Version:
An MCP server over what an rsc-kit build decided: the routes, why each one is static or not, and what it costs the browser.
170 lines (125 loc) • 5.48 kB
Markdown
# Sections
> Refreshing one region of a page without re-rendering the rest.
A section is a part of a page that an action can refresh on its own. Name a file
`<name>.section.tsx`:
```tsx title="src/app/orders/orders.section.tsx"
import { section } from '@rsc-kit/core/section'
async function OrdersList() {
const orders = await db.orders()
return (
<ul>
{orders.map((o) => <li key={o.id}>{o.reference}</li>)}
</ul>
)
}
const Orders = section('orders', OrdersList)
export default Orders
```
Then `revalidate('orders')` in an action renders that part again, and nothing
else.
## Defining a section
The name passed to `section()` must match the file: `orders.section.tsx` is the
section `orders`. The renderer finds a section by its file and the browser by
its name, so a section whose two names differ would refresh nothing: the build refuses
one, and says which file to rename.
Assign the section to a variable before exporting it, as above, rather than
writing `export default section(...)`. An editor names an import after the
default export, and a call has no name: it falls back to one made from the file
name, `OrdersSection`, so typing `<Orders` finds nothing to import.
Render it like any other component:
```tsx title="src/app/orders/page.tsx"
import Orders from './orders.section'
export default function OrdersPage() {
return (
<>
<h1>Orders</h1>
<Orders />
</>
)
}
```
## Refreshing it
An action names what it changed, and only that section is rendered again:
```tsx
'use server'
import { revalidate } from '@rsc-kit/core/revalidate'
export async function placeOrder(form: FormData) {
await db.orders.create({ reference: String(form.get('reference')) })
revalidate('orders')
}
```
The rest of the page is not rendered or fetched again, so state outside the
section, such as a half-filled form beside it, stays as it was.
With a backend, the backend names it: Go's `rsckit.Revalidate(ctx, "orders")`
or Laravel's `Rsc::revalidate('orders')`.
### When the calling page has no such section
An action says what it changed, not which page called it. When the page that
called it has no `orders` section, its own or one beside its layouts, there is
nothing on screen to refresh: the name is skipped and the action still answers.
A name that no page in the app has is a mistake. The action fails, naming the
sections and slots the calling page does have.
## A section in a layout
A layout stays mounted while you move between the pages under it. Put what every
tab shares there, such as a title, a status or the tab bar, and make it a section
when it shows data that changes:
```tsx title="src/app/[team]/[app]/layout.tsx"
import { Suspense } from 'react'
import Header from './header.section'
export default function AppLayout({ params, children }) {
return (
<>
<Suspense fallback={<HeaderSkeleton />}>
<Header params={params} />
</Suspense>
{children}
</>
)
}
```
```tsx title="src/app/[team]/[app]/header.section.tsx"
import { section } from '@rsc-kit/core/section'
async function AppHeader({ params }) {
const { team, app } = await params
const status = await db.status(team, app)
return <header>{status}</header>
}
const Header = section('header', AppHeader, {
refreshOn: ({ params }) => [`app:${params.app}`],
})
export default Header
```
The header reads `params`, so it has its own `<Suspense>`: the layout paints the
skeleton at once and the header streams in. A layout that awaits `params` above
every boundary is refused at build, on a route that lists no URLs.
`revalidate('header')` from an action on any page under the layout refreshes it.
A tab switch then replaces only the page, and the header is already there.
- **Names are looked up nearest first.** A page's sections are the ones beside
it, then the ones beside each of its layouts. If a page and its layout both
have a `header`, a refresh renders the page's.
- **A refresh renders the section with the page's props**, `params` and
`searchParams`, not the ones the layout passed. Pass `params` through, as
above: a page's `params` hold every segment the layout's do.
- **A refresh keeps the section on screen until it is ready.** The new content
is swapped in as a transition, so the section doesn't fall back to a skeleton
on every save.
- **A refresh lasts as long as its layout.** Moving to another page under the
same layout keeps it. A navigation to a page under a different layout renders
the section again.
- **Mark the active tab with `usePathname()`**, in a client component inside the
section. It follows the URL on every navigation, with no refresh.
## Refreshing on a change from outside
An action refreshes the sections it changed. For a change made anywhere else,
such as a webhook, a job or another visitor, a section can name the data it
shows and refresh in every open tab:
```tsx
section('repos', Repos, { refreshOn: ['repos'] })
```
See [Live data](/guides/live-data).
## How names are found
Two pages may both call a section `orders`. A name is looked up only among the
sections beside the page and its layouts, never in a table of every section in
the app. So a refresh can only reach regions of the page that asked for it,
behind that page's own guards.
A section is not a cache boundary and not a client component. It renders on the
server like everything else, and it can be rendered on its own, without the
page around it.