1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-25 00:07:47 +01:00

refactor: fix CDN prefix rewrite (#1585)

* fix: change folder path

* refactor: revert cdnPrefix test config

* refactor: fix rewriteHTML test data

* refactor: add rewriteHTML cdnPrefix test

Co-authored-by: Fredrik Oseberg <fredrik.no@gmail.com>
This commit is contained in:
olav 2022-05-10 14:05:53 +02:00 committed by GitHub
parent 943af1e804
commit 3b46bfb83a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 11 deletions

View File

@ -1,43 +1,84 @@
import { rewriteHTML } from './rewriteHTML';
const input = `<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="::faviconPrefix::/favicon.ico"/><meta http-equiv="X-UA-Compatible" content="IE=edge"/><meta name="baseUriPath" content="::baseUriPath::"/><meta name="cdnPrefix" content="::cdnPrefix::"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="description" content="unleash"/><title>Unleash</title><link rel="preconnect" href="https://fonts.googleapis.com"/><link rel="preconnect" href="https://fonts.gstatic.com" crossorigin/><link href="https://fonts.googleapis.com/css2?family=Sen:wght@400;700;800&display=swap" rel="stylesheet"/><script defer="defer" src="/static/js/main.84c7ff99.js"></script><link href="/static/css/main.9558c71e.css" rel="stylesheet"></head><body><div id="app"></div></body></html>`;
const input = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="::faviconPrefix::/favicon.ico" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="baseUriPath" content="::baseUriPath::" />
<meta name="cdnPrefix" content="::cdnPrefix::" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="unleash" />
<title>Unleash</title>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Sen:wght@400;700;800&display=swap"
rel="stylesheet"
/>
<script type="module" crossorigin src="/assets/index.556ac563.js"></script>
<link rel="stylesheet" href="/assets/index.4b6b260a.css">
</head>
<body>
<div id="app"></div>
</body>
</html>
`;
test('rewriteHTML substitutes meta tag with existing rewrite value', () => {
const result = rewriteHTML(input, '/hosted');
expect(
result.includes('<meta name="baseUriPath" content="/hosted"/>'),
result.includes('<meta name="baseUriPath" content="/hosted" />'),
).toBe(true);
});
test('rewriteHTML substitutes meta tag with empty value', () => {
const result = rewriteHTML(input, '');
expect(result.includes('<meta name="baseUriPath" content=""/>')).toBe(true);
expect(result.includes('<meta name="baseUriPath" content="" />')).toBe(
true,
);
});
test('rewriteHTML substitutes asset paths correctly with baseUriPath', () => {
const result = rewriteHTML(input, '/hosted');
expect(result.includes('<script defer="defer" src="/hosted/')).toBe(true);
expect(
result.includes(
'<script type="module" crossorigin src="/hosted/assets/index',
),
).toBe(true);
});
test('rewriteHTML substitutes asset paths correctly without baseUriPath', () => {
const result = rewriteHTML(input, '');
expect(result.includes('<script defer="defer" src="/static/js/')).toBe(
true,
);
expect(
result.includes('<script type="module" crossorigin src="/assets/index'),
).toBe(true);
});
test('rewriteHTML substitutes asset paths correctly with cdnPrefix', () => {
const result = rewriteHTML(input, '', 'https://cdn.getunleash.io/v4.1.0');
expect(
result.includes(
'<script type="module" crossorigin src="https://cdn.getunleash.io/v4.1.0/assets/index',
),
).toBe(true);
});
test('rewriteHTML swaps out faviconPath if cdnPrefix is set', () => {
const result = rewriteHTML(input, '', 'https://cdn.getunleash.io/v4.1.0');
expect(
result.includes(
'<link rel="icon" href="https://cdn.getunleash.io/favicon.ico"/>',
'<link rel="icon" href="https://cdn.getunleash.io/favicon.ico" />',
),
).toBe(true);
});
test('rewriteHTML sets favicon path to root', () => {
const result = rewriteHTML(input, '');
expect(result.includes('<link rel="icon" href="/favicon.ico"/>')).toBe(
expect(result.includes('<link rel="icon" href="/favicon.ico" />')).toBe(
true,
);
});

View File

@ -11,8 +11,8 @@ export const rewriteHTML = (
result = result.replace(/::faviconPrefix::/gi, faviconPrefix);
result = result.replace(
/\/static/gi,
`${cdnPrefix || rewriteValue}/static`,
/\/assets/gi,
`${cdnPrefix || rewriteValue}/assets`,
);
return result;