-
Notifications
You must be signed in to change notification settings - Fork 18
/
preconnect-html-webpack-plugin.js
72 lines (63 loc) · 2.67 KB
/
preconnect-html-webpack-plugin.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
/**
* Adds an extra option to the default HtmlWebpackPlugin configuration to make
* the list of preconnect domains available while generating the index.html file.
* That is, the HTML document will iterate over each preconnect domain and inject
* `<link rel="preconnect">` tags into the generated HTML to preconnect to the
* services used by the application to reduce the latency the requests.
*
* @param {Object} htmlWebpackPlugin
*
* @returns Modified instance of HtmlWebpackPlugin with preconnect domains added to
* the options such that the precconnect domains are available to the generated HTML.
*/
function addPreconnectDomainsToHtmlWebpackPlugin(htmlWebpackPlugin) {
const preconnectDomains = [
// Base services
process.env.LMS_BASE_URL,
process.env.ECOMMERCE_BASE_URL,
process.env.DISCOVERY_API_BASE_URL,
// Enterprise services
process.env.ENTERPRISE_CATALOG_API_BASE_URL,
process.env.ENTERPRISE_ACCESS_BASE_URL,
process.env.ENTERPRISE_SUBSIDY_BASE_URL,
process.env.LICENSE_MANAGER_URL,
// Miscellaneous services
'https://fonts.googleapis.com',
'https://fonts.gstatic.com',
];
// Add Segment origins, if enabled.
if (process.env.SEGMENT_KEY) {
preconnectDomains.push('https://cdn.segment.com');
preconnectDomains.push('https://api.segment.io');
}
// Add New Relic origins, if enabled.
if (process.env.ENABLE_NEW_RELIC !== 'false') {
preconnectDomains.push('https://js-agent.newrelic.com');
preconnectDomains.push('https://bam.nr-data.net');
}
// Add Optimizely origin, if enabled.
if (process.env.OPTIMIZELY_PROJECT_ID) {
// in production, optimizely script is loaded from marketing site
// domain, and subsequent requests are made to logx.optimizely.com.
preconnectDomains.push(process.env.MARKETING_SITE_BASE_URL);
preconnectDomains.push('https://logx.optimizely.com');
}
// Add HotJar origins, if enabled.
if (process.env.HOTJAR_APP_ID) {
preconnectDomains.push('https://static.hotjar.com');
preconnectDomains.push('https://script.hotjar.com');
}
// Add cookie law origin, if enabled.
if (process.env.COOKIE_LAW_CDN_URL) {
preconnectDomains.push(process.env.COOKIE_LAW_CDN_URL);
}
// Add Algolia origin, if ALGOLIA_APP_ID is present.
if (process.env.ALGOLIA_APP_ID) {
preconnectDomains.push(`https://${process.env.ALGOLIA_APP_ID}-dsn.algolia.net`);
}
// Add preconnect domains to the HtmlWebpackPlugin configuration.
htmlWebpackPlugin.userOptions.preconnect = preconnectDomains; // eslint-disable-line no-param-reassign
// Return modified HtmlWebpackPlugin configuration.
return htmlWebpackPlugin;
}
module.exports = addPreconnectDomainsToHtmlWebpackPlugin;