elements
Start Docs Blog Demos UI 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
  • Issues
  • Changelog
Manual / Elements HTML/ Server Rendering

Server Rendering

elements man html/server

On This Page

  • Redirect
  • HTTP Caching

How a page is rendered on the server and made reactive 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 from the route handler by returning 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 sets window.location; in a route or middleware it writes a 302; in an @rpc function the URL is sent back in the reply and the browser navigates when the call returns.

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 redirect an already signed-in visitor in 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 navbar that reads the session can stay. It changes to the signed-in state, and the destination page renders the navbar in that same state, so the navbar does not change again when the new page loads.

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. Write return after redirect() to skip the code that follows it.

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

redirect() 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, a hash of the rendered page. 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 to an unchanged page transfers no page body. The page's HTML names each asset by its hashed URL, so revalidating the page also tells the browser which asset files are current.

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() }); }

Elements sends the route's header when the page's data allows it. An anonymous page whose data includes no session state, LiveTable or channel is sent with exactly the header the route set. A page for a signed-in visitor, or a page that renders a LiveTable or a channel, is always private, always revalidated and varies by cookie, so one visitor's page is never served to another. On such a page a route header that allows more caching than that is replaced, and a route header that is at least as strict is sent as written.

With these headers a revisit to an unchanged dynamic page transfers a 304 response with no body, about what a revisit to a static file transfers, 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