Skip to main content

Function: exportStaticSite()

exportStaticSite(options): Promise<StaticExportResult>

Defined in: static.ts:177

Crawls every page served by an already-running server and writes the rendered output to outDir as a static site — plain HTML/CSS/JS, deployable to any static host with no server required at request time.

Reuses the real server's rendering pipeline over real HTTP (rather than reimplementing ReactRoute's rendering logic), so the exported output can never diverge from what the live server actually serves. Props are frozen at export time: pages whose fetchProps/ @ReactService depend on per-request or authenticated state will bake in whatever an unauthenticated crawl request renders — use exclude for personalized pages.

Hydration asset URLs injected by ReactRoute are always root-absolute, so this only produces a working site when it will be served from / (the same pre-existing constraint ReactRoute's hydration feature already has in a live deployment).

Parameters

options

StaticExportOptions

Returns

Promise<StaticExportResult>

Examples

// src/export.ts
import { Logger } from "@rapidrest/core";
import { ObjectFactory } from "@rapidrest/service-core";
import { runStaticExport } from "@rapidrest/react";
import config from "./config.js";

const logger = Logger();
const objectFactory = new ObjectFactory(config, logger);
const result = await runStaticExport(
{ config, basePath: ".", logger, objectFactory },
{ appDir: "app", routePrefix: "/app", outDir: "dist/export" }
);
await objectFactory.destroy();

if (result.errors.length > 0) {
console.error(`[export] Completed with ${result.errors.length} error(s).`);
process.exit(1);
}
console.log(`[export] Wrote ${result.pages.length} page(s) to dist/export.`);
// Multi-app project — writes www's pages to dist/export/, admin's to dist/export/admin/
await exportStaticSite({
port,
apps: [
{ appDir: "apps/www", routePrefix: "" },
{ appDir: "apps/admin", routePrefix: "/admin" },
],
});