How it works

When and where @k8ordo/framework renders pages and answers requests, and what it does and does not guarantee.

On this page

Handler calls

text
mode: 'static'
  vite build → handler(/products/1)           → index.html
             → handler(/products/1/index.rsc) → index.rsc

mode: 'server'
  request    → handler(request)               → Response

Under either mode, vite build writes a request handler to dist/rsc/index.js: a function that takes a Request and returns a Response.

Under static mode the build calls the handler once per page at its end, and writes the HTML it answered to index.html and the payload that HTML was rendered from to index.rsc. A page that redirects gets only index.html, and a route.ts is written as the one file its GET answers. Under server mode the handler is called for every request, and its response is returned.

vite dev calls the handler for every request under either mode, and writes no files. For how development differs from the build, see Troubleshooting.

HTML and the payload

text
/products/1            → HTML
/products/1/index.rsc  → RSC payload

Under either mode a page has two forms: HTML and an RSC payload. The payload’s URL is the page’s path with /index.rsc appended. It sits at a path rather than behind a header or a query because a static host varies its answer on neither.

A client navigation fetches that payload. The first page opened carries the payload it was rendered from inside its HTML. Hydration reads that, so the same page is never fetched twice.

page.tsx, not-found.tsx, route.ts and fallback.tsx are read when a request reaches them. The Client Components such a file renders and the CSS it imports, its Server Components’ CSS Modules included, are loaded when a page that renders it is opened. The Client Components are loaded as well when a link to such a page is fetched ahead. What a layout.tsx, error.tsx or loading.tsx imports, every page below it loads.

What every page loads comes in at most two files. One is the entry’s, with React, the router and the framework. The other is the frame’s, with the Client Components of every layout.tsx, error.tsx and loading.tsx and the framework’s own boundaries. A module a page shares with either goes into it. What pages share only among themselves stays in files of their own.

The framework sets the client build’s codeSplitting to do this. A manualChunks the application writes for the client environment has no effect, and a codeSplitting group of its own takes a module from those two files only with a priority above 2.

The first page opened hydrates once the client components it renders have loaded. A client navigation, too, changes the screen once the next page’s client components have loaded.

Refusals under static mode

vite build
static build cannot run guard.ts — a file has no request to guard, and these are guards:
  src/routes/admin/guard.ts
this application wants mode: 'server'

A static mode application still has the server features installed. A file the build writes has no request behind it, so using one under static mode fails the build.

vite dev is a server that receives requests, and could run a guard.ts or a Server Action. It reports the same error as the build anyway, so that no code works in development and does nothing in production.

What is refused, and when the build reports it, is listed on Choosing a mode.

Refusals under server mode

Under server mode, the server takes requests from anywhere. The handler turns away the ones the application should not answer before any guard.ts runs, so neither a guard nor a Server Action runs for them.

A Server Action can be called by anything that can send a POST, another site’s form included. A POST whose Origin names another host is turned away so that no such form calls an action with the visitor’s cookies.

Guarantees

  • Under either mode the same handler renders the same routes/. The routes/ conventions and the 'use client' boundary do not change with the mode.
  • A payload that does not match the script the tab is running is not rendered: the URL is loaded again as a document.
  • Under static mode, the build refuses a guard.ts, a 'use server' module and an import of @k8ordo/framework/server in the application’s own modules before any page is written.
  • Under static mode, a page whose Server Component throws is not written, and the build stops, naming its URL.
  • Under server mode, a POST to a page’s URL gets a 403 when it has no Origin header, or when that header names another host.
  • Under server mode, a method other than GET, HEAD and POST gets a 405 whose Allow lists those three. A URL outside Vite’s base gets a 404.
  • With @k8ordo/i18n’s params exported from src/routes/[locale]/layout.tsx, and not-found.tsx placed under [locale] too, a URL renders in the same locale on the server and in the browser.
  • The only locale a render is given is the one params accepted from its URL. A locale accepted for a pattern that was then refused does not carry over, and neither does the locale of a page the same build rendered earlier.
  • In <head>, the Server Components’ CSS comes before the Client Components’ CSS, the root layout’s first. That holds for a page loaded as a document, one reached by a client navigation, and what streams after the shell under server mode.
  • An inline script written in <head> with no src and no type is moved ahead of every stylesheet there, in the order it was written. That holds under either mode and under vite dev.
  • The JavaScript and CSS the browser runs are built for the browsers that support every Baseline newly available feature, so light-dark() and :dir() in the CSS stay as written. A build.target, build.cssTarget or css.lightningcss.targets the application sets is used as set.

Not guaranteed

  • Under static mode too, vite dev does not give what the build gives. A redirect.ts is answered with a 307 or a 308, and a Server Component that throws gets a 500.
  • A dependency in node_modules that imports @k8ordo/framework/server is not named by a static build. It fails once the dependency calls cookies() or the like.
  • A static build cannot write a status. Whether 404.html is served with a 404 is the host’s setting, and a redirect.ts becomes a page that sends the visitor on.
  • A POST to a route.ts has its Origin unchecked. Whatever it needs checked, the route.ts checks itself.
  • The handler reads no forwarding header such as X-Forwarded-Host. It compares Origin with the host of the URL of the Request it is handed.
  • Under static mode, 404.html is written in the default locale even when not-found.tsx sits under [locale]. A visitor without JavaScript sees that text whatever locale the URL names.
  • Under server mode, the CSS and JS of a client component that is only held in a prop’s Map, Set, promise, iterator, async iterable or ReadableStream, and never rendered, are written only into the HTML of the request that first loads its module in the process.
  • The order among the Client Components’ CSS is not guaranteed. A static file lists it in the order the RSC render named it, and a client navigation adds each as it loads.
  • CSS once loaded is never taken out. What an earlier page loaded, and the Client Components’ CSS a link fetched ahead loaded, apply to every page the tab moves to.
k8ordo

React libraries that use Baseline features without holding back.