Route conventions
The root filesystem-routing import exports two scanners with different filename conventions and the same module convention.
Nested convention
routePathFromFile
import { routePathFromFile } from "filesystem-routing";
function routePathFromFile(routeFile: string): string;Converts a root-relative filename without its extension:
/indexbecomes/./blog/indexbecomes/blog/./blog/[id]becomes/blog/:id./blog/[[page]]becomes/blog/:page?./docs/[...path]becomes/docs/*path./(marketing)/aboutremains/(marketing)/aboutin the flat manifest.
Route groups remain in manifest paths so tree construction can nest them.
The generated pageRoutes view removes groups from URL paths.
Pair a route file with a directory to create nesting.
For example, blog.tsx is a layout for routes under blog/.
PageFileSystemRouter
import { PageFileSystemRouter, type PageFileSystemRouterConfig,} from "filesystem-routing";
class PageFileSystemRouter extends BaseFileSystemRouter { constructor(config: PageFileSystemRouterConfig);}PageFileSystemRouter uses config.toPath when provided and otherwise uses routePathFromFile.
See Module convention for its manifest entries.
Flat convention
flatRoutePathFromFile
import { flatRoutePathFromFile } from "filesystem-routing";
function flatRoutePathFromFile(routeFile: string): string | undefined;Converts flat route names:
/_indexbecomes/./concerts.trendingbecomes/concerts/trending./concerts.$citybecomes/concerts/:city./concerts.($page)becomes/concerts/:page?./files.$becomes/files/*splat./_auth.loginbecomes/(_auth)/login, a pathless layout group./concerts_.minebecomes/(concerts_)/concerts/mine, outside theconcertslayout./[sitemap.xml]becomes/sitemap.xml.
Square brackets escape flat-convention characters.
The convention rejects an optional static segment such as (en) because the neutral path syntax represents optional parameters only.
A top-level directory contributes only its route module.
Other files in that directory and deeper nested directories return undefined.
FlatFileSystemRouter
import { FlatFileSystemRouter } from "filesystem-routing";
class FlatFileSystemRouter extends PageFileSystemRouter;Uses flatRoutePathFromFile and inherits the page module convention.
import { resolve } from "node:path";import { FlatFileSystemRouter } from "filesystem-routing";import { fileRoutes } from "filesystem-routing/vite";
fileRoutes({ router: new FlatFileSystemRouter({ dir: resolve("src/routes"), extensions: ["js", "jsx", "ts", "tsx"], }),});Module convention
A JavaScript or TypeScript route module participates when it has a default export or a recognized HTTP method export.
- A default export sets
page: true. $componentlazily selects same-named local runtime exports,default, and$css. It excludes therouteexport and recognized HTTP handlers.- An exported
routevalue creates the eager$$routeref. - Recognized uppercase handler exports create
$GET,$POST, and equivalent refs. - A recognized
GETalso creates$HEADselectingGETunless the module exportsHEAD. - A handler-only module sets
page: falseand has no component ref. - The scanner treats
.mdand.mdxfiles as pages when the configuration includes those extensions.
Set components: false to omit component refs while retaining page status and route configuration.
Set httpMethods: true to recognize the standard method set.
HTTP_METHODS
import { HTTP_METHODS } from "filesystem-routing";
const HTTP_METHODS = [ "HEAD", "GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS",] as const;PageFileSystemRouterConfig
interface PageFileSystemRouterConfig extends FileSystemRouterConfig { components?: boolean; httpMethods?: boolean | readonly string[];}components defaults to true.
httpMethods defaults to false; true selects HTTP_METHODS, and an array selects only the listed export names.