Skip to content

App providers and SSR APIs

The providers that configure the documentation application, and the APIs for server-side rendering. Add the providers to your application configuration (Installation [4. Add the providers]).

Providers

ProviderPackageRequiredDescription
provideNgDocContext()@ng-doc/generatedYesProvides the generated navigation and site data.
provideNgDocApp(config?)@ng-doc/appYesConfigures the application. See App providers and SSR APIs [provideNgDocApp].
provideSearchEngine(engine, ...args)@ng-doc/appFor searchProvides the search engine (Search and command palette).
providePageSkeleton(skeleton)@ng-doc/appYesSets the components around the page content (Page skeleton).
provideMainPageProcessor(processors)@ng-doc/appYesRegisters the main processors, usually NG_DOC_DEFAULT_PAGE_PROCESSORS.
providePageProcessor(processors)@ng-doc/app–Registers your own processors, after the main ones (Custom page components).
provideTypeControl(type, control, options?)@ng-doc/app–Registers a playground control for a type (Custom type controls).
providePlaygroundDemo(playgroundId, component)@ng-doc/app–Registers the component of a playground. Generated code calls it; you don't need to.
provideMermaid(config?)@ng-doc/appFor diagramsEnables Mermaid diagrams (Diagrams).

The routes of the site are in NG_DOC_ROUTING, also from @ng-doc/generated. The application also needs the Angular router and provideHttpClient(withInterceptorsFromDi()), because NgDoc registers an HTTP interceptor.

provideNgDocApp

provideNgDocApp(config?: NgDocApplicationConfig). See also NgDocApplicationConfig.

OptionTypeDefaultDescription
contentAnchorScrollingbooleanfalseScrolls to the anchor in the URL again after NgDoc content has loaded. Use it with the router's anchorScrolling.
contentScrollPositionRestoration'enabled' | 'top' | 'disabled''disabled'Restores the scroll position after NgDoc content has loaded. Use the same value as the router's scrollPositionRestoration.
uiKitNgDocUiConfigSee belowPaths of the UI kit assets.
shikiNgDocHighlighterConfig–Extra Shiki themes to load in the browser (themes). theme is deprecated and ignored.
shortcutsbooleantrueWhether single-key shortcuts are on for readers who haven't chosen (Search and command palette [Keyboard shortcuts]). ⌘K always works.

NgDocUiConfig has two fields. Set both when you pass uiKit:

FieldDefaultDescription
assetsPath'assets/ng-doc/ui-kit'Where the UI kit assets are served.
customIconsPath'assets/icons'Where your own SVG icons are (Icons).

The site uses the two scrolling options with the matching router options:

app.config.tsTypeScript
provideNgDocApp({
  contentAnchorScrolling: true,
  contentScrollPositionRestoration: 'enabled',
});

provideSearchEngine

provideSearchEngine(engine, ...args) creates the engine class with the given arguments. The default engine is NgDocDefaultSearchEngine, and its first argument is an NgDocDefaultSearchEngineOptions object:

OptionTypeDefaultDescription
stemmerA stemmer from @orama/stemmers–Stems words. Without it, words are not stemmed.
limitnumber10The most results to return.
tolerancenumber–The number of typos to allow. It doesn't work with exact.
exactboolean–Returns only exact matches.

The engine returns the matching guides first, then the matching API pages, each in order of relevance. Guides take up to half of limit, or more when fewer API pages match, and API pages fill the rest.

app.config.tsTypeScript

To use another engine, extend NgDocSearchEngine and pass your class.

providePageSkeleton

providePageSkeleton(skeleton: NgDocPageSkeleton). There is no default skeleton: pass NG_DOC_DEFAULT_PAGE_SKELETON for the standard components, or your own object.

Leave a field out to remove that part of the page.

Page processors

provideMainPageProcessor(processors) registers the processors you pass. Pass NG_DOC_DEFAULT_PAGE_PROCESSORS for the standard ones, which render links, icons, heading anchors, callouts, tooltips, diagrams, code blocks, demos, demo panes, playgrounds, tabs, images and the members tables of API pages. providePageProcessor(processors) adds your own, which run after the main processors. Each processor is an NgDocPageProcessor:

FieldTypeDescription
componentType<T>The component that replaces the element.
selectorstringThe CSS selector of the elements to replace.
extractOptions(element, root) => NgDocProcessorOptions<T>Reads the inputs and content from the element.
nodeToReplace(element, injector) => ElementChooses another element to replace. Optional.

provideTypeControl

provideTypeControl(type: string, control, options?) registers a playground control for inputs of the given type name.

OptionTypeDescription
hideLabelbooleanHides the input name next to the control.
ordernumberThe position of the control. The built-in controls use 10 to 40.

Preload pages

A page's code and content load when the reader opens it. To have them ready before that, set NgDocPreloadingStrategy as the router's preloading strategy:

app.config.tsTypeScript

NgDoc then preloads a page when the reader points at, focuses or touches a link to it anywhere on the site: in the sidebar, the previous and next page links, the page content, the search results and the table of contents. After a guide renders, its previous and next guides are preloaded once the browser is idle. Each page is preloaded once, and only the pages the reader is about to open: the strategy never loads every page, as PreloadAllModules would. Nothing is preloaded on the server, when the reader has turned on data saving, or on a 2G connection. NgDocRoutePreloader also preloads a page on demand, with preload(url).

Without the strategy, pages still open without a blank page in between: the router keeps the current page on screen until the next page and its content have loaded.

Server-side rendering

APIPackageDescription
withNgDocContentReady@ng-doc/appWraps the server bootstrap function. It waits until NgDoc content has loaded, and fails the render if content failed.
NgDocContentState@ng-doc/appThe content failures of the current request. withNgDocContentReady reads it.
NG_REQUEST_BASE_PATH@ng-doc/ui-kitThe base path of the current request: the <base href> or APP_BASE_HREF on the server, '' in the browser. NgDoc uses it to request assets.

Wrap the bootstrap function in main.server.ts:

main.server.tsTypeScriptLines 2, 9
import { bootstrapApplication, BootstrapContext } from '@angular/platform-browser';
import { withNgDocContentReady } from '@ng-doc/app';

import { App } from './app/app';
import { config } from './app/app.config.server';

const bootstrap = (context: BootstrapContext) => bootstrapApplication(App, config, context);

export default withNgDocContentReady(bootstrap);

Without it, the server can return a page before its content has loaded. Production builds and prerendering describes prerendering.

Edit this page