Skip to content

Commit 93e5704

Browse files
sheridansunierSheridan Sunier
andauthored
MWPW-188023: prevent cors error between bacom and news: (for stage) (#5422)
* federated-news * testing * MWPW-188023: seeing if it works * typo --------- Co-authored-by: Sheridan Sunier <sunier@Sheridans-MacBook-Pro.local>
1 parent fd5c52e commit 93e5704

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

libs/blocks/caas/utils.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -546,8 +546,9 @@ const isLocaleInRegionalSites = (regionalSites, locStr) => {
546546
.includes(locStr);
547547
};
548548

549-
async function getIsLingoLocale(origin, country, language) {
550-
const response = await fetch('https://www.adobe.com/federal/assets/data/lingo-site-mapping.json');
549+
async function getIsLingoLocale(origin, country, language, fqdn = 'www.adobe.com') {
550+
if (origin === 'news') return true;
551+
const response = await fetch(`https://www.adobe.com/federal/assets/data/lingo-site-mapping.json?${fqdn}`);
551552
if (!response.ok) throw new Error(`HTTP ${response.status}`);
552553
const configJson = await response.json();
553554

@@ -593,15 +594,16 @@ async function getIsLingoLocale(origin, country, language) {
593594
}
594595

595596
async function getLangFirstParam(origin, country, language) {
596-
const isLingoLocale = await getIsLingoLocale(origin, country, language);
597+
const fqdn = window.location.hostname;
598+
const isLingoLocale = await getIsLingoLocale(origin, country, language, fqdn);
597599
// if it's not a lingo locale, check if you're on a base site.
598600
if (!isLingoLocale && country !== 'xx') {
599601
return false;
600602
}
601603
return true;
602604
}
603605

604-
async function getLingoSiteLocale(origin, path) {
606+
async function getLingoSiteLocale(origin, path, fqdn = 'www.adobe.com') {
605607
const host = origin.toLowerCase();
606608
let lingoSiteMapping = {
607609
country: 'xx',
@@ -627,7 +629,7 @@ async function getLingoSiteLocale(origin, path) {
627629

628630
try {
629631
let siteId;
630-
const response = await fetch('https://www.adobe.com/federal/assets/data/lingo-site-mapping.json');
632+
const response = await fetch(`https://www.adobe.com/federal/assets/data/lingo-site-mapping.json?${fqdn}`);
631633
if (!response.ok) throw new Error(`HTTP ${response.status}`);
632634
const configJson = await response.json();
633635

@@ -681,7 +683,7 @@ async function getLingoSiteLocale(origin, path) {
681683
return lingoSiteMapping;
682684
}
683685

684-
export const getLanguageFirstCountryAndLang = async (path, origin) => {
686+
export const getLanguageFirstCountryAndLang = async (path, origin, fqdn) => {
685687
const localeArr = path.split('/');
686688
let langStr = 'en';
687689
let countryStr = 'xx';
@@ -692,7 +694,7 @@ export const getLanguageFirstCountryAndLang = async (path, origin) => {
692694
countryStr = countryStr.ietf?.split('-')[1] ?? 'xx';
693695
}
694696
} else {
695-
const mapping = await getLingoSiteLocale(origin, path);
697+
const mapping = await getLingoSiteLocale(origin, path, fqdn);
696698
countryStr = LOCALES[mapping.country.toLowerCase()] ?? 'xx';
697699
if (typeof countryStr === 'object') {
698700
countryStr = countryStr.ietf?.split('-')[1] ?? 'xx';
@@ -727,7 +729,8 @@ export async function getCountryAndLang({ autoCountryLang, country, language, so
727729
if (!isNewsSource) {
728730
const primeSource = Array.from([source].flat())[0];
729731
const pathname = pageConfigHelper()?.pathname || window.location.pathname;
730-
const mapping = await getLanguageFirstCountryAndLang(pathname, primeSource);
732+
const fqdn = window.location.hostname;
733+
const mapping = await getLanguageFirstCountryAndLang(pathname, primeSource, fqdn);
731734

732735
countryStr = mapping.country || fallbackCountry;
733736
langStr = mapping.lang || fallbackLang;
@@ -743,7 +746,7 @@ export async function getCountryAndLang({ autoCountryLang, country, language, so
743746
}
744747

745748
if (geoCountry) {
746-
const isLingoLocale = await getIsLingoLocale(primeSource, geoCountry, langStr);
749+
const isLingoLocale = await getIsLingoLocale(primeSource, geoCountry, langStr, fqdn);
747750
if (isLingoLocale) countryStr = geoCountry.toLowerCase();
748751
}
749752
} catch (error) {

test/blocks/caas/utils.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,8 @@ describe('getCountryAndLang', () => {
826826
ogFetch = window.fetch;
827827
window.fetch = stub().callsFake((url) => {
828828
const urlStr = typeof url === 'string' ? url : (url?.url ?? url?.href ?? '');
829-
return urlStr === LINGO_MAPPING_URL ? lingoMappingResponse() : ogFetch(url);
829+
const isLingoMapping = urlStr === LINGO_MAPPING_URL || urlStr.includes('lingo-site-mapping.json');
830+
return isLingoMapping ? lingoMappingResponse() : ogFetch(url);
830831
});
831832
});
832833

tools/send-to-caas/send-utils.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ const getBulkPublishLangAttr = async (options) => {
304304
const { country, lang } = await getLanguageFirstCountryAndLang(
305305
options.prodUrl,
306306
options.repo,
307+
options.host,
307308
);
308309
return `${lang}-${country}`;
309310
}
@@ -323,6 +324,7 @@ const getCountryAndLang = async (options, origin) => {
323324
return getLanguageFirstCountryAndLang(
324325
window.location.pathname,
325326
origin,
327+
window.location.hostname,
326328
);
327329
}
328330
/* c8 ignore next */

0 commit comments

Comments
 (0)