Skip to content

Commit 8e0cbe1

Browse files
authored
Merge pull request #21 from vtex/fix/cors
CHK-192: Add query string to request in order to avoid CORS issue
2 parents 33b4561 + 31f26fc commit 8e0cbe1

8 files changed

+1135
-153
lines changed

.eslintignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
rollup.config.js
3+
lib

.eslintrc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "vtex",
3+
"env": {
4+
"node": true,
5+
"browser": true
6+
},
7+
"rules": {
8+
"no-console": "off",
9+
"@typescript-eslint/no-namespace": "off"
10+
}
11+
}

.prettierrc

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
1-
{
2-
"semi": false,
3-
"singleQuote": true,
4-
"trailingComma": "es5",
5-
"jsxBracketSameLine": true
6-
}
1+
"@vtex/prettier-config"

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [0.2.2] - 2020-08-06
11+
### Fixed
12+
- Include origin to requests in order to bypass cache and avoid CORS issue.
13+
1014
## [0.2.1] - 2020-06-23
1115
### Fixed
1216
- Add global `__RUNTIME__` data local `runtime`.

package.json

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@vtex/render-extension-loader",
3-
"version": "0.2.1",
3+
"version": "0.2.2",
44
"description": "VTEX IO Render Extension Loader",
55
"main": "lib/render-extension-loader.js",
66
"browser": "lib/render-extension-loader.js",
@@ -9,7 +9,7 @@
99
"serve": "serve",
1010
"build": "rollup -c",
1111
"watch": "rollup -c -w",
12-
"lint": "tsc --noEmit && tslint src/**/*.ts",
12+
"lint": "tsc --noEmit && eslint .",
1313
"prepublishOnly": "run-s lint build",
1414
"prereleasy": "npm run lint",
1515
"postreleasy": "npm publish --access public"
@@ -25,14 +25,17 @@
2525
"homepage": "https://github.com/vtex/render-extension-loader#readme",
2626
"dependencies": {},
2727
"devDependencies": {
28-
"@types/node": "^7.0.5",
28+
"@types/jquery": "^3.5.1",
29+
"@types/node": "^14.0.27",
30+
"@vtex/prettier-config": "^0.3.1",
31+
"eslint": "^7.6.0",
32+
"eslint-config-vtex": "^12.8.2",
2933
"npm-run-all": "^4.1.2",
34+
"prettier": "^2.0.5",
3035
"rollup": "^0.58.1",
3136
"rollup-plugin-typescript2": "^0.13.0",
3237
"rollup-plugin-uglify": "^3.0.0",
3338
"serve": "^6.5.5",
34-
"tslint": "^5.2.0",
35-
"tslint-config-vtex": "^2.0.0",
36-
"typescript": "^2.4.2"
39+
"typescript": "^3.9.7"
3740
}
3841
}

src/render-extension-loader.ts

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,16 @@ declare global {
33
__RENDER_6_RUNTIME__: any
44
__RENDER_7_RUNTIME__: any
55
__RUNTIME__: any
6-
$: any
6+
$: typeof import('jquery')
77
RenderExtensionLoader: any
88
}
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+
}
916
}
1017

1118
class RenderExtensionLoader {
@@ -43,26 +50,27 @@ class RenderExtensionLoader {
4350
? 'myvtexdev.com'
4451
: 'myvtex.com')
4552
this.get = window.$
46-
? url =>
53+
? (url: string) =>
4754
window.$.ajax({ url, timeout: this.timeout }).retry({
4855
timeout: 2000,
4956
times: 2,
5057
})
5158
: window.fetch
52-
? url =>
59+
? (url: string) =>
5360
new Promise((resolve, reject) => {
5461
const fetchTimeout = setTimeout(() => {
5562
reject({ error: 'timeout' })
5663
}, this.timeout)
5764

5865
window
5966
.fetch(url)
60-
.then(res => {
67+
.then((res) => {
6168
clearTimeout(fetchTimeout)
69+
6270
return res
6371
})
64-
.then(res => res.json())
65-
.then(res => resolve(res))
72+
.then((res) => res.json())
73+
.then((res) => resolve(res))
6674
})
6775
: null
6876

@@ -106,7 +114,7 @@ class RenderExtensionLoader {
106114
return this.runtime
107115
}
108116

109-
public update = runtimeOrUpdateFn => {
117+
public update = (runtimeOrUpdateFn) => {
110118
if (typeof runtimeOrUpdateFn === 'function') {
111119
this.runtime = runtimeOrUpdateFn(this.runtime)
112120
} else {
@@ -129,6 +137,7 @@ class RenderExtensionLoader {
129137

130138
this.time('render-extension-loader:render')
131139
const runtime = window[`__RENDER_${this.renderMajor}_RUNTIME__`]
140+
132141
runtime.render(extension, this.runtime, element)
133142
this.timeEnd('render-extension-loader:render')
134143

@@ -138,17 +147,22 @@ class RenderExtensionLoader {
138147
private loadExtensionPointsContext = async () => {
139148
this.time('render-extension-loader:json')
140149
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}`
142151
)
152+
143153
this.timeEnd('render-extension-loader:json')
144154

145155
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+
) {
147160
runtime[key] = window.__RUNTIME__[key]
148161
}
149162
}
150163

151164
this.setGlobalContext({ runtime, styles, scripts })
165+
152166
return { runtime, styles, scripts }
153167
}
154168

@@ -164,23 +178,26 @@ class RenderExtensionLoader {
164178

165179
private getExistingScriptSrcs = () => {
166180
const paths = []
181+
167182
for (let i = 0; i < document.scripts.length; i++) {
168183
paths.push(document.scripts.item(i).src)
169184
}
185+
170186
return paths
171187
}
172188

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)
175191
}
176192

177-
private addScriptToPage = src => {
193+
private addScriptToPage = (src) => {
178194
return new Promise((resolve, reject) => {
179195
if (this.scriptOnPage(src)) {
180196
return resolve()
181197
}
182198

183199
const script = document.createElement('script')
200+
184201
script.crossOrigin = 'anonymous'
185202
script.onload = () => resolve()
186203
script.onerror = () => reject()
@@ -190,21 +207,22 @@ class RenderExtensionLoader {
190207
})
191208
}
192209

193-
private addStyleToPage = href => {
210+
private addStyleToPage = (href) => {
194211
const link = document.createElement('link')
212+
195213
link.href = href
196214
link.type = 'text/css'
197215
link.rel = 'stylesheet'
198216
document.head.appendChild(link)
199217
}
200218

201-
private time = label => {
219+
private time = (label) => {
202220
if (this.verbose) {
203221
console.time(label)
204222
}
205223
}
206224

207-
private timeEnd = label => {
225+
private timeEnd = (label) => {
208226
if (this.verbose) {
209227
console.timeEnd(label)
210228
}

tslint.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)