Pages behind a sign-in
A Récit site is prerendered: every page is an HTML file written at build time, the same for every visitor. The application mode adds pages that are not: their routes are rendered for each request, after a guard that decides who may enter, with data read for that request only. Nothing a visitor sees ever lands in a built file.
It goes through one plugin in recit.. A site without it builds exactly the same files as
before.
When to use it
- A page for signed-in people only: an account, a team's notes, a dashboard of one customer.
- A page whose content depends on who reads it, read from an API at each visit.
For a page everyone may read, write Markdown as usual. For a small lock over a whole static site, with no server, see Protect a site with a password: it is a modal in the browser, not a guard.
The plugin
An application is an AppPlugin, exported by @recit/, in the plugins of the site:
recit.config.ts
It has three parts, of which only the routes are required:
| Part | Receives | Returns | At prerender | At each request |
|---|---|---|---|---|
getAppRoutes | nothing | React Router routes | one shell per root route | the route, after the guard |
guard | the request, the params | { allow: or { redirect } | never called | called, cookies included |
createData | the request (on the server) | the value useAppData reads | never called | a new value, in a React context |
app/plugin.tsx
The whole example, with its sign-in page and its test backend, is examples/.
The routes
getAppRoutes returns React Router routes, built by a function, never at
module level. They are never prerendered, and never enter the site's plan, its search index, its
llms.txt or its Markdown export.
- Give every root route a
HydrateFallback: what shows while the route loads. The build writes one shell per root route, the site's layout and this fallback, markednoindex, with no guard and no data. It sits at an address its route matches (/teams/:givesteamId /teams/__), and the host serves it for every address of the route. A child route uses its parent's shell.shell.html - The build stops when a shell would land on a prerendered page, or when two root routes share one shell.
handle:on a route that draws its own frame; without it, the page sits in the site's layout, header and sidebar included.{ layout: "none" }
The guard
guard runs before the loader and the action of every route of the plugin:
- On the server, with the real request, cookies included;
{ redirect:leaves as a"/ login? returnTo= …" } 302. - In the browser, at each navigation and when a shell first loads.
It keeps nothing between two calls. A page or a redirect of the application is sent with
Cache-. A shell is never hydrated: the browser replaces it with a fresh
render of the route, after its guard.
Put public pages of the application (sign-in, a password link, an error page) either before the
check in the guard, as the example does for /login, or in a second plugin with no guard. Bring
the visitor back where they were going: pass the path in returnTo, and accept only a path of the
site (/notes, never //example.com).
The guard decides who sees a page, not what the data says: an API should answer « not found » to someone outside the resource, and the page should show it that way.
The data
createData gives one value per request on the server and one per tab in the browser. A
page reads it with useAppData<Data>() from @recit/:
app/plugin.tsx
Never keep anything of a visitor in a module variable, a top-level query client or a shared
singleton: the same server answers every visitor. A prerendered page has no data (createData is
not called at build time); the browser mounts it after hydration.
Hosting
A guarded page needs a server: the guard reads the request, which a static file cannot. Build the site for server rendering, with one of the adapters of the site framework:
Bash
node is the default: it writes a server (Hono) that serves the prerendered pages and renders the
application's routes. A site of its own can replace that entry with a recit. next to its
recit..
A static build still serves the public pages and the shells, so a visitor sees the loading state, then the guard runs in the browser: enough for a demonstration, never for a page whose content must stay private.
Test it
e2e/is the model: the public page, a visit without a session sent to sign in and back, a visit with a session, and no application route in the prerendered files.site- app. spec.ts - Give each page component a story whose
playreads the page by roles and names, with its data mocked.
See also
- Protect a site with a password, the lock with no server.
- Récit account, the platform's own application.
recit.configreference, the site options.