-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrewriteHeaders.js
30 lines (27 loc) · 928 Bytes
/
rewriteHeaders.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
const { readFileSync, writeFileSync } = require('fs');
/* Default index.html output path */
const filePath = './build/index.html';
const headersPath = './build/_headers';
const rawHtml = readFileSync(filePath).toString();
let rawHeaders = readFileSync(headersPath).toString();
/* Extract the CSP from file */
const csp = /<meta http-equiv="Content-Security-Policy" content="([^>]*)">/.exec(rawHtml)[1]
/* Redact the CSP meta tag */
writeFileSync(filePath, rawHtml.replace(
/<meta http-equiv="Content-Security-Policy" content="[^>]*">/g,
'',
));
const scriptSourceMatches = rawHtml.matchAll(/<script src="([^"]*)" [^>]*><\/script>/g);
const preloadLinks = Array.from(scriptSourceMatches).map(([_, path]) => (
`Link: <${path}; rel=preload; as=script>`
));
writeFileSync(headersPath, rawHeaders
.replace(
'%CSP_PLACEHOLDER%',
csp,
)
.replace(
'Link: <placeholder>',
preloadLinks.join('\n '),
)
);