Skip to content

Commit 159d0da

Browse files
committedAug 30, 2024·
Tidy dekoratøren code and use correct happy-dom close method
1 parent 7c27dab commit 159d0da

File tree

1 file changed

+59
-59
lines changed

1 file changed

+59
-59
lines changed
 

‎server/src/nav-dekoratoren/nav-dekoratoren.ts

+59-59
Original file line numberDiff line numberDiff line change
@@ -10,68 +10,68 @@ type DecoratorElements = {
1010
DECORATOR_FOOTER: string;
1111
};
1212

13-
const fetchDecorator = async (url: string, props: DecoratorFetchProps, retries = 3): Promise<DecoratorElements> =>
14-
fetch(url)
15-
.then((res) => {
16-
if (res.ok) {
17-
return res.text();
18-
}
13+
const fetchDecorator = async (url: string, props: DecoratorFetchProps, retries = 3): Promise<DecoratorElements> => {
14+
try {
15+
const res = await fetch(url);
16+
17+
if (!res.ok) {
1918
throw new Error(`${res.status} - ${res.statusText}`);
20-
})
21-
.then((html) => {
22-
const window = new Window({
23-
settings: {
24-
disableJavaScriptFileLoading: true,
25-
disableCSSFileLoading: true,
26-
disableComputedStyleRendering: true,
27-
disableJavaScriptEvaluation: true,
28-
},
29-
});
30-
const { document } = window;
31-
32-
document.write(html);
33-
34-
const styles = document.getElementById('styles')?.innerHTML;
35-
if (!styles) {
36-
throw new Error('Decorator styles element not found!');
37-
}
38-
39-
const scripts = document.getElementById('scripts')?.innerHTML;
40-
if (!scripts) {
41-
throw new Error('Decorator scripts element not found!');
42-
}
43-
44-
const header = document.getElementById('header-withmenu')?.innerHTML;
45-
if (!header) {
46-
throw new Error('Decorator header element not found!');
47-
}
48-
49-
const footer = document.getElementById('footer-withmenu')?.innerHTML;
50-
if (!footer) {
51-
throw new Error('Decorator footer element not found!');
52-
}
53-
54-
const elements = {
55-
DECORATOR_STYLES: styles.trim(),
56-
DECORATOR_SCRIPTS: scripts.trim(),
57-
DECORATOR_HEADER: header.trim(),
58-
DECORATOR_FOOTER: footer.trim(),
59-
};
60-
61-
document.close();
62-
window.close();
63-
64-
return elements;
65-
})
66-
.catch((e) => {
67-
if (retries > 0) {
68-
console.warn(`Failed to fetch decorator, retrying ${retries} more times - Url: ${url} - Error: ${e}`);
69-
return fetchDecorator(url, props, retries - 1);
70-
}
71-
72-
throw e;
19+
}
20+
21+
const html = await res.text();
22+
23+
const window = new Window({
24+
settings: {
25+
disableJavaScriptFileLoading: true,
26+
disableCSSFileLoading: true,
27+
disableComputedStyleRendering: true,
28+
disableJavaScriptEvaluation: true,
29+
},
7330
});
7431

32+
const { document } = window;
33+
34+
document.write(html);
35+
36+
const styles = document.getElementById('styles')?.innerHTML;
37+
if (!styles) {
38+
throw new Error('Decorator styles element not found!');
39+
}
40+
41+
const scripts = document.getElementById('scripts')?.innerHTML;
42+
if (!scripts) {
43+
throw new Error('Decorator scripts element not found!');
44+
}
45+
46+
const header = document.getElementById('header-withmenu')?.innerHTML;
47+
if (!header) {
48+
throw new Error('Decorator header element not found!');
49+
}
50+
51+
const footer = document.getElementById('footer-withmenu')?.innerHTML;
52+
if (!footer) {
53+
throw new Error('Decorator footer element not found!');
54+
}
55+
56+
window.happyDOM.close();
57+
58+
return {
59+
DECORATOR_STYLES: styles.trim(),
60+
DECORATOR_SCRIPTS: scripts.trim(),
61+
DECORATOR_HEADER: header.trim(),
62+
DECORATOR_FOOTER: footer.trim(),
63+
};
64+
} catch (e) {
65+
if (retries > 0) {
66+
console.warn(`Failed to fetch decorator, retrying ${retries} more times - Url: ${url} - Error: ${e}`);
67+
68+
return fetchDecorator(url, props, retries - 1);
69+
}
70+
71+
throw e;
72+
}
73+
};
74+
7575
export const fetchDecoratorHtml = async (props: DecoratorFetchProps): Promise<DecoratorElements> => {
7676
const url = getDecoratorUrl(props);
7777

0 commit comments

Comments
 (0)
Please sign in to comment.