elements
Blog Demos UI Docs Pricing
Elements Manual
  • Getting Started
  • CLI
  • Build
  • Browser
  • Packages
  • TypeScript
  • Elements HTML
    • Templates
    • Attributes
    • Directives
    • Events
    • Reactivity
    • Server Rendering
    • RPC and LiveTable in Templates
  • Assets
  • RPC
  • Router
  • Session
  • Database
  • Migrations
  • LiveTable
  • Channel
  • Jobs
  • Email
  • Payments
  • JSON
  • Async
  • Style
  • UI
  • JSOC
  • Config
  • Tests
  • Logs
  • Deploy
  • Editors
  • Recipes
  • Licenses
Manual / Elements HTML/ Server Rendering

Server Rendering

elements man html/server

How a page is rendered on the server and picked up by the browser runtime, how to redirect, and how HTML responses are cached.

Pages render on the server and attach in the browser. Pass data into the template through the route handler. The shape is new html({ ... }).

Route handler
export default function route(req: Request, res: Response) { let users = sql<User>(`select * from users`).all(); return new html({ users }); }

Usually you'll want to provide all the data needed to render the page from the route. This ensures the page is fully server-rendered. Occasionally you may want to delay data fetching until after the initial page renders; in that case you can call an @rpc function from an oninsert handler. After first render, @rpc and LiveTable are the right tools for browser-initiated data.

Redirect

redirect(url) leaves the current page for another url. Use it in place of assigning window.location directly.

It is one call everywhere: a template handler, a route, middleware, and an @rpc all take the same redirect(url). In the browser it drives window.location; in a route or middleware it writes a 302; in an @rpc the target rides back on the reply and the browser navigates once the call settles.

Example
import { redirect } from "@elements/app"; function onSubmit(form: SigninForm) { try { signin(form.email, form.password); form.error = ""; redirect("/"); } catch (err: any) { form.error = err.message; } }

Navigation is asynchronous: the browser fetches the next page while this one stays live, so a reactive change made just before you leave repaints the current page before it unloads. This matters most for auth. signin above is an @rpc, so the compiler awaits it; session.login runs on the server and the browser applies the session update, flipping session.isLoggedIn(), while the await is still pending, before the redirect(url) line runs.

So render the signin page as the form alone, and send an already signed-in visitor on from the route. A page that swaps between the form and a "you're signed in" panel based on session.isLoggedIn() would paint that panel where the form was for the moment before the new page loads.

Signin template
<!-- Don't: the page body swaps to a signed-in view the instant login lands, one frame before redirect() leaves. --> <main> <div e:if={session.isLoggedIn()}>You're signed in. <a href="/">Go home</a></div> <form e:else onsubmit={() => onSubmit(form)}> ... </form> </main> <!-- Do: the page is the form. A signed-in visitor is handled at the route. --> <main> <form onsubmit={() => onSubmit(form)}> ... </form> </main>

Handle an already-signed-in visitor in the route, before the page renders:

Route handler
export default function route(req: Request, res: Response) { if (session.isLoggedIn()) { redirect("/"); return; } return new html({}); }

A session-reactive navbar is fine. It flips to the signed-in state and the destination shows that same state, so the change carries across the navigation rather than flashing.

redirect() is not control flow. In the browser navigation is asynchronous and the code after it keeps running; on the server the handler keeps running too, and a value returned afterward is discarded rather than written over the redirect. Return early when you want the rest skipped.

redirect() always sends a 302. For any other status, a route can use the http-specific res.redirect(url, 301).

It throws where there is no browser to navigate: a job, a test, module top level, or after a response has already started streaming.

HTTP Caching

Caching is correct by default, so a page that changes rarely loads from cache without any setup, and a page that must not be shared never is.

Every HTML response carries an etag computed from the page's source, the data passed into the template, and the session state. The default Cache-Control is public, no-cache: a browser or CDN may store the page but revalidates it on every visit, and an unchanged page returns 304 Not Modified with no body. A revisit skips both rendering and transfer when nothing relevant has changed. The page response itself doubles as the cache manifest.

Every script, stylesheet, image and font the build emits has a content hash in its URL and is served with public, max-age=15552000, immutable, six months. A browser loads them from its own cache on every visit after the first, and a deploy that changes a file changes its URL (see elements man assets).

A page that changes rarely can stay in cache without revalidating. Set the header in the route and return the page:

app/pages/pricing/index.ts
export default function route(req: Request, res: Response) { res.setHeader("Cache-Control", "public, max-age=2592000"); return new html({ plans: sql<Plan>(`select name, price from plans`).all() }); }

The header is a request, and the response data sets the floor. An anonymous page whose data carries no session or live state gets exactly the header the route asked for. A page for a signed-in visitor, or one that renders a LiveTable or a channel, is always private, always revalidated and varies by cookie, whatever the route asked for, so one visitor's page is never served to another. A header below the floor is raised to it; a header at or above it is honored.

The difference between a static page and a dynamic one with these headers is close to zero, and the dynamic page is still a page in an app: it can take a form, a signed-in area or per-visitor content later without changing how it is built. A CDN in front is one config setting (see elements man deploy/load-balancer).

Prev ReactivityNext RPC and LiveTable in Templates
Product
Manual Pricing Compare
Company
History Contact
Legal
Privacy Policy Acceptable Use Terms License Agreement
© Elements Dev Corporation 2026