@@ -3,9 +3,16 @@ declare global {
3
3
__RENDER_6_RUNTIME__ : any
4
4
__RENDER_7_RUNTIME__ : any
5
5
__RUNTIME__ : any
6
- $ : any
6
+ $ : typeof import ( 'jquery' )
7
7
RenderExtensionLoader : any
8
8
}
9
+
10
+ namespace JQuery {
11
+ // eslint-disable-next-line @typescript-eslint/naming-convention
12
+ interface jqXHR < TResolve > {
13
+ retry : ( opts : { timeout ?: number ; times ?: number } ) => TResolve
14
+ }
15
+ }
9
16
}
10
17
11
18
class RenderExtensionLoader {
@@ -43,26 +50,27 @@ class RenderExtensionLoader {
43
50
? 'myvtexdev.com'
44
51
: 'myvtex.com' )
45
52
this . get = window . $
46
- ? url =>
53
+ ? ( url : string ) =>
47
54
window . $ . ajax ( { url, timeout : this . timeout } ) . retry ( {
48
55
timeout : 2000 ,
49
56
times : 2 ,
50
57
} )
51
58
: window . fetch
52
- ? url =>
59
+ ? ( url : string ) =>
53
60
new Promise ( ( resolve , reject ) => {
54
61
const fetchTimeout = setTimeout ( ( ) => {
55
62
reject ( { error : 'timeout' } )
56
63
} , this . timeout )
57
64
58
65
window
59
66
. fetch ( url )
60
- . then ( res => {
67
+ . then ( ( res ) => {
61
68
clearTimeout ( fetchTimeout )
69
+
62
70
return res
63
71
} )
64
- . then ( res => res . json ( ) )
65
- . then ( res => resolve ( res ) )
72
+ . then ( ( res ) => res . json ( ) )
73
+ . then ( ( res ) => resolve ( res ) )
66
74
} )
67
75
: null
68
76
@@ -106,7 +114,7 @@ class RenderExtensionLoader {
106
114
return this . runtime
107
115
}
108
116
109
- public update = runtimeOrUpdateFn => {
117
+ public update = ( runtimeOrUpdateFn ) => {
110
118
if ( typeof runtimeOrUpdateFn === 'function' ) {
111
119
this . runtime = runtimeOrUpdateFn ( this . runtime )
112
120
} else {
@@ -129,6 +137,7 @@ class RenderExtensionLoader {
129
137
130
138
this . time ( 'render-extension-loader:render' )
131
139
const runtime = window [ `__RENDER_${ this . renderMajor } _RUNTIME__` ]
140
+
132
141
runtime . render ( extension , this . runtime , element )
133
142
this . timeEnd ( 'render-extension-loader:render' )
134
143
@@ -138,17 +147,22 @@ class RenderExtensionLoader {
138
147
private loadExtensionPointsContext = async ( ) => {
139
148
this . time ( 'render-extension-loader:json' )
140
149
const { runtime, styles, scripts } = await this . get (
141
- `https://${ this . workspace } --${ this . account } .${ this . publicEndpoint } /legacy-extensions${ this . path } ?__disableSSR&locale=${ this . locale } &v=3`
150
+ `https://${ this . workspace } --${ this . account } .${ this . publicEndpoint } /legacy-extensions${ this . path } ?__disableSSR&locale=${ this . locale } &v=3&origin= ${ window . location . hostname } `
142
151
)
152
+
143
153
this . timeEnd ( 'render-extension-loader:json' )
144
154
145
155
for ( const key in window . __RUNTIME__ || { } ) {
146
- if ( window . __RUNTIME__ . hasOwnProperty ( key ) && runtime [ key ] === undefined ) {
156
+ if (
157
+ Object . prototype . hasOwnProperty . call ( window . __RUNTIME__ , key ) &&
158
+ runtime [ key ] === undefined
159
+ ) {
147
160
runtime [ key ] = window . __RUNTIME__ [ key ]
148
161
}
149
162
}
150
163
151
164
this . setGlobalContext ( { runtime, styles, scripts } )
165
+
152
166
return { runtime, styles, scripts }
153
167
}
154
168
@@ -164,23 +178,26 @@ class RenderExtensionLoader {
164
178
165
179
private getExistingScriptSrcs = ( ) => {
166
180
const paths = [ ]
181
+
167
182
for ( let i = 0 ; i < document . scripts . length ; i ++ ) {
168
183
paths . push ( document . scripts . item ( i ) . src )
169
184
}
185
+
170
186
return paths
171
187
}
172
188
173
- private scriptOnPage = path => {
174
- return this . getExistingScriptSrcs ( ) . some ( src => src . indexOf ( path ) !== - 1 )
189
+ private scriptOnPage = ( path ) => {
190
+ return this . getExistingScriptSrcs ( ) . some ( ( src ) => src . indexOf ( path ) !== - 1 )
175
191
}
176
192
177
- private addScriptToPage = src => {
193
+ private addScriptToPage = ( src ) => {
178
194
return new Promise ( ( resolve , reject ) => {
179
195
if ( this . scriptOnPage ( src ) ) {
180
196
return resolve ( )
181
197
}
182
198
183
199
const script = document . createElement ( 'script' )
200
+
184
201
script . crossOrigin = 'anonymous'
185
202
script . onload = ( ) => resolve ( )
186
203
script . onerror = ( ) => reject ( )
@@ -190,21 +207,22 @@ class RenderExtensionLoader {
190
207
} )
191
208
}
192
209
193
- private addStyleToPage = href => {
210
+ private addStyleToPage = ( href ) => {
194
211
const link = document . createElement ( 'link' )
212
+
195
213
link . href = href
196
214
link . type = 'text/css'
197
215
link . rel = 'stylesheet'
198
216
document . head . appendChild ( link )
199
217
}
200
218
201
- private time = label => {
219
+ private time = ( label ) => {
202
220
if ( this . verbose ) {
203
221
console . time ( label )
204
222
}
205
223
}
206
224
207
- private timeEnd = label => {
225
+ private timeEnd = ( label ) => {
208
226
if ( this . verbose ) {
209
227
console . timeEnd ( label )
210
228
}
0 commit comments