@sitecore-feaas/clientside 0.1.19 → 0.1.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/basic.js +5 -1
- package/build/basic.js.map +1 -1
- package/build/cloud.js +40 -7
- package/build/cloud.js.map +1 -1
- package/build/components/FEAASBase.js +68 -29
- package/build/components/FEAASBase.js.map +1 -1
- package/build/components/FEAASComponent.js +36 -2
- package/build/components/FEAASComponent.js.map +1 -1
- package/build/components/FEAASElement.js +19 -0
- package/build/components/FEAASElement.js.map +1 -0
- package/build/components/FEAASLoader.js +13 -1
- package/build/components/FEAASLoader.js.map +1 -1
- package/build/components/FEAASStylesheet.js +1 -1
- package/build/components/FEAASStylesheet.js.map +1 -1
- package/build/dom.js +6 -0
- package/build/dom.js.map +1 -1
- package/build/fetch.js +48 -45
- package/build/fetch.js.map +1 -1
- package/build/ui/FEAASContext.js +13 -14
- package/build/ui/FEAASContext.js.map +1 -1
- package/build/ui/FEAASEditor.js +448 -76
- package/build/ui/FEAASEditor.js.map +1 -1
- package/build/ui/FEAASPicker.js +9 -59
- package/build/ui/FEAASPicker.js.map +1 -1
- package/dist/browser/basic.esm.js +5 -5
- package/dist/browser/basic.esm.js.map +1 -1
- package/dist/browser/basic.js +5 -5
- package/dist/browser/basic.js.map +1 -1
- package/dist/browser/index.esm.js +5 -10
- package/dist/browser/index.esm.js.map +1 -1
- package/dist/browser/index.js +5 -10
- package/dist/browser/index.js.map +1 -1
- package/dist/node/basic.esm.js +3 -3
- package/dist/node/basic.esm.js.map +1 -1
- package/dist/node/basic.js +3 -3
- package/dist/node/basic.js.map +1 -1
- package/dist/node/index.esm.js +3 -8
- package/dist/node/index.esm.js.map +1 -1
- package/dist/node/index.js +3 -8
- package/dist/node/index.js.map +1 -1
- package/examples/index.html +8 -49
- package/package.json +1 -6
- package/src/basic.ts +8 -1
- package/src/cloud.ts +43 -13
- package/src/components/FEAASBase.tsx +82 -32
- package/src/components/{FEAASComponent.ts → FEAASComponent.tsx} +38 -2
- package/src/components/FEAASElement.tsx +32 -0
- package/src/components/{FEAASLoader.ts → FEAASLoader.tsx} +14 -1
- package/src/components/{FEAASStylesheet.ts → FEAASStylesheet.tsx} +1 -1
- package/src/dom.ts +5 -0
- package/src/fetch.ts +82 -51
- package/src/ui/FEAASContext.tsx +14 -15
- package/src/ui/FEAASEditor.tsx +599 -145
- package/src/ui/FEAASPicker.tsx +19 -66
- package/test/components.ts +19 -0
- package/types/basic.d.ts +3 -0
- package/types/basic.d.ts.map +1 -1
- package/types/cloud.d.ts +8 -1
- package/types/cloud.d.ts.map +1 -1
- package/types/components/FEAASBase.d.ts +20 -6
- package/types/components/FEAASBase.d.ts.map +1 -1
- package/types/components/FEAASComponent.d.ts +8 -1
- package/types/components/FEAASComponent.d.ts.map +1 -1
- package/types/components/FEAASElement.d.ts +21 -0
- package/types/components/FEAASElement.d.ts.map +1 -0
- package/types/components/FEAASLoader.d.ts +6 -0
- package/types/components/FEAASLoader.d.ts.map +1 -1
- package/types/components/FEAASStylesheet.d.ts +3 -3
- package/types/components/FEAASStylesheet.d.ts.map +1 -1
- package/types/dom.d.ts.map +1 -1
- package/types/fetch.d.ts +42 -1
- package/types/fetch.d.ts.map +1 -1
- package/types/tsbuildinfo +1 -1
- package/types/tsbuildinfo_ui +1 -1
- package/types/ui/FEAASContext.d.ts +4 -4
- package/types/ui/FEAASContext.d.ts.map +1 -1
- package/types/ui/FEAASEditor.d.ts +70 -4
- package/types/ui/FEAASEditor.d.ts.map +1 -1
- package/types/ui/FEAASPicker.d.ts +14 -33
- package/types/ui/FEAASPicker.d.ts.map +1 -1
package/src/fetch.ts
CHANGED
|
@@ -1,64 +1,95 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Utility to cache-bust static files. It relies on reading Date header to detect cached response, so it requires CORS
|
|
3
|
+
* settings to be set.
|
|
4
|
+
*
|
|
5
|
+
* It accepts validator function that determines if response is usable. If response was pulled from cache and is valid,
|
|
6
|
+
* the optional callback function is fired right away. Then HEAD request is made bypassing the cache, to see if the
|
|
7
|
+
* response has an newer Last-Modified header. In case it does and it passes validation, third request is fired that
|
|
8
|
+
* actually fetches the new content and callback is invoked the second time.
|
|
9
|
+
*
|
|
10
|
+
* It works as promise, but also accepts callback to use cached blobs mid-flight and have access to headers. Any network
|
|
11
|
+
* errors will not be caught. Function throws when unable to fetch valid response.
|
|
12
|
+
*/
|
|
13
|
+
export interface ValidatableRequest {
|
|
14
|
+
url: Parameters<typeof fetch>[0]
|
|
15
|
+
options?: Parameters<typeof fetch>[1]
|
|
16
|
+
validator?: (resposne: Response) => boolean
|
|
17
|
+
callback?: (response: Response, status: 'initial' | 'updated' | 'check') => void
|
|
18
|
+
isCached?: boolean
|
|
19
|
+
response?: Response
|
|
20
|
+
}
|
|
15
21
|
|
|
16
|
-
*/
|
|
17
22
|
export async function fetchAndRevalidate(
|
|
18
|
-
url:
|
|
19
|
-
options
|
|
20
|
-
validator
|
|
21
|
-
callback
|
|
23
|
+
url: ValidatableRequest['url'],
|
|
24
|
+
options?: ValidatableRequest['options'],
|
|
25
|
+
validator?: ValidatableRequest['validator'],
|
|
26
|
+
callback?: ValidatableRequest['callback']
|
|
22
27
|
) {
|
|
23
28
|
// make request as usual, see if there's cached version of an asset
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
var response = initialResponse
|
|
29
|
-
callback(initialResponse, 'cached')
|
|
29
|
+
return fetchAndValidate({ url, options, validator, callback })
|
|
30
|
+
.then(refetchAndValidate)
|
|
31
|
+
.then((result) => result.response)
|
|
32
|
+
}
|
|
30
33
|
|
|
34
|
+
export async function fetchAndValidate(
|
|
35
|
+
{ url, options, validator, callback }: ValidatableRequest,
|
|
36
|
+
phase: 'initial' | 'updated' | 'check' = 'initial'
|
|
37
|
+
) {
|
|
38
|
+
const date = new Date()
|
|
39
|
+
console.log('fetch', phase, url)
|
|
40
|
+
const response = await fetch(url, options)
|
|
41
|
+
if (!response.ok) throw new Error('Fetch failed')
|
|
42
|
+
if (validator?.(response) === false) throw new Error('Response is not valid')
|
|
31
43
|
// see if response was pulled from cache
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
44
|
+
const isCached = new Date(response.headers.get('date')) < date
|
|
45
|
+
callback?.(response, phase)
|
|
46
|
+
return {
|
|
47
|
+
url,
|
|
48
|
+
options,
|
|
49
|
+
isCached,
|
|
50
|
+
response,
|
|
51
|
+
validator
|
|
52
|
+
}
|
|
53
|
+
}
|
|
39
54
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
55
|
+
export async function refetchAndValidate(request: ValidatableRequest & Required<Pick<ValidatableRequest, 'response'>>) {
|
|
56
|
+
const { url, isCached, options, validator, response, callback } = request
|
|
57
|
+
if (isCached) {
|
|
58
|
+
// check if response was stale by sending quick HEAD request busting cache
|
|
59
|
+
const { response: head } = await fetchAndValidate(
|
|
60
|
+
{
|
|
61
|
+
url: url + '?' + Math.random(),
|
|
62
|
+
options: {
|
|
63
|
+
...options,
|
|
64
|
+
method: 'HEAD',
|
|
65
|
+
cache: 'no-cache'
|
|
66
|
+
},
|
|
67
|
+
validator
|
|
68
|
+
},
|
|
69
|
+
'check'
|
|
70
|
+
)
|
|
47
71
|
|
|
72
|
+
// there's a newer version of the file that we may fetch
|
|
73
|
+
if (head.headers.get('last-modified') != response.headers.get('last-modified')) {
|
|
48
74
|
// otherwise the new resource has to be re-fetched
|
|
49
|
-
|
|
50
|
-
...options,
|
|
51
|
-
cache: 'reload'
|
|
52
|
-
})
|
|
75
|
+
return invalidateRequest(request)
|
|
53
76
|
}
|
|
54
77
|
}
|
|
78
|
+
return request
|
|
79
|
+
}
|
|
55
80
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
81
|
+
export async function invalidateRequest(request: ValidatableRequest) {
|
|
82
|
+
const { url, options, validator, callback } = request
|
|
83
|
+
return fetchAndValidate(
|
|
84
|
+
{
|
|
85
|
+
url,
|
|
86
|
+
options: {
|
|
87
|
+
...options,
|
|
88
|
+
cache: 'reload'
|
|
89
|
+
},
|
|
90
|
+
validator,
|
|
91
|
+
callback
|
|
92
|
+
},
|
|
93
|
+
'updated'
|
|
94
|
+
)
|
|
64
95
|
}
|
package/src/ui/FEAASContext.tsx
CHANGED
|
@@ -4,7 +4,7 @@ import { FEAASBase, OptionalExcept } from '../components/FEAASBase.js'
|
|
|
4
4
|
export function FEAASContextProps(element: HTMLElement = this) {
|
|
5
5
|
return {
|
|
6
6
|
library: element?.getAttribute('library') as string,
|
|
7
|
-
src: element?.getAttribute('src') ?? '/
|
|
7
|
+
src: element?.getAttribute('src') ?? '/context.js',
|
|
8
8
|
frontend: element?.getAttribute('frontend') ?? 'https://components.sitecoreclud.io',
|
|
9
9
|
backend: element?.getAttribute('backend') ?? 'https://components-api.sitecoreclud.io',
|
|
10
10
|
cdn: element?.getAttribute('cdn') ?? 'https://feaascompontsapiprod.blob.core.windows.net',
|
|
@@ -36,26 +36,24 @@ export class FEAASContext extends FEAASBase<FEAASContextProps, typeof FEAASConte
|
|
|
36
36
|
return this.shadowRoot || this.attachShadow({ mode: 'open' })
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
if (!this.
|
|
42
|
-
this.
|
|
43
|
-
this.getRoot().appendChild(this.
|
|
39
|
+
reactRootElement: HTMLDivElement
|
|
40
|
+
getReactRootElement() {
|
|
41
|
+
if (!this.reactRootElement) {
|
|
42
|
+
this.reactRootElement = document.createElement('div')
|
|
43
|
+
this.getRoot().appendChild(this.reactRootElement)
|
|
44
44
|
}
|
|
45
|
-
return this.
|
|
45
|
+
return this.reactRootElement
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
/** Pull in children FEAAS react elements into the shadow DOM so they receieve the styles */
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
this.getRoot().appendChild(target)
|
|
55
|
-
}
|
|
49
|
+
connectedCallback(): void {
|
|
50
|
+
Array.from(this.children).forEach((child) => {
|
|
51
|
+
this.getRoot().appendChild(child)
|
|
52
|
+
})
|
|
53
|
+
super.connectedCallback()
|
|
56
54
|
}
|
|
57
55
|
|
|
58
|
-
|
|
56
|
+
render(
|
|
59
57
|
{ children, library }: React.PropsWithChildren<FEAASContextProps>,
|
|
60
58
|
{ AppContext, Chakra: { Button, Tooltip }, React }: typeof this.payload
|
|
61
59
|
) {
|
|
@@ -69,6 +67,7 @@ export class FEAASContext extends FEAASBase<FEAASContextProps, typeof FEAASConte
|
|
|
69
67
|
}
|
|
70
68
|
return (
|
|
71
69
|
<AppContext
|
|
70
|
+
backend={this.props.backend}
|
|
72
71
|
libraryId={this.props.library}
|
|
73
72
|
container={this.getRoot()}
|
|
74
73
|
getToken={this.props.getToken}
|