Static sites

A static Soda site renders every page at build time, into files any host serves: Vercel, Netlify, GitHub Pages, a bucket. Pages are written as on any backend: routes as folders, view!, components, layouts, typed links. sodakit.dev is one, built from this repository's docs/.

This walks through the template, Atlas: a home page, a guide and notes, all from Markdown.

Make a site#

soda new --static field-guide
cd field-guide && soda dev        # http://localhost:5180
field-guide/
  pages/
    guide/            one Markdown file per page, in file-name order
    notes/            one file per note, named for its day: 2026-10-14-dark-mode.md
  src/
    main.rs           soda_static::app!(site::Site)
    site.rs           loads the guide and notes from pages/
    components.rs     shared components
    routes/
      layout.rs       wraps every page
      page.rs         /
      guide/page.rs           /guide
      guide/[topic]/page.rs   /guide/:topic
      notes/page.rs           /notes
      notes/[note]/page.rs    /notes/:note
      not_found.rs    built as 404.html
  web/
    app.html          the page shell
    app.css           the design tokens, light and dark
  tests/e2e.mjs       end-to-end tests on the built site

Load the site's data#

The site's state implements soda_static::App. Its load runs once at build time, from the site's folder. An error stops the build and says why.

pub struct Site {
    pub guide: Vec<Page>,
    pub notes: Vec<Note>, // newest first
}

impl soda_static::App for Site {
    const WATCH: &'static [&'static str] = &["pages/guide", "pages/notes"];

    async fn load() -> Result<Self, Box<dyn std::error::Error + Send + Sync>> {
        // read pages/ (see src/site.rs)
    }
}

WATCH lists what load reads. Under soda dev, saving a file there reloads the data and re-renders every open page.

Pages read it as cx.state. Name it in src/main.rs:

mod components;
mod site;

soda_static::app!(site::Site);

Write a page#

// src/routes/notes/page.rs
pub fn head(_cx: &Cx) -> Head {
    Head::title("Notes").description("What's new, newest first.")
}

pub fn page(cx: &Cx) -> View {
    view! {
        <h1>"Notes"</h1>
        <ul class="grid gap-4 md:grid-cols-3">
            {cx.state.notes.iter().map(|note| view! { <NoteCard {note} /> })}
        </ul>
    }
}

head is written into the page's file: the title goes in <title> (as Notes · Atlas) and og:title, the description in <meta name="description"> and og:description. Search engines and link previews see them without running anything.

What a search engine or an assistant should know about what's on a page, in schema.org's words (a local business and where it works, a person, an article and its day), goes in the page as structured data: {soda::view::structured_data(&json!({"@context": "https://schema.org", "@type": "BlogPosting", "headline": …}))}, anywhere in its view (a layout's, for what every page should say). It's written as JSON-LD, which browsers never run. A view can't hold a <script> itself, so this is the way in, and it can't be ended early by what's in it.

A page for each item#

A route with a param, like notes/[note], lists the pages to build with params:

// src/routes/notes/[note]/page.rs
use super::Params;
use crate::prelude::*;

pub fn params(cx: &Cx) -> Vec<Params> {
    cx.state.notes.iter().map(|n| Params { note: n.page.slug.clone() }).collect()
}

pub fn head(cx: &Cx<Params>) -> Head {
    match cx.state.note(&cx.params.note) {
        Some(n) => Head::title(&n.page.title).description(&n.page.summary),
        None => Head::default(),
    }
}

pub fn page(cx: &Cx<Params>) -> Result<View> {
    let note = cx.state.note(&cx.params.note).ok_or(NotFound)?;
    // …
}
  • A route with a param but no params is a compile error.
  • params can be async.
  • Link to a page with its function: link::notes::note("dark-mode").
  • A param can't be named page. That's why the guide's is [topic].

Build#

soda build
In dist/What
index.html/
guide/start-here/index.html, …each page, in its own folder
404.htmlnot_found.rs, served by static hosts for unknown addresses
app.css, soda.js, …styles and scripts
sitemap.xml, robots.txtwritten when the site has a url

The build fails, listing each address, if a page errors or returns NotFound for params it was given. A route with an #[action] is a compile error: nothing can post to files. When a site needs actions, move its pages to a server or local-first app.

Give the site's address to get sitemap.xml and robots.txt. Each page's canonical address is filled in too, but only where web/app.html has the tags (<link rel="canonical">, <meta property="og:url">). Atlas's shell has neither, so add them for canonical links:

soda_static::app!(site::Site, url = "https://atlas.example");

A download is built once, into a file at its address, so it takes only cx:

// src/routes/downloads.rs
#[download]
pub fn search(cx: &Cx) -> Result<Download> {
    Ok(Download::text("search.json", "application/json", index(&cx.state)))
}

Link to it with href=Search. The build writes it to dist/api/_/search.

Each page and stylesheet asks for the site's images, fonts and media by version: the build adds each file's content hash to every address that names it (src, srcset, href, poster, and url(…) in a style or a stylesheet), so /images/meg.webp is written /images/meg.webp?v=3fa94c1e. The template's vercel.json has browsers keep any address with ?v= for a year, so a page that's reloaded or gone back to has its images in its first paint instead of waiting on the host, and a file that changes is at a new address. What a script asks for, and a script or stylesheet itself, is left as it is.

Don't give a photo that might be on the first screen decoding="async": it lets the browser paint the page before the photo's decoded, so even a photo it kept blinks in on every reload. Leave decoding out (or sync), and keep loading="lazy" for what's further down.

In the browser#

Each page is a file, so links and Back are the browser's own. Add a crossfade between pages with one line of CSS (the template has it):

@view-transition { navigation: auto; }

A page renders the same for everyone, from its path alone. The query string and cx.recall aren't available, and cx.now() is the build time.

soda dev#

soda dev runs the site as a server, rendering each page when it's requested, exactly as the build would. Code changes are patched in and open pages re-render in about 0.6 s. Saving a watched file shows at once. A page that panics shows the error in its place.

Test#

soda check runs the site's tests (cargo test), Clippy and the token check. soda test then builds the site and runs tests/e2e.mjs in real browsers against dist/, served as a static host serves it. It checks each page's head, the links, the 404, and that no page asks for a backend.

Deploy#

dist/ works on any host that serves files.

soda deploy publishes with Vercel. It needs the Vercel CLI (npm i -g vercel), linked to the site's project (vercel link). It runs scripts/build.sh locally as vercel.json says, then uploads dist/ with vercel.json's headers (including its Content-Security-Policy). A static site's policy keeps 'unsafe-eval' for Datastar's expressions: the server and local-first backends give each page a nonce of its own instead, but a nonce written into a built file would be everyone's, and nothing on a static page comes from anyone but the site.

To build on the host instead (a Vercel or Netlify project connected to the repository, or CI):

  1. Depend on the SDK by its repository, not a path, so Cargo.lock pins the commit. The template's Cargo.toml shows how.
  2. Run scripts/vercel-install.sh, then scripts/build.sh. The SDK's repository is private, so set SODA_SDK_TOKEN (a read-only GitHub token for it) in the host's project.
  3. Serve dist/.

This page is docs/static.md in the SDK, built into this site as it is.

Esc