Skip to content

Commit

Permalink
Move federal code to utils (#3591)
Browse files Browse the repository at this point in the history
* Move federal base code to utils

* Migrate federated.js

* Fix linting errors
  • Loading branch information
mokimo authored Feb 21, 2025
1 parent 67fbb44 commit 60ccc33
Show file tree
Hide file tree
Showing 17 changed files with 99 additions and 84 deletions.
2 changes: 1 addition & 1 deletion libs/blocks/fragment/fragment.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export default async function init(a) {

let resourcePath = a.href;
if (a.href.includes('/federal/')) {
const { getFederatedUrl } = await import('../../utils/federated.js');
const { getFederatedUrl } = await import('../../utils/utils.js');
resourcePath = getFederatedUrl(a.href);
}
const resp = await customFetch({ resource: `${resourcePath}.plain.html`, withCacheRules: true })
Expand Down
4 changes: 2 additions & 2 deletions libs/blocks/global-footer/global-footer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
getConfig,
localizeLink,
loadStyle,
getFederatedUrl,
getFedsPlaceholderConfig,
} from '../../utils/utils.js';

import {
Expand All @@ -22,8 +24,6 @@ import {
isDarkMode,
} from '../global-navigation/utilities/utilities.js';

import { getFederatedUrl, getFedsPlaceholderConfig } from '../../utils/federated.js';

import { replaceKey } from '../../features/placeholders.js';

const { miloLibs, codeRoot, locale, mep } = getConfig();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { getMetadata, getConfig } from '../../../../utils/utils.js';
import { getMetadata, getConfig, getFederatedUrl } from '../../../../utils/utils.js';
import { toFragment, lanaLog } from '../../utilities/utilities.js';
import { getFederatedUrl } from '../../../../utils/federated.js';

const metadata = {
seo: 'breadcrumbs-seo',
Expand Down
3 changes: 1 addition & 2 deletions libs/blocks/global-navigation/features/profile/dropdown.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { getConfig } from '../../../../utils/utils.js';
import { getConfig, getFedsPlaceholderConfig } from '../../../../utils/utils.js';
import { toFragment, trigger, closeAllDropdowns, logErrorFor } from '../../utilities/utilities.js';
import { replaceKeyArray } from '../../../../features/placeholders.js';
import { getFedsPlaceholderConfig } from '../../../../utils/federated.js';

const getLanguage = (ietfLocale) => {
if (!ietfLocale.length) return 'en';
Expand Down
3 changes: 1 addition & 2 deletions libs/blocks/global-navigation/features/search/gnav-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import {
lanaLog,
} from '../../utilities/utilities.js';
import { replaceKeyArray } from '../../../../features/placeholders.js';
import { getConfig } from '../../../../utils/utils.js';
import { getConfig, getFedsPlaceholderConfig } from '../../../../utils/utils.js';
import { debounce } from '../../../../utils/action.js';
import { getFedsPlaceholderConfig } from '../../../../utils/federated.js';

const CONFIG = {
suggestions: {
Expand Down
2 changes: 1 addition & 1 deletion libs/blocks/global-navigation/global-navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
decorateLinks,
loadScript,
getGnavSource,
getFedsPlaceholderConfig,
} from '../../utils/utils.js';
import {
closeAllDropdowns,
Expand Down Expand Up @@ -45,7 +46,6 @@ import {
branchBannerLoadCheck,
getBranchBannerInfo,
} from './utilities/utilities.js';
import { getFedsPlaceholderConfig } from '../../utils/federated.js';

import { replaceKey, replaceKeyArray } from '../../features/placeholders.js';

Expand Down
11 changes: 9 additions & 2 deletions libs/blocks/global-navigation/utilities/utilities.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
/* eslint import/no-relative-packages: 0 */
import {
getConfig, getMetadata, loadStyle, loadLana, decorateLinks, localizeLink,
getConfig,
getMetadata,
loadStyle,
loadLana,
decorateLinks,
localizeLink,
getFederatedContentRoot,
getFederatedUrl,
getFedsPlaceholderConfig,
} from '../../../utils/utils.js';
import { getFederatedContentRoot, getFederatedUrl, getFedsPlaceholderConfig } from '../../../utils/federated.js';
import { processTrackingLabels } from '../../../martech/attributes.js';
import { replaceText } from '../../../features/placeholders.js';
import { PERSONALIZATION_TAGS } from '../../../features/personalization/personalization.js';
Expand Down
2 changes: 1 addition & 1 deletion libs/blocks/modal/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function getCustomModal(custom, dialog) {
async function getPathModal(path, dialog) {
let href = path;
if (path.includes('/federal/')) {
const { getFederatedUrl } = await import('../../utils/federated.js');
const { getFederatedUrl } = await import('../../utils/utils.js');
href = getFederatedUrl(path);
}
const block = createTag('a', { href });
Expand Down
2 changes: 1 addition & 1 deletion libs/features/georoutingv2/georoutingv2.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getFederatedContentRoot } from '../../utils/federated.js';
import { getFederatedContentRoot } from '../../utils/utils.js';

let config;
let createTag;
Expand Down
10 changes: 8 additions & 2 deletions libs/features/personalization/personalization.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@
/* eslint-disable no-underscore-dangle */
/* eslint-disable no-console */

import { createTag, getConfig, loadLink, loadScript, localizeLink } from '../../utils/utils.js';
import { getFederatedUrl } from '../../utils/federated.js';
import {
createTag,
getConfig,
loadLink,
loadScript,
localizeLink,
getFederatedUrl,
} from '../../utils/utils.js';

/* c8 ignore start */
const PHONE_SIZE = window.screen.width < 550 || window.screen.height < 550;
Expand Down
3 changes: 1 addition & 2 deletions libs/features/webapp-prompt/webapp-prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import {
lanaLog,
toFragment,
} from '../../blocks/global-navigation/utilities/utilities.js';
import { getConfig, decorateSVG } from '../../utils/utils.js';
import { getConfig, decorateSVG, getFedsPlaceholderConfig } from '../../utils/utils.js';
import { replaceKey, replaceText } from '../placeholders.js';
import { getFedsPlaceholderConfig } from '../../utils/federated.js';

export const DISMISSAL_CONFIG = {
animationCount: 2,
Expand Down
10 changes: 8 additions & 2 deletions libs/utils/decorate.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { createTag, loadStyle, getConfig, createIntersectionObserver } from './utils.js';
import { getFederatedContentRoot, getFedsPlaceholderConfig } from './federated.js';
import {
createTag,
loadStyle,
getConfig,
createIntersectionObserver,
getFederatedContentRoot,
getFedsPlaceholderConfig,
} from './utils.js';

const { miloLibs, codeRoot } = getConfig();
const HIDE_CONTROLS = '_hide-controls';
Expand Down
62 changes: 3 additions & 59 deletions libs/utils/federated.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,3 @@
import { getConfig, SLD } from './utils.js';

let federatedContentRoot;
/* eslint-disable import/prefer-default-export */
export const getFederatedContentRoot = () => {
const cdnWhitelistedOrigins = [
'https://www.adobe.com',
'https://business.adobe.com',
'https://blog.adobe.com',
'https://milo.adobe.com',
'https://news.adobe.com',
];
const { allowedOrigins = [], origin: configOrigin } = getConfig();
if (federatedContentRoot) return federatedContentRoot;
// Non milo consumers will have its origin from config
const origin = configOrigin || window.location.origin;

federatedContentRoot = [...allowedOrigins, ...cdnWhitelistedOrigins].some((o) => origin.replace('.stage', '') === o)
? origin
: 'https://www.adobe.com';

if (origin.includes('localhost') || origin.includes(`.${SLD}.`)) {
federatedContentRoot = `https://main--federal--adobecom.aem.${origin.endsWith('.live') ? 'live' : 'page'}`;
}

return federatedContentRoot;
};

// TODO we should match the akamai patterns /locale/federal/ at the start of the url
// and make the check more strict.
export const getFederatedUrl = (url = '') => {
if (typeof url !== 'string' || !url.includes('/federal/')) return url;
if (url.startsWith('/')) return `${getFederatedContentRoot()}${url}`;
try {
const { pathname, search, hash } = new URL(url);
return `${getFederatedContentRoot()}${pathname}${search}${hash}`;
} catch (e) {
window.lana?.log(`getFederatedUrl errored parsing the URL: ${url}: ${e.toString()}`);
}
return url;
};

let fedsPlaceholderConfig;
export const getFedsPlaceholderConfig = ({ useCache = true } = {}) => {
if (useCache && fedsPlaceholderConfig) return fedsPlaceholderConfig;

const { locale, placeholders } = getConfig();
const libOrigin = getFederatedContentRoot();

fedsPlaceholderConfig = {
locale: {
...locale,
contentRoot: `${libOrigin}${locale.prefix}/federal/globalnav`,
},
placeholders,
};

return fedsPlaceholderConfig;
};
import { getFederatedContentRoot, getFederatedUrl, getFedsPlaceholderConfig } from './utils.js';
// DO NOT USE THIS FILE, THIS IS HERE FOR HISTORY PURPOSES AND AVOIDING BREAKING CHANGES.
export { getFederatedContentRoot, getFederatedUrl, getFedsPlaceholderConfig };
58 changes: 58 additions & 0 deletions libs/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,64 @@ export const [setConfig, updateConfig, getConfig] = (() => {
];
})();

let federatedContentRoot;
/* eslint-disable import/prefer-default-export */
export const getFederatedContentRoot = () => {
const cdnWhitelistedOrigins = [
'https://www.adobe.com',
'https://business.adobe.com',
'https://blog.adobe.com',
'https://milo.adobe.com',
'https://news.adobe.com',
];
const { allowedOrigins = [], origin: configOrigin } = getConfig();
if (federatedContentRoot) return federatedContentRoot;
// Non milo consumers will have its origin from config
const origin = configOrigin || window.location.origin;

federatedContentRoot = [...allowedOrigins, ...cdnWhitelistedOrigins].some((o) => origin.replace('.stage', '') === o)
? origin
: 'https://www.adobe.com';

if (origin.includes('localhost') || origin.includes(`.${SLD}.`)) {
federatedContentRoot = `https://main--federal--adobecom.aem.${origin.endsWith('.live') ? 'live' : 'page'}`;
}

return federatedContentRoot;
};

// TODO we should match the akamai patterns /locale/federal/ at the start of the url
// and make the check more strict.
export const getFederatedUrl = (url = '') => {
if (typeof url !== 'string' || !url.includes('/federal/')) return url;
if (url.startsWith('/')) return `${getFederatedContentRoot()}${url}`;
try {
const { pathname, search, hash } = new URL(url);
return `${getFederatedContentRoot()}${pathname}${search}${hash}`;
} catch (e) {
window.lana?.log(`getFederatedUrl errored parsing the URL: ${url}: ${e.toString()}`);
}
return url;
};

let fedsPlaceholderConfig;
export const getFedsPlaceholderConfig = ({ useCache = true } = {}) => {
if (useCache && fedsPlaceholderConfig) return fedsPlaceholderConfig;

const { locale, placeholders } = getConfig();
const libOrigin = getFederatedContentRoot();

fedsPlaceholderConfig = {
locale: {
...locale,
contentRoot: `${libOrigin}${locale.prefix}/federal/globalnav`,
},
placeholders,
};

return fedsPlaceholderConfig;
};

export function isInTextNode(node) {
return node.parentElement.firstChild.nodeType === Node.TEXT_NODE;
}
Expand Down
3 changes: 1 addition & 2 deletions test/blocks/global-navigation/utilities/utilities.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ import {
dropWhile,
getBranchBannerInfo,
} from '../../../../libs/blocks/global-navigation/utilities/utilities.js';
import { setConfig, getConfig } from '../../../../libs/utils/utils.js';
import { setConfig, getConfig, getFedsPlaceholderConfig } from '../../../../libs/utils/utils.js';
import { createFullGlobalNavigation, config, mockRes } from '../test-utilities.js';
import gnavWithlocalNav from '../mocks/gnav-with-localnav.plain.js';
import mepInBlock from '../mocks/mep-config.js';
import { getFedsPlaceholderConfig } from '../../../../libs/utils/federated.js';

const baseHost = 'https://main--federal--adobecom.aem.page';
describe('global navigation utilities', () => {
Expand Down
3 changes: 1 addition & 2 deletions test/features/georoutingv2/georoutingv2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { setViewport } from '@web/test-runner-commands';

const { default: init, getCookie } = await import('../../../libs/features/georoutingv2/georoutingv2.js');
let { getMetadata } = await import('../../../libs/utils/utils.js');
const { getFederatedContentRoot } = await import('../../../libs/utils/federated.js');
const { createTag, loadStyle, loadBlock, setConfig } = await import('../../../libs/utils/utils.js');
const { createTag, loadStyle, loadBlock, setConfig, getFederatedContentRoot } = await import('../../../libs/utils/utils.js');

const mockConfig = {
locales: {
Expand Down
2 changes: 1 addition & 1 deletion test/utils/federated.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from '@esm-bundle/chai';
import { getFederatedUrl, getFederatedContentRoot } from '../../libs/utils/federated.js';
import { getFederatedUrl, getFederatedContentRoot } from '../../libs/utils/utils.js';

const baseHost = 'https://main--federal--adobecom.aem.page';

Expand Down

0 comments on commit 60ccc33

Please sign in to comment.