@@ -10,68 +10,68 @@ type DecoratorElements = {
10
10
DECORATOR_FOOTER : string ;
11
11
} ;
12
12
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 ) {
19
18
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
+ } ,
73
30
} ) ;
74
31
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
+
75
75
export const fetchDecoratorHtml = async ( props : DecoratorFetchProps ) : Promise < DecoratorElements > => {
76
76
const url = getDecoratorUrl ( props ) ;
77
77
0 commit comments