> ## Documentation Index
> Fetch the complete documentation index at: https://better-router.bansal.io/llms.txt
> Use this file to discover all available pages before exploring further.

# API reference

> Exports from better-router and its subpath modules.

Types listed here are exported from `better-router` unless noted.

## `better-router/plugin`

| Export                   | Purpose                                                                                             |
| ------------------------ | --------------------------------------------------------------------------------------------------- |
| `betterRouter(options?)` | Vite plugin (includes the React plugin). Import this from `vite.config.ts`, not from route modules. |

## `better-router`

| Export                                | Purpose                                                |
| ------------------------------------- | ------------------------------------------------------ |
| `defineHandler(...)`                  | Hono handler with return-value normalization           |
| `defineHandler.withValidator(schema)` | Standard Schema validation (`body`, `query`, `params`) |
| `defineMiddleware(fn)`                | Typed Hono middleware                                  |
| `redirect(to, status?)`               | Loader/action redirect (`302` default)                 |
| `getEnv()`                            | Request `c.env` (throws outside a request)             |
| `getHonoContext()`                    | Request Hono `Context`                                 |
| `getRequestContext()`                 | ALS store (`env`, `hono`, `request`)                   |

### Types

`LoaderArgs` (`c`, `params`, `request`), `ActionArgs` (`formData` plus loader args), `AppEnv`, `AppBindings`, `AppVariables`, `PageServerModule`, `WorkerHandlers`, `TocItem`, `MarkdownOptions`, `MarkdownCompileFile`, `MarkdownParseResult`.

Augment `AppBindings` / `AppVariables` via `declare module "better-router"`.

## `better-router/react`

| Export                        | Purpose                                                                           |
| ----------------------------- | --------------------------------------------------------------------------------- |
| `Link`                        | SPA navigation. `replace`, `reloadDocument`, `onNavigate`                         |
| `Form`                        | Progressive form. `method`, `encType`, `action`. `data-enhance="false"` skips SPA |
| `useForm` / `useIslandForm`   | Controlled form helper (`data`, `errors`, `pending`, `post`)                      |
| `useLoaderData<T>()`          | Page loader result                                                                |
| `useFrontmatter<T>()`         | Markdown frontmatter (empty object on JSX pages)                                  |
| `useToc()`                    | Markdown heading tree                                                             |
| `useParams<T>()`              | Dynamic segments                                                                  |
| `useQuery<T>()`               | Query string                                                                      |
| `useRouter()`                 | `{ path, navigating, visit, refresh }`                                            |
| `island(Component, strategy)` | Manual island wrapper                                                             |

Hooks throw if they run outside a page (no `RouterContext`).

## `better-router/worker`

| Export                              | Purpose                        |
| ----------------------------------- | ------------------------------ |
| `defineWorker(handlers)`            | `src/worker.ts` default export |
| `createWorker({ app, userWorker })` | Used by the generated entry    |

`handlers` may include `fetch`, `scheduled`, `queue`, and `email`. See [Worker entrypoint](/worker).
