Skip to content

Commit

Permalink
MWPW-143202 firefox gnav not appearing bug (#1969)
Browse files Browse the repository at this point in the history
* try to catch error

* test without breaking things

* catch akamai failure

* add try catch

* attempt 2 to catch jsonp failure

* cleanup

* fix geo2 source

* test using proper fetch

* MWPW-143202 remove old jsonp request

* MWPW-143202 catch case if resp is not ok

* MWPW-143202 prevent caching of geo2 request

---------

Co-authored-by: Blaine Gunn <Blainegunn@gmail.com>
  • Loading branch information
vhargrave and Blainegunn authored Mar 18, 2024
1 parent 944ae19 commit f5a436c
Showing 1 changed file with 26 additions and 32 deletions.
58 changes: 26 additions & 32 deletions libs/features/georoutingv2/georoutingv2.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,35 +35,25 @@ export const getCookie = (name) => document.cookie
.find((row) => row.startsWith(`${name}=`))
?.split('=')[1];

/* c8 ignore next 16 */
const geo2jsonp = (callback) => {
// Setup a unique name that can be called & destroyed
const callbackName = `jsonp_${Math.round(100000 * Math.random())}`;

const script = document.createElement('script');
script.src = `https://geo2.adobe.com/json/?callback=${callbackName}`;

// Define the function that the script will call
window[callbackName] = (data) => {
delete window[callbackName];
document.body.removeChild(script);
callback(data);
};

document.body.appendChild(script);
};

const getAkamaiCode = () => new Promise((resolve) => {
const getAkamaiCode = () => new Promise((resolve, reject) => {
const urlParams = new URLSearchParams(window.location.search);
const akamaiLocale = urlParams.get('akamaiLocale') || sessionStorage.getItem('akamai');
if (akamaiLocale !== null) {
resolve(akamaiLocale.toLowerCase());
} else {
/* c8 ignore next 5 */
geo2jsonp((data) => {
const code = data.country.toLowerCase();
sessionStorage.setItem('akamai', code);
resolve(code);
fetch('https://geo2.adobe.com/json/', { cache: 'no-cache' }).then((resp) => {
if (resp.ok) {
resp.json().then((data) => {
const code = data.country.toLowerCase();
sessionStorage.setItem('akamai', code);
resolve(code);
});
} else {
reject(new Error(`Something went wrong getting the akamai Code. Response status text: ${resp.statusText}`));
}
}).catch((error) => {
reject(new Error(`Something went wrong getting the akamai Code. ${error.message}`));
});
}
});
Expand Down Expand Up @@ -329,15 +319,19 @@ export default async function loadGeoRouting(
}

// Show modal when derived countries from url locale and akamai disagree
const akamaiCode = await getAkamaiCode();
if (akamaiCode && !getCodes(urlGeoData).includes(akamaiCode)) {
const localeMatches = getMatches(json.georouting.data, akamaiCode);
const details = await getDetails(urlGeoData, localeMatches, json.geos.data);
if (details) {
await showModal(details);
sendAnalyticsFunc(
new Event(`Load:${urlLocale || 'us'}-${akamaiCode || 'us'}|Geo_Routing_Modal`),
);
try {
const akamaiCode = await getAkamaiCode();
if (akamaiCode && !getCodes(urlGeoData).includes(akamaiCode)) {
const localeMatches = getMatches(json.georouting.data, akamaiCode);
const details = await getDetails(urlGeoData, localeMatches, json.geos.data);
if (details) {
await showModal(details);
sendAnalyticsFunc(
new Event(`Load:${urlLocale || 'us'}-${akamaiCode || 'us'}|Geo_Routing_Modal`),
);
}
}
} catch (e) {
window.lana?.log(e.message);

Check warning on line 335 in libs/features/georoutingv2/georoutingv2.js

View check run for this annotation

Codecov / codecov/patch

libs/features/georoutingv2/georoutingv2.js#L335

Added line #L335 was not covered by tests
}
}

0 comments on commit f5a436c

Please sign in to comment.