1. Route with ordinary links
src/router.ts uses the native Navigation API (window.navigation), URLPattern, and document.startViewTransition()—automatically falling back to the History API in browsers that don't yet support Navigation API.
Start with HTML <a href="/..."> tags:
import { Router, useParams, type RouteDef } from '@fierst/aster/router';
function Home() {
return <h1>Home</h1>;
}
export function* Profile() {
for (;;) {
const { id } = useParams<{ id: string }>();
yield <h1>User #{id}</h1>;
}
}
const routes: RouteDef[] = [
{ path: '/', component: Home },
{ path: '/users/:id', component: Profile },
];
export function App(props: { url?: string }) {
return (
<main>
<nav>
<a href="/">Home</a>
<a href="/users/42">User #42</a>
</nav>
<Router routes={routes} url={props.url} fallback={() => <h1>404</h1>} />
</main>
);
}With a mounted browser router, eligible same-origin links use client navigation. Without JavaScript, a link makes an ordinary request to the server. Configure the server to handle each route and pass its request URL to the server-rendered router. Omit the controlled url prop when mounting a client router that should follow browser navigation.
Literal paths, named segments such as /users/:id, and wildcards work with the fallback matcher. Route parameters are decoded when possible. A malformed percent escape keeps the captured text available to the route.
2. Automatic Event-Delegated Lazy Revival
In a standalone Aster document, revival metadata comes from the JSX transform. Astro uses its own client directives to schedule Aster islands.
- Every exported stateful generator (
export function* Counter) is automatically stamped withCounter.__asterSrc = '/path/File.js#Counter'. - An exported stateless component can also own an island when the client needs to recreate an event handler or a child composition. Static HTML needs no activation.
- During SSR (
renderToString(view, { revive: true })orhtmlResponse()), Aster automatically wraps the outermost stamped generator in<aster-island data-aster-src="..." data-aster-props="...">.
On page load, the delegated listener from @fierst/aster/revive runs:
- Hovering (
pointerenter) or focusing (focusin) inside an island preloads its ES module via<link rel="modulepreload">. - A first
keydown,click,input,change, orsubmitwakes the island. Aster attaches to compatible DOM and can preserve text entered before activation. A mismatch can cause a local remount. A deferred native form submission runs browser validation and gives the component's submit handler the original submit button and the opportunity to cancel. - To wake a component before interaction, place
data-aster-eageron the emitted element that carriesdata-aster-src.
Production Manifest Resolution (aster-manifest.json)
During vp build, @fierst/aster/vite preserves exported generator chunks and emits aster-manifest.json mapping source module paths to real content-hashed Rolldown chunks (e.g. "/src/Counter.tsx": "/assets/Counter-Dk8x2a.js"). Pass that manifest (or standard Vite .vite/manifest.json) to renderToString() or renderDocument():
import manifest from './dist/aster-manifest.json' with { type: 'json' };
import { htmlResponse } from '@fierst/aster/ssr';
return htmlResponse(<App />, { revive: true, manifest });Serve the client build at its configured base URL. The Aster manifest includes the loader and runtime chunks as well as component entries. Include each interactive component module in the client build. Keep island modules free of application startup side effects since the browser imports them on demand.
For a hand-built client or a standard Vite manifest without those entries, pass browser module URLs in revivalUrl and runtimeUrl, or provide an import map for @fierst/aster/revive and @fierst/aster. A failed activation dispatches a bubbling aster:revival-error event with the error in detail; the next interaction attempts activation again. When installing the loader manually, call the function returned by installRevival() to remove its delegated listeners.
SSR route hooks read the nearest router's URL and parameters. Async components on Node can read context across an await. On runtimes without AsyncLocalStorage, capture context before the first await or use the route props. Components can register resource cleanup using ctx.onCleanup() on both server and client. Stream consumers can cancel the reader to signal pending generators through ctx.signal.
Async SSR awaits promises returned by registered cleanup callbacks. If cleanup fails, the render promise or active stream surfaces an AggregateError. Synchronous SSR logs late cleanup rejections, and a canceled stream logs cleanup failures after its reader has gone away.
Each client generator execution has its own ctx.signal. After a prop restart or unmount, that execution continues to see its aborted signal, including after an await. A cleanup registered on an already aborted context runs immediately. Client teardown reports cleanup failures to the console and continues disposing the component.
Island props support plain data, arrays, dates, query parameters, JSX templates, and exported child components. A named callback exported from a compiled client module travels as a module reference. A callback that closes over component locals is recreated by running its owning client component. Include those modules in the client build. Routed islands also restore their matched URL and parameters.
Only pass public data across an island boundary. A closure created exclusively in a server request handler has no browser implementation. SSR reports its prop path, as it does for cyclic input or unsupported class instances.
In-Place HMR During vp dev
During development, the Vite plugin adds a hot-update handler to eligible component modules. To see which state survives an edit, mount two counters, increment both, then change one component’s render code in its own module. Observe the edited instance and its sibling separately.
Async & Out-of-Order Streaming SSR (renderToStringAsync & renderToStream)
When your tree contains async function* components:
- Use
renderToStringAsync(view, opts)to await every async generator to completion and return a single resolved HTML string. - Use
renderToStream(view, opts)/streamResponse(view, opts)to send the shell with the first yielded loading views before every result is ready, then send self-cleaning<template>+<script>swap chunks out-of-order as each async component resolves:
import manifest from './dist/aster-manifest.json' with { type: 'json' };
import { streamResponse } from '@fierst/aster/ssr';
export default {
fetch(req: Request) {
const url = new URL(req.url);
return streamResponse(<App url={url.pathname + url.search} />, {
title: 'Streaming Aster',
revive: true,
manifest,
});
},
};For a nonce-based Content Security Policy, pass the response's nonce as nonce in the document options and include the matching nonce in the response's script-src header. Aster applies it to its import map, module bootstrap, and deferred streaming swap scripts. The application supplies the policy header and generates a fresh nonce for each response.
Match a request URL
Select a URL to see how named route segments become component props. This example supplies the URL explicitly, as a server would.
import { Router, useParams } from "@fierst/aster/router";
import "./example.css";
function Project() {
const { id } = useParams();
return (
<dl class="route-result">
<div>
<dt>Matched route</dt>
<dd>/projects/:id</dd>
</div>
<div>
<dt>id</dt>
<dd>{id}</dd>
</div>
</dl>
);
}
const routes = [{ path: "/projects/:id", component: Project }];
export default function* RouteInspector() {
let url = "/projects/atlas";
for (;;)
yield (
<section class="specimen">
<header>
<h1>Route inspector</h1>
</header>
<label class="field">
Request URL
<select value={url} onChange={(e) => (url = e.currentTarget.value)}>
<option>/projects/atlas</option>
<option>/projects/orbit</option>
<option>/archive</option>
</select>
</label>
<Router
routes={routes}
url={url}
fallback={() => <p class="notice">No matching project route</p>}
/>
</section>
);
}
@media screen and (prefers-reduced-motion: no-preference) {
#example button.btn:enabled {
transition: transform 120ms ease-out;
}
#example button.btn:enabled:active {
transform: scale(0.98);
}
}
@media (prefers-reduced-motion: reduce) {
#example button.btn:enabled:active {
transition: none;
transform: none;
}
}
* {
box-sizing: border-box;
}
:root {
color-scheme: light dark;
--control-border: light-dark(#827a87, #928699);
--accent: light-dark(
oklch(0.45 var(--site-chroma, 0.15) var(--site-hue, 296)),
oklch(0.8 calc(var(--site-chroma, 0.15) * 0.74) var(--site-hue, 296))
);
--on-accent: light-dark(white, #18131e);
--ink: light-dark(#2b2630, #f4edf7);
--quiet: light-dark(#645d69, #c8bdcd);
--edge: light-dark(#d9d1df, #514857);
--paper: light-dark(#fffdfa, #27232c);
--canvas: light-dark(#f1edf2, #201c24);
font:
1rem/1.5 system-ui,
sans-serif;
}
:root[data-site-theme="light"] {
color-scheme: light;
}
:root[data-site-theme="dark"] {
color-scheme: dark;
}
body {
margin: 0;
padding: clamp(0.85rem, 4vw, 1.7rem);
color: var(--ink);
background-color: var(--canvas);
background-image: radial-gradient(var(--edge) 0.8px, transparent 0.8px);
background-size: 13px 13px;
}
.specimen {
max-width: 38rem;
margin: auto;
padding: clamp(1rem, 4vw, 1.65rem);
border: 1px solid var(--edge);
border-top: 3px solid var(--accent);
border-radius: 0.7rem;
background: var(--paper);
box-shadow: 0 4px 12px #211a2810;
}
.specimen header,
.specimen footer,
.demo-box,
.demo-row,
.demo-confirm {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 0.65rem;
}
.specimen header {
justify-content: space-between;
gap: 0.4rem;
margin-bottom: 0.9rem;
}
h1,
h3 {
margin: 0;
font-size: 1.25rem;
line-height: 1.2;
letter-spacing: -0.035em;
}
header > span,
.caption,
.readouts span,
dt {
font:
500 0.78rem/1.4 ui-monospace,
Consolas,
monospace;
color: var(--quiet);
}
p {
margin: 0.6rem 0 1rem;
}
.specimen footer {
margin-top: 1rem;
}
button,
input,
select {
font: inherit;
color: var(--ink);
border: 1px solid var(--control-border);
border-radius: 0.45rem;
padding: 0.45rem 0.72rem;
max-width: 100%;
background: var(--paper);
}
button {
cursor: pointer;
font-weight: 570;
}
button:hover {
border-color: var(--accent);
}
button:disabled {
opacity: 0.55;
cursor: default;
}
.primary {
color: var(--on-accent);
background: var(--accent);
border-color: var(--accent);
}
.danger {
color: light-dark(#913148, #ffb5c2);
background: light-dark(#fff3f4, #3c2930);
}
:focus-visible {
outline: 3px solid var(--accent);
outline-offset: 2px;
}
.readouts,
.route-result {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 0.65rem;
margin: 0.6rem 0;
}
.readouts > div,
.route-result > div {
display: grid;
gap: 0.15rem;
padding: 0.65rem 0.85rem;
background: var(--canvas);
border-radius: 0.35rem;
}
.readouts output,
.ticket-body output {
font:
600 2.55rem/1.1 ui-monospace,
monospace;
letter-spacing: -0.08em;
color: var(--accent);
}
dd {
margin: 0;
font:
600 0.92rem/1.4 ui-monospace,
monospace;
}
.field {
display: grid;
gap: 0.22rem;
margin: 0.5rem 0;
color: var(--quiet);
font-weight: 550;
}
.field input {
width: 100%;
}
.score-controls {
align-items: flex-end;
}
.score-controls .field {
margin: 0;
}
.demo-list {
list-style: none;
margin: 0.6rem 0 0;
padding: 0;
}
.demo-list li {
border-top: 1px solid var(--edge);
padding: 0.37rem 0;
}
.checklist form,
.specimen > .demo-box {
margin: 0.5rem 0;
}
.pill,
.notice {
padding: 0.25rem 0.6rem;
color: var(--accent);
background: var(--canvas);
border-radius: 0.4rem;
}
.done {
text-decoration: line-through;
color: var(--quiet);
}
.demo-muted {
color: var(--quiet);
}
.trace-log {
max-height: 11rem;
overflow: auto;
padding: 0.7rem 0.8rem 0.7rem 2.7rem;
margin: 0.6rem 0 0;
background: light-dark(#322b38, #151119);
color: #f3e9fc;
font:
0.83rem/1.65 ui-monospace,
monospace;
border-radius: 0.45rem;
}
.trace-log li::marker {
color: #c6a3e5;
}
.trace button {
margin: 0.15rem;
}
.ticket-body {
display: flex;
align-items: center;
justify-content: space-between;
border-block: 1px dashed var(--edge);
padding: 0.65rem 0;
}
.ticket-body output {
display: block;
}
.perforations {
height: 2.3rem;
width: 5rem;
background: repeating-linear-gradient(
90deg,
var(--accent) 0 2px,
transparent 2px 4px,
var(--accent) 4px 5px,
transparent 5px 8px
);
}
3. Recovering Closed Generators with <ErrorBoundary>
In JavaScript, an uncaught error permanently closes a generator (done: true). Wrap subtrees in <ErrorBoundary> so calling reset() mounts a brand-new generator instance cleanly:
import { ErrorBoundary } from '@fierst/aster';
export function SafePanel({ children }: { children?: any }) {
return (
<ErrorBoundary
fallback={(err, reset) => (
<div role="alert">
<p>Error: {String((err as Error)?.message ?? err)}</p>
<button onClick={reset}>Retry</button>
</div>
)}
>
{children}
</ErrorBoundary>
);
}Wake a server-rendered counter
The ticket arrives as HTML. Hover or focus to preload the component. Watch the module request below, then click Add a seat. The first click activates the existing button and updates the ticket.
export default function* Counter() {
let seats = 1;
for (;;)
yield (
<main id="example" data-ready="revival-counter" class="specimen ticket">
<header>
<h1>Studio admission</h1>
<span>Group ticket</span>
</header>
<div class="ticket-body">
<div>
<span class="caption">Seats on this ticket</span>
<output>{seats}</output>
</div>
<div class="perforations" aria-hidden="true"></div>
</div>
<footer>
<button class="primary" onClick={() => seats++}>
Add a seat
</button>
</footer>
</main>
);
}
@media screen and (prefers-reduced-motion: no-preference) {
#example button.btn:enabled {
transition: transform 120ms ease-out;
}
#example button.btn:enabled:active {
transform: scale(0.98);
}
}
@media (prefers-reduced-motion: reduce) {
#example button.btn:enabled:active {
transition: none;
transform: none;
}
}
* {
box-sizing: border-box;
}
:root {
color-scheme: light dark;
--control-border: light-dark(#827a87, #928699);
--accent: light-dark(
oklch(0.45 var(--site-chroma, 0.15) var(--site-hue, 296)),
oklch(0.8 calc(var(--site-chroma, 0.15) * 0.74) var(--site-hue, 296))
);
--on-accent: light-dark(white, #18131e);
--ink: light-dark(#2b2630, #f4edf7);
--quiet: light-dark(#645d69, #c8bdcd);
--edge: light-dark(#d9d1df, #514857);
--paper: light-dark(#fffdfa, #27232c);
--canvas: light-dark(#f1edf2, #201c24);
font:
1rem/1.5 system-ui,
sans-serif;
}
:root[data-site-theme="light"] {
color-scheme: light;
}
:root[data-site-theme="dark"] {
color-scheme: dark;
}
body {
margin: 0;
padding: clamp(0.85rem, 4vw, 1.7rem);
color: var(--ink);
background-color: var(--canvas);
background-image: radial-gradient(var(--edge) 0.8px, transparent 0.8px);
background-size: 13px 13px;
}
.specimen {
max-width: 38rem;
margin: auto;
padding: clamp(1rem, 4vw, 1.65rem);
border: 1px solid var(--edge);
border-top: 3px solid var(--accent);
border-radius: 0.7rem;
background: var(--paper);
box-shadow: 0 4px 12px #211a2810;
}
.specimen header,
.specimen footer,
.demo-box,
.demo-row,
.demo-confirm {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 0.65rem;
}
.specimen header {
justify-content: space-between;
gap: 0.4rem;
margin-bottom: 0.9rem;
}
h1,
h3 {
margin: 0;
font-size: 1.25rem;
line-height: 1.2;
letter-spacing: -0.035em;
}
header > span,
.caption,
.readouts span,
dt {
font:
500 0.78rem/1.4 ui-monospace,
Consolas,
monospace;
color: var(--quiet);
}
p {
margin: 0.6rem 0 1rem;
}
.specimen footer {
margin-top: 1rem;
}
button,
input,
select {
font: inherit;
color: var(--ink);
border: 1px solid var(--control-border);
border-radius: 0.45rem;
padding: 0.45rem 0.72rem;
max-width: 100%;
background: var(--paper);
}
button {
cursor: pointer;
font-weight: 570;
}
button:hover {
border-color: var(--accent);
}
button:disabled {
opacity: 0.55;
cursor: default;
}
.primary {
color: var(--on-accent);
background: var(--accent);
border-color: var(--accent);
}
.danger {
color: light-dark(#913148, #ffb5c2);
background: light-dark(#fff3f4, #3c2930);
}
:focus-visible {
outline: 3px solid var(--accent);
outline-offset: 2px;
}
.readouts,
.route-result {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 0.65rem;
margin: 0.6rem 0;
}
.readouts > div,
.route-result > div {
display: grid;
gap: 0.15rem;
padding: 0.65rem 0.85rem;
background: var(--canvas);
border-radius: 0.35rem;
}
.readouts output,
.ticket-body output {
font:
600 2.55rem/1.1 ui-monospace,
monospace;
letter-spacing: -0.08em;
color: var(--accent);
}
dd {
margin: 0;
font:
600 0.92rem/1.4 ui-monospace,
monospace;
}
.field {
display: grid;
gap: 0.22rem;
margin: 0.5rem 0;
color: var(--quiet);
font-weight: 550;
}
.field input {
width: 100%;
}
.score-controls {
align-items: flex-end;
}
.score-controls .field {
margin: 0;
}
.demo-list {
list-style: none;
margin: 0.6rem 0 0;
padding: 0;
}
.demo-list li {
border-top: 1px solid var(--edge);
padding: 0.37rem 0;
}
.checklist form,
.specimen > .demo-box {
margin: 0.5rem 0;
}
.pill,
.notice {
padding: 0.25rem 0.6rem;
color: var(--accent);
background: var(--canvas);
border-radius: 0.4rem;
}
.done {
text-decoration: line-through;
color: var(--quiet);
}
.demo-muted {
color: var(--quiet);
}
.trace-log {
max-height: 11rem;
overflow: auto;
padding: 0.7rem 0.8rem 0.7rem 2.7rem;
margin: 0.6rem 0 0;
background: light-dark(#322b38, #151119);
color: #f3e9fc;
font:
0.83rem/1.65 ui-monospace,
monospace;
border-radius: 0.45rem;
}
.trace-log li::marker {
color: #c6a3e5;
}
.trace button {
margin: 0.15rem;
}
.ticket-body {
display: flex;
align-items: center;
justify-content: space-between;
border-block: 1px dashed var(--edge);
padding: 0.65rem 0;
}
.ticket-body output {
display: block;
}
.perforations {
height: 2.3rem;
width: 5rem;
background: repeating-linear-gradient(
90deg,
var(--accent) 0 2px,
transparent 2px 4px,
var(--accent) 4px 5px,
transparent 5px 8px
);
}
import { renderDocument } from '@fierst/aster/ssr';
import Counter from '../revival-counter.jsx';
export function exampleDocument(options) {
return renderDocument(<Counter />, { title: 'Lazy revival', ...options });
}