Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions ilc/server/registry/Registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ module.exports = class Registry {
return data.find((item) => item.domainName === domainName)?.id;
}

/**
*
* @param {String} domainName
* @returns {String | undefined} alias
*/
async resolveDomainAlias(domainName) {
const { data } = await this.getRouterDomains();
return data.find((item) => item.domainName === domainName)?.alias ?? undefined;
}

/**
* Fetch config from registry
* @param {String} options.filter.domain
Expand Down
1 change: 1 addition & 0 deletions ilc/server/routes/wildcardRequestHandlerFactory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ describe('wildcardRequestHandlerFactory', () => {
apps: {},
}),
resolveDomainId: sinon.stub().resolves(1),
resolveDomainAlias: sinon.stub().resolves('test-alias'),
getTemplate: sinon.stub().resolves({ data: { content: 'template', styleRefs: [] }, cachedAt: Date.now() }),
} as any;

Expand Down
11 changes: 8 additions & 3 deletions ilc/server/routes/wildcardRequestHandlerFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,14 @@ export function wildcardRequestHandlerFactory(
newrelic.getTransaction().ignore();
}

const domainId = overrideConfigs ? await registryService.resolveDomainId(currentDomain) : undefined;

const finalRegistryConfig = mergeConfigs(registryConfig, overrideConfigs, domainId);
const [domainId, domainAlias] = overrideConfigs
? await Promise.all([
registryService.resolveDomainId(currentDomain),
registryService.resolveDomainAlias(currentDomain),
])
: [undefined, undefined];

const finalRegistryConfig = mergeConfigs(registryConfig, overrideConfigs, domainId, domainAlias);
req.raw.registryConfig = finalRegistryConfig;

const unlocalizedUrl = i18n.unlocalizeUrl(finalRegistryConfig.settings.i18n, url);
Expand Down
100 changes: 100 additions & 0 deletions ilc/server/tailor/merge-configs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,106 @@ describe('merge configs', () => {
expect(result.routes.find((r) => r.route === '/other-domain-only')).to.be.undefined;
});

it('should add override route matched by domainAlias', () => {
const overrideConfig = {
routes: [
{
routeId: 99,
route: '/alias-only',
next: false,
orderPos: 50,
slots: {},
meta: {},
versionId: 'v1.0.99',
domainAlias: 'my-shop',
},
],
};

const result = mergeConfigs(registryConfig, overrideConfig, 5, 'my-shop');

const [commonRoute, constRoute, willChangeRoute] = registryConfig.routes;
expect(result.routes).to.eql([
commonRoute,
constRoute,
{
routeId: 99,
route: '/alias-only',
next: false,
orderPos: 50,
slots: {},
meta: {},
versionId: 'v1.0.99',
domainAlias: 'my-shop',
},
willChangeRoute,
]);
});

it('should merge override route into existing route when matched by domainAlias', () => {
const overrideConfig = {
routes: [
{
routeId: 2,
route: '/const',
next: true,
domainAlias: 'my-shop',
},
],
};

const result = mergeConfigs(registryConfig, overrideConfig, 5, 'my-shop');

const constRoute = result.routes.find((r) => r.routeId === 2);
expect(constRoute?.next).to.be.true;
expect(result.routes).to.have.lengthOf(registryConfig.routes.length);
});

it('should not apply domainAlias override route when alias does not match', () => {
const overrideConfig = {
routes: [
{
routeId: 99,
route: '/alias-only',
next: false,
orderPos: 50,
slots: {},
meta: {},
versionId: 'v1.0.99',
domainAlias: 'other-shop',
},
],
};

const result = mergeConfigs(registryConfig, overrideConfig, 5, 'my-shop');

expect(result.routes).to.have.lengthOf(registryConfig.routes.length);
expect(result.routes.find((r) => r.route === '/alias-only')).to.be.undefined;
});

it('should match by domainAlias when both domainAlias and domainId are on the override route', () => {
// domainAlias takes precedence — domainId is ignored when alias is present
const overrideConfig = {
routes: [
{
routeId: 99,
route: '/alias-wins',
next: false,
orderPos: 50,
slots: {},
meta: {},
versionId: 'v1.0.99',
domainAlias: 'my-shop',
domainId: 999, // wrong id — should be ignored
},
],
};

const result = mergeConfigs(registryConfig, overrideConfig, 5, 'my-shop');

expect(result.routes.find((r) => r.route === '/alias-wins')).to.exist;
});

it('should handle routes with same pattern but different orderPos including wildcard routes', () => {
const registryConfigWithWildcardRoutes: TransformedRegistryConfig = {
...registryConfig,
Expand Down
10 changes: 8 additions & 2 deletions ilc/server/tailor/merge-configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type DeepPartial<T> = T extends object
}
: T;

type OverrideRoute = DeepPartial<TransformedRoute> & { domainId?: number };
type OverrideRoute = DeepPartial<TransformedRoute> & { domainId?: number; domainAlias?: string };
export type OverrideConfig = DeepPartial<Pick<TransformedRegistryConfig, 'apps'>> & {
routes?: OverrideRoute[];
sharedLibs?: {
Expand All @@ -21,6 +21,7 @@ export function mergeConfigs(
original: TransformedRegistryConfig,
override: OverrideConfig | null | undefined,
domainId?: number,
domainAlias?: string,
): TransformedRegistryConfig {
if (!override || (!override.apps && !override.routes && !override.sharedLibs)) {
return original;
Expand All @@ -35,7 +36,12 @@ export function mergeConfigs(
? overrideRoute.route === originalRoute.route && overrideRoute.orderPos === originalRoute.orderPos
: overrideRoute.route === originalRoute.route;

const isSameDomainRoute = (route: OverrideRoute) => route.domainId === undefined || route.domainId === domainId;
const isSameDomainRoute = (route: OverrideRoute) => {
if (route.domainAlias !== undefined) {
return route.domainAlias === domainAlias;
}
return route.domainId === undefined || route.domainId === domainId;
};

const routes = override.routes
? [
Expand Down
1 change: 1 addition & 0 deletions ilc/server/types/Registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ export interface Registry {

getTemplate(templateName: string, options?: TemplateOptions): Promise<CacheResult<Template>>;
resolveDomainId(domainName: string): Promise<number>;
resolveDomainAlias(domainName: string): Promise<string | undefined>;
}
Loading