-
-
Notifications
You must be signed in to change notification settings - Fork 768
/
Copy pathload-index-html.ts
34 lines (31 loc) · 947 Bytes
/
load-index-html.ts
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
import fs from 'fs';
import type { IUnleashConfig } from '../server-impl';
import { rewriteHTML } from './rewriteHTML';
import path from 'path';
import fetch from 'make-fetch-happen';
export async function loadIndexHTML(
config: IUnleashConfig,
publicFolder: string,
): Promise<string> {
const { cdnPrefix, baseUriPath = '' } = config.server;
const uiFlags = encodeURI(JSON.stringify(config.ui.flags || '{}'));
const unleashToken = config.unleashFrontendToken;
let indexHTML: string;
if (cdnPrefix) {
const res = await fetch(`${cdnPrefix}/index.html`);
indexHTML = await res.text();
} else {
indexHTML = fs
.readFileSync(
path.join(config.publicFolder || publicFolder, 'index.html'),
)
.toString();
}
return rewriteHTML(
indexHTML,
baseUriPath,
cdnPrefix,
uiFlags,
unleashToken,
);
}