smolter
API

flatRoutes

Discover a flat route table at build time.

Compatibility

ReactServer/build API, not a browser import.
React SSRAvailable to this renderer's SSR integration; browser event handlers run after hydration.
PreactServer/build API, not a browser import.
Preact SSRAvailable to this renderer's SSR integration; browser event handlers run after hydration.

Import from @kasperrt/smolter/react/vite @kasperrt/smolter/preact/vite

Signature

function flatRoutes(
  directory: string,
): Promise<RouteFile[] | Error>;

Parameters

directory is the path to the routes folder.

Returns

A Promise<RouteFile[] | Error>. Each item contains a route pattern and module filepath.

Usage

Import this API from one of the entry points listed above. Keep server and build helpers in their dedicated subpaths.

const routes = await flatRoutes("src/routes");

if (routes instanceof Error) {
  console.error(routes.message);
}

Behavior and edge cases

Node filesystem access is required; do not import this entry into browser components. Only immediate flat files and route folders are discovered. Invalid patterns and conflicting parameter shapes return Error values. The Vite plugin normally calls this for you.

Literal dots and route identity

Dots separate URL segments. Escape a literal dot as [.]: robots[.]txt.ts or robots[.]txt/route.ts maps to /robots.txt; feeds.rss[.]xml maps to /feeds/rss.xml. File-route paths and identity come from filenames. Do not export id or routePath: the Vite plugin rejects those overrides. Target generated route data with its path and root data with root. Explicit IDs remain available in manually defined route arrays. Other bracket escapes and optional segments are unsupported.

View implementation ↗