AsterDocsv1.2.3

Docs / Guides

Routing, SSR and revival

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:

tsx
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 with Counter.__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 }) or htmlResponse()), 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:

  1. Hovering (pointerenter) or focusing (focusin) inside an island preloads its ES module via <link rel="modulepreload">.
  2. A first keydown, click, input, change, or submit wakes 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.
  3. To wake a component before interaction, place data-aster-eager on the emitted element that carries data-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():

tsx
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:
tsx
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.

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:

tsx
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.