Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MWPW-159595] - support same domain consumers in the links conversion feature #3067

Merged
merged 8 commits into from
Oct 29, 2024
8 changes: 5 additions & 3 deletions libs/scripts/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ const stageDomainsMap = {
},
'.business-graybox.adobe.com': { 'business.adobe.com': 'origin' },
// TODO: remove after QE approval
'--milo--robert-bogos.hlx.page': {
'www.adobe.com': 'main--dc--adobecom.hlx.page',
'business.adobe.com': 'business.stage.adobe.com',
'^https://.*--milo--robert-bogos.hlx.page': {
'^https://www.adobe.com/acrobat': 'https://main--dc--adobecom.hlx.page',
'^https://business.adobe.com(?!/blog)': 'https://business.stage.adobe.com',
'^https://business.adobe.com/blog': 'https://main--bacom-blog--adobecom.hlx.page',
'^https://www.adobe.com': 'origin',
},
};

Expand Down
19 changes: 11 additions & 8 deletions libs/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -644,19 +644,22 @@ const decorateCopyLink = (a, evt) => {
});
};

export function convertStageLinks({ anchors, config, hostname }) {
export function convertStageLinks({ anchors, config, hostname, href }) {
if (config.env?.name === 'prod' || !config.stageDomainsMap) return;
const matchedRules = Object.entries(config.stageDomainsMap)
.find(([domain]) => hostname.includes(domain));
.find(([domain]) => (new RegExp(domain)).test(href));
if (!matchedRules) return;
const [, domainsMap] = matchedRules;
[...anchors].forEach((a) => {
const matchedDomain = Object.keys(domainsMap)
.find((domain) => a.href.includes(domain));
.find((domain) => (new RegExp(domain)).test(a.href));
if (!matchedDomain) return;
a.href = a.href.replace(a.hostname, domainsMap[matchedDomain] === 'origin'
? hostname
: domainsMap[matchedDomain]);
a.href = a.href.replace(
new RegExp(matchedDomain),
domainsMap[matchedDomain] === 'origin'
? `${matchedDomain.includes('https') ? 'https://' : ''}${hostname}`
: domainsMap[matchedDomain],
);
if (/(\.page|\.live).*\.html(?=[?#]|$)/.test(a.href)) a.href = a.href.replace(/\.html(?=[?#]|$)/, '');
});
}
Expand All @@ -665,7 +668,7 @@ export function decorateLinks(el) {
const config = getConfig();
decorateImageLinks(el);
const anchors = el.getElementsByTagName('a');
const { hostname } = window.location;
const { hostname, href } = window.location;
const links = [...anchors].reduce((rdx, a) => {
appendHtmlToLink(a);
a.href = localizeLink(a.href);
Expand Down Expand Up @@ -702,7 +705,7 @@ export function decorateLinks(el) {
}
return rdx;
}, []);
convertStageLinks({ anchors, config, hostname });
convertStageLinks({ anchors, config, hostname, href });
return links;
}

Expand Down
43 changes: 42 additions & 1 deletion test/utils/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ const stageDomainsMap = {
},
'.business-graybox.adobe.com': { 'business.adobe.com': 'origin' },
};
const stageDomainsMapWRegex = {
hostname: 'stage--milo--owner.hlx.page',
map: {
'^https://.*--milo--owner.hlx.page': {
'^https://www.adobe.com/acrobat': 'https://main--dc--adobecom.hlx.page',
'^https://business.adobe.com/blog': 'https://main--bacom-blog--adobecom.hlx.page',
'^https://business.adobe.com': 'https://business.stage.adobe.com',
'^https://www.adobe.com': 'origin',
},
},

};
const prodDomains = ['www.adobe.com', 'business.adobe.com', 'blog.adobe.com', 'helpx.adobe.com', 'news.adobe.com'];
const externalDomains = ['external1.com', 'external2.com'];
const ogFetch = window.fetch;
Expand Down Expand Up @@ -521,7 +533,7 @@ describe('Utils', () => {
});

describe('stageDomainsMap', () => {
it('should convert links when stageDomainsMap provided', async () => {
it('should convert links when stageDomainsMap provided without regex', async () => {
const stageConfig = {
...config,
env: { name: 'stage' },
Expand All @@ -536,6 +548,35 @@ describe('Utils', () => {
anchors: [...anchors, ...externalAnchors],
config: stageConfig,
hostname,
href: `https://${hostname}`,
});

anchors.forEach((a, index) => {
const expectedDomain = Object.values(domainsMap)[index];
expect(a.href).to.contain(expectedDomain === 'origin' ? hostname : expectedDomain);
});

externalAnchors.forEach((a) => expect(a.href).to.equal(a.href));
});
});

it('should convert links when stageDomainsMap provided with regex', async () => {
const { hostname, map } = stageDomainsMapWRegex;
const stageConfigWRegex = {
...config,
env: { name: 'stage' },
stageDomainsMap: map,
};

Object.entries(map).forEach(([, domainsMap]) => {
const anchors = Object.keys(domainsMap).map((d) => utils.createTag('a', { href: d.replace('^', '') }));
const externalAnchors = externalDomains.map((url) => utils.createTag('a', { href: url }));

utils.convertStageLinks({
anchors: [...anchors, ...externalAnchors],
config: stageConfigWRegex,
hostname,
href: `https://${hostname}`,
});

anchors.forEach((a, index) => {
Expand Down
Loading