- Developer guides
Angular middleware
The src/server.ts file in a Content SDK Angular application contains customizable server-side pre-processing logic for incoming requests. This logic runs through Express.js middleware. This topic describes the built-in middleware and their roles.
Middleware overview
The following table summarizes each middleware, when it runs, and its purpose. For detailed configuration options, see Middleware reference.
Middleware | Server navigation | Browser navigation | Editing/preview modes | Description |
|---|---|---|---|---|
Loader data service | ✓ | Provides layout, dictionary, and other loader data during browser navigation. | ||
Personalize | ✓ | ✓ | Resolves personalization data for the current route. | |
Multisite | ✓ | ✓ | Resolves the target site by hostname in multi-site configurations. | |
Bot detection | ✓ | ✓ | Detects bot requests and reports bot page views, skipping personalization for bot traffic. | |
Redirects | ✓ | ✓ | Matches requests against SXA redirect items and redirects when there's a match. | |
Editing config | Handles | |||
Editing render | ✓ | Handles | ||
Cache revalidate | Processes | |||
Sitemap | Generates and serves XML sitemaps. | |||
Robots | Generates and serves the |
Middleware reference
The following sections explain the middleware available in the Content SDK for Angular.
Loader data service middleware
This middleware is required for the Angular application to function correctly. Do not remove it. The Loader data service middleware supplies layout, dictionary, and other loader data to Content SDK loaders during browser navigation.
Register this middleware in server.ts using the createLoaderDataServiceMiddleware() factory function.
Options reference
Property | Type | Description |
|---|---|---|
|
| The loader registry, equivalent to Required |
|
| The loader cache instance initialized earlier in Required |
Personalize middleware
The Personalize middleware resolves personalization data for the current route. The resulting variantId is used when resolving layout data from the SitecoreAI Edge endpoint.
Register this middleware in server.ts using the createPersonalizeMiddleware() factory function.
Options reference
Property | Type | Description |
|---|---|---|
|
| Enables or disables the middleware. Optional |
|
| A callback that skips middleware execution for specific requests. Optional |
|
| Configures path inclusion and exclusion rules for the middleware. Optional |
|
| Personalization settings. Maps to the Required |
|
| API endpoint settings. Maps to the Required |
|
| List of locales for personalization. Optional |
|
| Fallback language. Default: Optional |
|
| Fallback site name. Optional |
|
| A custom personalize service instance. Optional |
|
| Returns additional UTM parameters to include in personalization requests. Optional |
Multisite middleware
The Multisite middleware resolves the target site for an incoming request based on hostname. Use this middleware when you have multiple sites configured in Sitecore.
Register this middleware in server.ts using the createMultisiteMiddleware() factory function.
Options reference
Property | Type | Description |
|---|---|---|
|
| Enables or disables the middleware. Optional |
|
| When it returns Optional |
|
| The list of sites to consider during resolution. Optional |
|
| Fallback site name when resolution fails. Optional |
Editing config middleware
The Editing config middleware processes /api/editing/config requests used internally by Pages Editor. We recommend that you do not customize this middleware.
Register this middleware in server.ts using the createEditingConfigMiddleware() factory function.
Options reference
Property | Type | Description |
|---|---|---|
|
| The component map, equivalent to Required |
|
| Inline metadata. If set, Optional |
|
| A dynamic function for metadata resolution. Optional |
Editing render middleware
The Editing render middleware processes /api/editing/render requests that enable editing in Pages Editor.
Register this middleware in server.ts using the createEditingRenderMiddleware() factory function.
Options reference
Property | Type | Description |
|---|---|---|
|
| Remaps the editor's route query parameter to a custom URL. Optional |
|
| Extra query parameters propagated into the editing preview data, such as deployment-protection bypass tokens. Optional |
Cache revalidate middleware
The Cache revalidate middleware processes the /api/revalidate endpoint. You can use this endpoint in combination with webhooks to revalidate loader cache entries. For more information, see Loaders cache layer and tags revalidation.
Register this middleware in server.ts using the createSitecoreRevalidateMiddleware() factory function.
Options reference
Property | Type | Description |
|---|---|---|
|
| The loader cache instance initialized earlier in Required |
|
| The revalidation secret. Defaults to Optional |
|
| Fallback locale. Defaults to Optional |
Sitemap middleware
The Sitemap middleware generates and serves XML sitemaps at /sitemap.xml and numbered /sitemap-{id}.xml paths.
Register this middleware in server.ts using the createSitemapMiddleware() factory function.
Options reference
Property | Type | Description |
|---|---|---|
|
| The Sitecore client instance. Required |
|
| The list of sites to include in the sitemap. Required |
Robots middleware
The Robots middleware generates and serves the robots.txt file at the /robots.txt path.
Register this middleware in server.ts using the createRobotsMiddleware() factory function.
Options reference
Property | Type | Description |
|---|---|---|
|
| The Sitecore client instance. Required |
|
| The list of sites to use for Required |
Bot detection middleware
This middleware runs bot detection once per request. It detects bots based on the User-Agent, marks the request/response with the sc_bot cookie, and dispatches a dedicated bot page-view event for the resolved site/language. For more information, see Lightweight tracking.
Register this middleware in server.ts using the createBotTrackingMiddleware() factory function.
Options reference
Property | Type | Description |
|---|---|---|
|
| Edge context ID. Required to send bot page-view events. Optional |
|
| Edge client context ID. Optional |
|
| URL of the SitecoreAI Edge endpoint that bot page-view events are sent to. Optional |
|
| Locales used to extract the language from the request path. Optional |
|
| Fallback language when the request path has no locale prefix. Default: Optional |
|
| Fallback site name when not resolved by the multisite middleware or the site cookie. Optional |
This middleware does not accept an enabled option. It is skipped automatically for editing/preview requests, non-matching paths, prefetch requests, and on localhost or in a development environment, unless the SITECORE_ENABLE_BOT_TRACKING environment variable is set to true.
Redirects middleware
The Redirects middleware matches the current request against SXA redirect items and redirects the request when there's a matching item.
Register this middleware in server.ts using the createRedirectsMiddleware() factory function.
This logic runs on both server and browser navigation. However, due to Angular routing API limitations, 301/302 redirects produce browser navigation only.
Options reference
Property | Type | Description |
|---|---|---|
|
| Enables or disables the middleware. Optional |
|
| Edge context ID used to fetch redirects, unless local API configuration is provided instead. Optional |
|
| Edge client context ID. Optional |
|
| URL of the SitecoreAI Edge endpoint that redirects are fetched from. Optional |
|
| Local API hostname used to fetch redirects from a local instance instead of Edge. Optional |
|
| Local API key, used together with Optional |
|
| GraphQL endpoint path appended to Optional |
|
| Locales configured for the app, used to match locale-versioned redirect rules. Derived from Optional |
|
| Fallback language when the request path has no locale prefix. Default: Optional |
|
| Fallback site name when not resolved by the multisite middleware or the site cookie. Optional |
|
| Sites used to resolve the target site's default language for the Optional |
|
| Override the redirects service instance, for example for testing. Optional |