# Pre-rendering

> Render a RapiDoc API reference to HTML at build time with @rapidoc/prerender: Declarative Shadow DOM that shows before any JavaScript runs, and that search engines can read.

`<rapi-doc>` renders in the browser, so until its script runs the page is empty, and a crawler that doesn't run JavaScript sees nothing. `@rapidoc/prerender` renders the same templates at build time, in Node or Bun, and returns HTML: the reference shows on the first paint, and search engines index it. Load `rapidoc` in the page too, and the element takes over the pre-rendered markup for **Try**, search and navigation.

```bash
npm install @rapidoc/prerender
```

It needs Node 22.12 or later.

## Render the whole reference

`renderRapiDoc` returns a `<rapi-doc>` element with its content inside, as [Declarative Shadow DOM](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/template#shadowrootmode): the browser attaches the shadow root while parsing, before any script runs.

<Tabs syncKey="prerender">
  <TabItem label="Node or Bun">
```javascript title="prerender.mjs"
import fs from 'node:fs';
import { renderRapiDoc } from '@rapidoc/prerender';

const reference = await renderRapiDoc({
  spec: './specs/petstore.yaml', // a file, a URL, spec text or a spec object
  renderStyle: 'read',
  theme: 'ember',
  colorScheme: 'dark',
});

fs.writeFileSync('./dist/api.html', `<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>API Reference</title>
    <script type="module" src="https://unpkg.com/rapidoc/dist/rapidoc-min.js"></script>
  </head>
  <body>${reference}</body>
</html>`);
```
  </TabItem>
  <TabItem label="Astro">
```astro title="src/pages/api.astro"
---
import { renderRapiDoc } from '@rapidoc/prerender';

const reference = await renderRapiDoc({ spec: './public/specs/petstore.json', renderStyle: 'read' });
---

<html lang="en">
  <head>
    <meta charset="utf-8" />
    <script type="module" src="https://unpkg.com/rapidoc/dist/rapidoc-min.js"></script>
  </head>
  <body>
    <Fragment set:html={reference} />
  </body>
</html>
```
  </TabItem>
  <TabItem label="Next.js">
```tsx title="app/api-reference/page.tsx"
import { renderRapiDoc } from '@rapidoc/prerender';
import Script from 'next/script';

export default async function ApiReferencePage() {
  const reference = await renderRapiDoc({ spec: 'https://api.example.com/openapi.json' });

  return (
    <main>
      <Script type="module" src="https://unpkg.com/rapidoc/dist/rapidoc-min.js" />
      <div dangerouslySetInnerHTML={{ __html: reference }} />
    </main>
  );
}
```
  </TabItem>
</Tabs>

The output looks like this:

```html
<rapi-doc render-style="read" theme="ember" color-scheme="dark">
  <template shadowrootmode="open">
    <style>/* the element's styles */</style>
    <!-- navigation, tags, operations and schemas, as the element renders them -->
  </template>
</rapi-doc>
```

Only the options you pass become attributes. For everything else the element uses its own defaults, and the HTML is rendered with those same defaults, so nothing changes when the element takes over.

With `outputMode: 'html'` (or `'light'`) you get plain light-DOM HTML in a `<div class="rapidoc-prerender-container">` with scoped styles instead: static, with nothing for the element to take over.

## Render one endpoint or one schema

`renderEndpoint` and `renderSchema` render a single operation or schema, for a guide or a blog post built at build time. Their output is light-DOM HTML with its own styles, so it needs no JavaScript at all. This card was rendered when this site was built:

<PrerenderEndpointDemo />

```javascript
import { renderEndpoint, renderSchema } from '@rapidoc/prerender';

const endpoint = await renderEndpoint({
  spec: './specs/logistics.yaml',
  path: '/shipments/{shipmentId}',
  method: 'get',
  allowTry: false,
});

const schema = await renderSchema({
  spec: './specs/logistics.yaml',
  schema: 'ShipmentResponse', // a name under components/schemas, or a schema object
  schemaStyle: 'table',       // or 'tree'
});
```

For an interactive single endpoint in the browser instead, use [`<rapi-doc-mini>`](/docs/rapidoc-mini/).

## Options

Options are camelCase and have the names of `<rapi-doc>`'s properties: `schemaStyle` for the `schema-style` attribute, `usePathInNavBar` for `use-path-in-nav-bar`. An option that is also an attribute of `<rapi-doc>` can be written in the attribute's kebab-case form too (`'schema-style': 'tree'`). Options of this package only, such as `outputMode` or `basePath`, have their camelCase name alone.

A name a function does not know is an error that lists the valid names, so a typo is caught at build time. `renderRapiDoc` is the exception: any other name is taken for an attribute of `<rapi-doc>` and written to the element.

### `renderRapiDoc`

The defaults are those of `<rapi-doc>`.

| Option | Default | |
| :--- | :--- | :--- |
| `spec` | (required) | A file path, a URL, spec text (JSON or YAML) or a spec object. |
| `renderStyle` | `'read'` | `'read'`, `'view'` or `'focused'`. |
| `layout` | `'row'` | `'row'` or `'column'`: request and response side by side, or one above the other. |
| `theme` | `'amber'` | A preset (`ember`, `carbon`, `cobalt`, `jade`, `indigo`, `amber`) or a hex color. |
| `colorScheme` | `'dark'` | `'dark'`, `'light'`, `'system'` or `'auto'`. |
| `schemaStyle` | `'tree'` | `'tree'` or `'table'`. |
| `schemaExpandLevel` | `99999` | How many schema levels start expanded. |
| `defaultSchemaTab` | `'example'` | `'example'` or `'schema'`. |
| `showHeader`, `showSideNav`, `showInfo` | `true` | Show or hide those parts, as on the element. |
| `allowTry`, `allowSearch`, `allowAdvancedSearch`, `allowServerSelection`, `allowAuthentication` | `true` | Include those features, as on the element. |
| `focusedElementId` | `'overview'` | The operation shown first in focused mode. |
| `pageDirection` | `'ltr'` | `'ltr'` or `'rtl'`. |
| `outputMode` | `'dsd'` | `'dsd'` (Declarative Shadow DOM), or `'html'` / `'light'` for plain light-DOM HTML. |
| `includeStyles` | `true` | Inline the element's styles in the output. |
| `inlineSpec` | `true` | With a spec object, inline it in the output, so the element doesn't fetch it again. |
| any other attribute of `<rapi-doc>` | | Written to the element. |

### How the spec is read

These are accepted by `renderRapiDoc`, `renderEndpoint`, `getNavBarHtml` and `getSpecNavigation`, with the meaning they have on `<rapi-doc>`:

| Option | Default | |
| :--- | :--- | :--- |
| `filterOperations` | | Which operations to keep, as [`filter-operations`](/docs/rapidoc/reference/#filter-operations). |
| `sortTags`, `sortSchemas` | `false` | Sort tags or schemas by name. |
| `sortEndpointsBy` | `'none'` | `'path'`, `'method'`, `'summary'` or `'none'` (the spec's order). The element's own default is `'path'`: pass the option to get the same order before and after the element loads. |
| `generateMissingTags` | `false` | Give an operation without a tag one named after its path. |
| `showUnusedTags` | `false` | Keep tags no operation uses when they have a description. |
| `apiKeyName`, `apiKeyLocation`, `apiKeyValue`, `serverUrl` | | As on the element. |

### `renderEndpoint` and `renderSchema`

Their output is static HTML that no element takes over, so their defaults are their own.

| Function | Options |
| :--- | :--- |
| `renderEndpoint` | `spec`, `path`, `method` (all required), `renderStyle` (`'read'`), `layout` (`'column'`), `schemaStyle` (`'table'`), `allowTry` (`false`), `theme`, `colorScheme`, `includeStyles`, and the options of the table above. |
| `renderSchema` | `schema` (required: a name or a schema object), `spec` (required with a name), `schemaStyle` (`'table'`), `schemaExpandLevel`, `theme`, `colorScheme`, `includeStyles`. |

The types are in the package's `index.d.ts`.

## Errors

A spec that cannot be loaded stops the build with an error that says what was being loaded and why: `Cannot load the OpenAPI spec: file not found: ./specs/petsotre.yaml`, a URL that does not answer, or text that is not an OpenAPI document. A string is taken for a URL when it starts with `http://` or `https://`, for spec text when it starts with `{` or `[` or has more than one line, and for a local file path otherwise. The functions never return an empty page or an empty sidebar for a spec they could not read.

## A static sidebar

For a site that draws its own navigation beside a headless `<rapi-doc>`, as the [RapiDoc Portal](https://portal.rapidocui.com/) does, the package also renders the sidebar: `getNavBarHtml(spec, options)` returns its HTML, `getNavCss()` and `getThemeTokensCss(theme)` its styles, and `bindNavBar(sidebar, rapiDoc)` ties it to the element in the browser, so clicks scroll the reference and the current operation is highlighted.

| Option of `getNavBarHtml` | Default | |
| :--- | :--- | :--- |
| `basePath` | `''` | The prefix of every link, such as `/apis/orders`. A closing slash is ignored. |
| `routingMode` | `'single-page'` | `'single-page'`: links are `<basePath>#<operation>`. `'tag-pages'`: `<basePath>/<tag>#<operation>`, for a site with a page per tag. |
| `activeHref` | | The current URL or anchor; its item is marked active. |
| `showMethodInNavBar` | `'as-colored-text'` | `'false'`, `'as-plain-text'`, `'as-colored-text'` or `'as-colored-block'`, as on the element. |
| `usePathInNavBar` | `false` | Show each operation's path instead of its summary. |
| `showBadgesInNavBar` | `'dot'` | `'dot'` marks items whose operation shows a badge; `'false'` turns that off. |
| `methodBadgeFormat` | `'3-char'` | With `'as-colored-block'`: `'3-char'` (`DEL`) or `'full'` (`DELETE`). |
| `includeSearch`, `searchPlaceholder` | `true`, `'Filter...'` | The filter box above the list. |
| `includeOverview` | `true` | The link to the API's overview. |
| `backToDocsHref`, `backToDocsLabel` | | A link below the list. |
| `itemClassName` | | A class added to every operation link. |

`getSpecNavigation(spec)` returns the same tags and operations as data, to build your own: `title`, `version`, `description`, `tagGroups`, `tags` and `remainingTags`. Each tag has `name`, `displayName`, `description`, `elementId`, `expanded` and `paths`, its operations, each with exactly `method`, `path`, `summary`, `description`, `elementId` (its anchor in `<rapi-doc>`), `operationId`, `deprecated` and `isWebhook`. Operations and tags marked `x-internal` or `x-hidden` are left out.

> **Note:**
> The pre-rendered HTML comes from the same templates as the element, so the two match.
