1
votes

We'd like to store some site-specific config on the frontend in Spartacus. For example: Each site (of a multisite setup) has a different Google API Key.

Right now, I've built a CONFIG_INITIALIZER factory, like the following. But with the fake scoping and all, it does not seem the correct way to do this.

import { Inject, Injectable } from '@angular/core';
import { ConfigInitializer, ConfigInitializerService, deepMerge } from '@spartacus/core';
import { StoreFinderConfig } from '@spartacus/storefinder/core';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { MultiSiteConfigChunk } from './multisiteconfig-tokens';

@Injectable({ providedIn: 'root' })
export class MultisiteConfigInitializer implements ConfigInitializer {

  // Fake scoping :(
  readonly scopes = ['all'];

  readonly configFactory = () => this.resolveConfig().toPromise();

  constructor(
    protected configInit: ConfigInitializerService,  @Inject(MultiSiteConfigChunk) private multiSiteConfig: Array<any>) {
  }

  protected resolveConfig(): Observable<StoreFinderConfig> {
    return this.configInit.getStable('context.baseSite').pipe(
      map((config) => {
        const mergedConfig = deepMerge(...this.multiSiteConfig);
        return mergedConfig[config.context.baseSite];
      })
    );
  }
}

What would be the recommended way to get be able to do this?

1

1 Answers

0
votes

You can check this doc first: https://sap.github.io/spartacus-docs/automatic-context-configuration/#adding-a-custom-context

If you are using automatic context configuration, you also can try this:

  1. extends SiteContextConfigInitializer (inject your multiSiteConfig in the child class); replace SiteContextConfigInitializer using the child in providers.
  2. replace getConfig function in child class to add the Google API Key to site-context.