Skip to content

Routing

A page with no config.route derives its URL and name from its location beneath src/web/.

FileDerived pathDerived name
src/web/index.page.tsx/index
src/web/about.page.tsx/aboutabout
src/web/blog/index.page.tsx/blogblog
src/web/products/[id].page.tsx/products/:idproducts.id
src/web/(marketing)/pricing.page.tsx/pricingpricing

The rules are:

  • index.page.tsx claims its directory. It is the only special ordinary-page filename; home.page.tsx serves /home, not /.
  • Every directory and non-index filename contributes a path segment.
  • A plain [id] directory or filename becomes a whole-segment :id parameter.
  • A (group) directory contributes nothing to the path or route name; it only organizes files. Bracket syntax inside a group name is a boot-time error because a group can never contribute a dynamic segment. Write (marketing)/[id]/page.page.tsx, not (marketing[id])/page.page.tsx.
  • The route name is the dotted sequence of contributing segments.

Put a literal path or object in the page’s config:

src/web/products/[id].setup.ts
import type { PageConfig } from "@warlock.js/web";
export const config: PageConfig = {
route: { path: "/catalog/:id", name: "products.show" },
};
src/web/products/[id].page.tsx
export default function ProductPage() {
return <main>Product</main>;
}

An explicit route wins over filesystem derivation for both path and name. A bare string inside config is also valid:

src/web/products.setup.ts
import type { PageConfig } from "@warlock.js/web";
export const config: PageConfig = { route: "/products" };

Warlock reads config.route without executing application code. Route variables, function calls, computed keys, spreads, and indirect config aliases are refused.

Two pages that resolve to the same effective path or name fail discovery and name both files, instead of leaving one page silently unreachable.

Use href() to interpolate a name from the published route table:

import { href } from "@warlock.js/web";
const productUrl = href("products.show", { id: "42" });

During development and production builds, Warlock writes the discovered route table to .warlock/typings/web-routes.d.ts. Include that directory in the application TypeScript project. Once present, href() accepts only known page names and requires the parameters declared by the route:

href("products.show", { id: "42" });
// href("products.shoow", { id: "42" }); // TypeScript error
// href("products.show", {}); // TypeScript error: id is required

The generated file is an aid, not a security boundary. Before declarations exist (for example in a new checkout), names remain strings and href() still validates against the published route table at runtime.

Page documents and their x-warlock-data representations are no-store by default. Opt a public page into shared caching with config.cache:

src/web/products/index.setup.ts
import type { PageConfig } from "@warlock.js/web";
export const config: PageConfig = {
route: { path: "/products", name: "products.index" },
cache: { public: true, maxAge: 60 },
};

maxAge is seconds. Both keys are required: cache: { public: true } fails at boot with InvalidPageCacheOptInError because Warlock will not guess a freshness window. Remove cache entirely to keep the safe default.

The declaration is an opt-in, not permission to cache every response:

Request or response stateFinal Cache-Control
No cache declarationno-store
Opted in, provably unauthenticated, no cookie sent or writtenpublic, max-age=<maxAge>
The request carries a cookie other than locale (including malformed)private, no-store
Authenticated state was used, or the response sets or clears a cookieprivate, no-store
Warlock cannot determine whether authenticated state was usedno-store

The final decision is applied once after loaders finish, to both the document and data response. A loader cannot bypass it by setting Cache-Control manually.

Page routes support static segments, parameters, the exact * wildcard, a terminal wildcard such as /docs/*, and segments that mix several parameters and static text.

They do not support regex parameters, optional parameters, two parameters with no text between them, doubled or trailing slashes, or a non-terminal wildcard. Write two pages for an optional segment and validate constrained values in the loader.

An unsupported declared path fails at boot with PageRoutePathNotSupportedError; Warlock does not normalize it or register it literally. Rejected examples include /users/:id?, /users/:id(\\d+), /:a:b, /a//b, and /users/.

One segment may hold several parameters with static text around or between them. Static text may use letters, digits and . _ ~ -, and two parameters are always separated by text. This is how readable, SEO-friendly URLs are declared:

src/web/listing.setup.ts
export const config = {
route: { path: "/:type-for-:purpose", name: "listing" }, // /house-for-sale
} as const satisfies PageConfig;
src/web/near.setup.ts
export const config = {
route: { path: "/near/:lat-:lng", name: "near" }, // /near/30.04-31.23
} as const satisfies PageConfig;

Each parameter keeps its own name in params, in the loader, and in the typed href() call (href("listing", { type: "house", purpose: "sale" })). href() encodes each value on its own and leaves the static text as written.

How a segment splits. The split is greedy: every parameter except the last takes as much as it can while the text after it still matches. Against /:type-for-:purpose, a-for-b-for-c gives type = "a-for-b" and purpose = "c". A parameter may be empty (/house-for- gives purpose = "") and never spans a /. Server and browser split every URL the same way. Because a value that contains the separator moves the split, keep the separator out of the values (slugify them) or pick text that cannot occur in them.

Which route wins. At each segment, left to right: a static segment beats a mixed one, a mixed one beats a plain :param, and a plain :param beats the catch-all. A route that does not match the whole URL is skipped, so /:id still answers /abc when /:type-for-:purpose is also declared and /house-for-sale goes to the mixed route. Between two mixed segments, the one with the longer leading text wins, then the longer trailing text, then more static text overall (/x-for-:b beats /:a-for-:b). Two pages whose mixed segments tie on all three at the same position, with the same number of segments, are refused at boot with a collision error naming both pages, because the server’s router would pick between them by registration order. The check is conservative: /:a-:b and /:c.:d are refused although no URL contains both separators. Disambiguate by giving one more static text (a different separator or a longer one). Two routes with the same shape (/:a-for-:b and /:x-for-:y) are refused the same way.

A parameter name may be used only once in a path that has a mixed segment or a named wildcard. API routes keep their existing, broader parameter typing.

A terminal wildcard may carry a name: /files/*rest. Its value is available under that name (params.rest, href("files", { rest: "a/b" })) and, as before, under params["*"]. The bare * and /docs/* forms are unchanged.

Filesystem routing has no file name for these shapes; declare them with config.route.path as above.

warlock generate.typings is the exception to stopping: it prints the page file and the rejected path, leaves that one page out of the route typings, and keeps generating typings for every other page.

Use an explicit terminal wildcard instead:

src/web/docs/catch-all.setup.ts
import type { PageConfig } from "@warlock.js/web";
export const config: PageConfig = {
route: { path: "/docs/*", name: "docs.catchAll" },
};

Every discovered page must default-export a runtime component. A page with only named exports is a hard build failure naming the file. A type-only default export does not count because it disappears at runtime.

Every URL segment is written either in the page route/filesystem path or in a layout prefix above it. A prefix replaces its own directory segment for a derived page route and prepends an explicit page path.

See layout prefix and middleware composition →