generated from adobe/aem-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 175
/
Copy pathnavigation.js
85 lines (80 loc) · 2.41 KB
/
navigation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const blockConfig = [
{
key: 'header',
name: 'global-navigation',
targetEl: 'header',
appendType: 'prepend',
params: ['imsClientId', 'searchEnabled', 'unavHelpChildren'],
},
{
key: 'footer',
name: 'global-footer',
targetEl: 'footer',
appendType: 'appendChild',
params: ['privacyId', 'privacyLoadDelay'],
},
];
const envMap = {
prod: 'https://www.adobe.com',
stage: 'https://www.stage.adobe.com',
qa: 'https://gnav--milo--adobecom.hlx.page',
};
function getParamsConfigs(configs) {
return blockConfig.reduce((acc, block) => {
block.params.forEach((param) => {
const value = configs[block.key]?.[param];
if (value !== undefined) {
acc[param] = value;
}
});
return acc;
}, {});
}
export default async function loadBlock(configs, customLib) {
const {
header,
footer,
authoringPath,
env = 'prod',
locale = '',
theme,
} = configs || {};
const branch = new URLSearchParams(window.location.search).get('navbranch');
const miloLibs = branch ? `https://${branch}--milo--adobecom.hlx.page` : customLib || envMap[env];
if (!header && !footer) {
console.error('Global navigation Error: header and footer configurations are missing.');
return;
}
// Relative path can't be used, as the script will run on consumer's app
const [{ default: bootstrapBlock }, { default: locales }, { setConfig }] = await Promise.all([
import(`${miloLibs}/libs/navigation/bootstrapper.js`),
import(`${miloLibs}/libs/utils/locales.js`),
import(`${miloLibs}/libs/utils/utils.js`),
]);
const paramConfigs = getParamsConfigs(configs, miloLibs);
const clientConfig = {
origin: `https://main--federal--adobecom.hlx.${env === 'prod' ? 'live' : 'page'}`,
miloLibs: `${miloLibs}/libs`,
pathname: `/${locale}`,
locales: configs.locales || locales,
contentRoot: authoringPath || footer.authoringPath,
theme,
...paramConfigs,
};
setConfig(clientConfig);
for await (const block of blockConfig) {
const configBlock = configs[block.key];
try {
if (configBlock) {
await bootstrapBlock(`${miloLibs}/libs`, {
...block,
...(block.key === 'header' && { unavComponents: configBlock.unavComponents, redirect: configBlock.redirect }),
});
configBlock.onReady?.();
}
} catch (e) {
configBlock.onError?.(e);
}
}
}
window.loadNavigation = loadBlock;