Static pages
A page that calls no action can be written to a file and served by any static host, with the runtime next to it. The page also covers rendering under a path prefix and what a static host leaves out.
A page without a server
A page whose statements change only signals, and call no action, needs no server once it is rendered. Page::render returns the whole document as a String, which a program writes to a file. The browser then needs the runtime from a URL the static host serves, so the program writes the runtime's source next to the page and points the page at it:
| Item | Signature | What it is |
|---|---|---|
Page::runtime_src | fn runtime_src(self, src: Asset) -> Self | Loads the runtime from src instead of RUNTIME_PATH |
RUNTIME_JS | const RUNTIME_JS: &str | The browser runtime's source, to write next to the page |
RUNTIME_PATH | const RUNTIME_PATH: &str | The path a page loads the runtime from without runtime_src, where RouterExt::hypersig_runtime serves it |
Page::render_under | fn render_under(&self, base: Base) -> String | Renders the document for a site served under base |
use hypersig::html::{button, span};
use hypersig::{Asset, Page, RUNTIME_JS, Signals};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Counter {
count: i32,
}
let count = Counter::count();
let html = Page::new("Counter", Counter { count: 0 })
// Load the runtime from a file next to the page.
.runtime_src(Asset::new("./hypersig.js"))
.child(button().on_click(count.set(count + 1)).text("+1"))
.child(span().bind_text(count.to_text()))
.render();
// Write `html` to index.html and RUNTIME_JS to hypersig.js, then serve both.The page's <head> then holds <script type="module" src="./hypersig.js"></script>. A URL relative to the page, such as ./hypersig.js, is written as given. The first HTML already shows each binding's initial value, so the page reads correctly before the script runs.
runtime_src takes an Asset, so a plain string is E0308, mismatched types. An Asset is built only from text written in the program, because the runtime's URL decides what code the page runs:
use hypersig::{Page, Signals};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Counter {
count: i32,
}
// A plain string, where an `Asset` belongs.
let page = Page::new("Counter", Counter { count: 0 }).runtime_src("./hypersig.js");A site under a path prefix
A GitHub Pages project site is served at /<repository>, such as /hypersig. Page::render_under takes that prefix as a Base, a constant from Base::new, and writes the runtime's path and each Asset path that starts with a single / under it, as a page served by a route under that base would. A relative URL, or one on another host, stays as written.
use hypersig::html::p;
use hypersig::{Asset, Base, Page, Signals};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Notes {}
// A GitHub Pages project site is served under the repository's name.
const SITE: Base = Base::new("/hypersig");
const RUNTIME: Asset = Asset::new("/hypersig.js");
const STYLE: Asset = Asset::new("/style.css");
let page = Page::new("Notes", Notes {})
.runtime_src(RUNTIME)
.stylesheet(STYLE)
.child(p().text("Hello"));
let html = page.render_under(SITE);
// Write `html` to `notes/index.html`, and `RUNTIME_JS` to `hypersig.js`.Base::new panics, and in a const fails to compile, on a path that does not start with /, ends with /, or is / alone. Serving under a path prefix covers a Base for a served application.
This site is built that way: a Rust program renders each page with hypersig and render_under, and writes the files that GitHub Pages serves. The live demos on these pages run on signals alone for the same reason.
What a static host leaves out
A static file carries none of the headers a served page has, X-Frame-Options and frame-ancestors among them, and a <meta> cannot carry frame-ancestors, so set them in the host's configuration where the host allows it.
The types do not tell a static page from a served one, so a static page that calls an action still compiles. The static host has no handler at the action's path, so the call gets whatever error the host answers with, and fails as Busy and failed calls describes. A runtime file missing from the host shows as a 404 in the browser's console, and the page then shows its first HTML and reacts to nothing.
Next, Security covers what hypersig checks in what a visitor sends.