When qwik-docs renders a page, it first builds a route data object that represents what is on that page. This guide explains how route data is generated and how you read it.
See the "Route Data Reference" for the full list of properties.
What is route data?
Route data is an object containing the information required to render a single page. It carries data for the current page together with data derived from your qwik-docs configuration.
The route data model is ported from Astro Starlight.
The RouteData interface lives in @qwik-docs/framework and mirrors Starlight's route data where the field exists.
Some Starlight fields have no qwik-docs equivalent, and qwik-docs adds a few fields of its own.
See the reference for the exact shape.
Building route data
qwik-docs builds route data with generateRouteData from @qwik-docs/framework.
The catch-all documentation route at src/routes/docs/[...slug]/index.tsx calls it in a Qwik City routeLoader$:
import { entries } from 'virtual:qwik-docs/content';
import siteConfig from 'virtual:qwik-docs/config';
import { loadPage, generateRouteData } from '@qwik-docs/framework';
export const useDocsData = routeLoader$(async (event) => {
const page = await loadPage(fullSlug, entries);
return generateRouteData({
config: siteConfig as any,
entries,
page,
pathname: event.pathname,
base: import.meta.env.BASE_URL,
});
});
generateRouteData derives the sidebar, prev/next pagination, table of contents, edit URL, head tags, and last-updated date from the page frontmatter, the content set, and the site config.
See the "Using the docs design in custom pages" section of the Pages guide for its full option list.
Reading route data
qwik-docs components read route data through a Qwik context.
The route provides it with <DocsProvider>, and any child component reads it with useRouteData().
Both are exported from @qwik-docs/framework/components.
For example, the site title comes from siteTitle and the sidebar navigation comes from the sidebar array.
import { component$ } from '@builder.io/qwik';
import { useRouteData } from '@qwik-docs/framework/components';
export const Custom = component$(() => {
const { siteTitle } = useRouteData();
return <p>The title of this site is "{siteTitle}"</p>;
});
The catch-all route wraps its content in <DocsProvider>, so any component inside the page can call useRouteData().
When you build a custom page, wrap it in <DocsProvider> yourself:
import { component$ } from '@builder.io/qwik';
import { DocsProvider, Page, useRouteData } from '@qwik-docs/framework/components';
export default component$(() => {
const data = useDocsData();
return (
<DocsProvider data={data.value}>
<Page>
<Slot />
</Page>
</DocsProvider>
);
});
If you write code that needs the route data type, import RouteData from @qwik-docs/framework:
import type { RouteData } from '@qwik-docs/framework';
Customizing route data
Route data works out of the box and needs no configuration. For advanced cases you can customize route data for some or all pages with route middleware, ported from Astro Starlight.
Route middleware is a function that runs on every page render and can mutate the route data object before it reaches <DocsProvider>.
This is the data-layer counterpart to component overrides: instead of changing how qwik-docs renders your data, you change the data it renders.
Use it when configuration and frontmatter cannot express what you need — for example to rewrite titles for a section of pages, filter sidebar items, or set editUrl or a banner conditionally.
How to customize route data
-
Create a file exporting an
onRequestfunction with thedefineRouteMiddleware()helper from@qwik-docs/framework:import { defineRouteMiddleware } from '@qwik-docs/framework'; export const onRequest = defineRouteMiddleware(() => {}); -
Point the
routeMiddlewareoption at that file inqwik-docs.config.ts:import { defineConfig } from '@qwik-docs/framework'; export default defineConfig({ title: 'My delightful docs site', routeMiddleware: './src/routeData.ts', }); -
Update
onRequestto modify route data.The first argument is the middleware context. Its
routeproperty is the current page'sRouteDataobject; mutate its fields in place. The context also exposes the requesturl.This example adds an exclamation mark to every page title:
import { defineRouteMiddleware } from '@qwik-docs/framework'; export const onRequest = defineRouteMiddleware((context) => { context.route.title = context.route.title + '!'; });
A worked example
Route middleware runs after generateRouteData, so the route object already carries the derived sidebar, pagination, and edit URL. Read the request url to branch per page.
This middleware points the edit link for every page under /docs/api/ at a generated-source notice, and shows a banner on those pages:
import { defineRouteMiddleware } from '@qwik-docs/framework';
export const onRequest = defineRouteMiddleware((context) => {
if (context.url.pathname.startsWith('/docs/api/')) {
// API pages are generated; send editors to the generator, not the page.
context.route.editUrl = 'https://github.com/my-org/my-repo/tree/main/scripts/gen-api';
context.route.banner = {
content: 'This page is generated. Edit the generator, not the Markdown.',
};
}
});
Because you mutate context.route in place, changes apply to the RouteData that useRouteData() returns in every component on the page.
Multiple route middleware
Set routeMiddleware to an array of paths to run more than one handler.
qwik-docs runs them in order:
import { defineConfig } from '@qwik-docs/framework';
export default defineConfig({
title: 'My site with multiple middleware',
routeMiddleware: ['./src/middleware-one.ts', './src/middleware-two.ts'],
});
Waiting for later route middleware
To wait for middleware later in the stack before your code runs, await the next() callback passed as the second argument.
This is useful when you want another handler's changes in place before you read or adjust route data:
import { defineRouteMiddleware } from '@qwik-docs/framework';
export const onRequest = defineRouteMiddleware(async (context, next) => {
// Wait for later middleware to run.
await next();
// Then modify route data.
context.route.title = context.route.title + '!';
});