Skip to main content

CMSModule

The CMSModule is the central piece of the server-side CMS integration. It handles the registration of all controllers, routes, and services required for the CMS functionality to work on the server.

Usage

You must register the CMSModule in the initModules method of your main Server class. It requires a configured CMSProvider instance to be passed in its options.

// src/shop/server/module/server.tsx
import { CMSModule } from '@archibald/cms';
import { CommerceCMSProvider } from '@archibald/commerce/cms';
import { CoreServer } from '@archibald/server';

class Server extends CoreServer {
public async initModules() {
const { hybris } = this.configService.get();
await this.registerModules([
new CMSModule({ provider: new CommerceCMSProvider({ config: hybris.api }) })
]);
}
}

Options

ParameterTypeDescription
providerCMSProviderThe CMS provider instance that will handle the actual data fetching from the CMS.

Deep Dive

Description: The CMSModule acts as a plug-and-play component for your Archibald server. By registering it, you are instantly equipping your server with all the necessary endpoints, services, and controllers for handling CMS data requests from the client. It's a self-contained unit that registers its own dependencies with the CoreServer.

Registration Lifecycle

The module's register method is called by the CoreServer during the initModules phase of the server startup. This method orchestrates the setup of the module's components in a specific order:

public override async register(server: CoreServer) {
// 1. Registers internal services needed by the module.
await this.registerServices(server);
// 2. Registers decorators like CookieDecorator.
await this.registerDecorators(server);
// 3. Registers the CMSController.
await this.registerControllers(server);
// 4. Registers the routes defined in CMSRouteConfig.
await this.registerRoutes(server);

// 5. Attaches the provider after the server has started.
server.internal.ext('onPostStart', async () => {
this.cmsService.registerProvider(this.options.provider);
});
}
  • Internal Dependencies: The registerServices method registers not only the CMSService but also other framework services it depends on, such as CacheService, HapiService, and HttpService.

  • Provider Registration: Crucially, the CMSProvider is not registered immediately. Instead, it's attached during the onPostStart lifecycle event. This ensures that all services and server components are fully initialized before the provider, which may depend on them, is made available to the CMSService.

  • Best Practice: Only one CMSModule should be registered per server instance. Registering multiple CMSModule instances would cause conflicts in route definitions and service registrations. If you need to fetch data from multiple CMS systems simultaneously, you should create a single "composite" CMSProvider that internally calls the other providers.