Routes and documents
API references extracted from the Gracile code base.
Examples, functions, classes, constants, type declarations…
Function: defineRoute()
function defineRoute<GetHandlerData, PostHandlerData, CatchAllHandlerData, StaticPathOptions, RouteContext>(options): (RouteModule) => RouteModule;
Defined in: packages/server/dist/route.d.ts:33
Defines a file-based route for Gracile to consume.
Important: Property order matters for type inference.
handler (or staticPaths) must be declared before document and
template in the options object. TypeScript resolves generic type
parameters from object properties in declaration order — the handler’s
return type feeds into RouteContext.props, which is then used to type
the context parameter of document and template.
If document/template appear first, props will be inferred as
undefined.
Type parameters
| Type Parameter | Default type |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Parameters
| Parameter | Type | Description |
|---|---|---|
|
|
{ |
Options to populate the current route module. |
|
|
|
A function that returns a server only template. Route context is provided at runtime during the build. Receives the same See documentation |
|
|
|
A function or an object containing functions named after HTTP methods.
A handler can return either a standard Must be declared before See documentation |
|
|
|
A switch to produce an HTML file as it was built with the Only available in See documentation |
|
|
() => |
A function that returns an array of route definition object.
Only available in See documentation |
|
|
|
A function that returns a server only or a Lit client hydratable template. Route context is provided at runtime during the build. Receives the same See documentation |
Returns
(RouteModule) => RouteModule
Example
// ✅ Correct — handler first
defineRoute({
handler: { GET: async (ctx) => ({ id: 1 }) },
document: (ctx) => html`…${ctx.props.GET.id}…`,
template: (ctx) => html`…${ctx.props.GET.id}…`,
});
// ❌ Broken — document before handler, props is undefined
defineRoute({
document: (ctx) => html`…`,
handler: { GET: async (ctx) => ({ id: 1 }) },
template: (ctx) => html`…${ctx.props.GET.id}…`, // TS error!
});
See full guide in the documentation.
Function: html()
function html(strings, ...values): ServerRenderedTemplate;
Defined in: node_modules/.pnpm/@lit-labs+ssr@4.0.0_@types+node@25.3.3/node_modules/@lit-labs/ssr/lib/server-template.d.ts:35
A lit-html template that can only be rendered on the server, and cannot be hydrated.
These templates can be used for rendering full documents, including the
doctype, and rendering into elements that Lit normally cannot, like
<title>, <textarea>, <template>, and non-executing <script> tags
like <script type="text/json">. They are also slightly more efficient than
normal Lit templates, because the generated HTML doesn’t need to include
markers for updating.
Server-only html templates can be composed, and combined, and they support
almost all features that normal Lit templates do, with the exception of
features that don’t have a pure HTML representation, like event handlers or
property bindings.
Server-only html templates can only be rendered on the server, they will
throw an Error if created in the browser. However if you render a normal Lit
template inside a server-only template, then it can be hydrated and updated.
Likewise, if you place a custom element inside a server-only template, it can
be hydrated and update like normal.
A server-only template can’t be rendered inside a normal Lit template.
Parameters
| Parameter | Type |
|---|---|
|
|
|
|
… |
|
Returns
ServerRenderedTemplate