@envelop/preload-assets 3.4.0 → 3.4.1-alpha-72027859.0
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/README.md +27 -27
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -12,9 +12,9 @@ yarn add @envelop/preload-assets
|
|
|
12
12
|
```
|
|
13
13
|
|
|
14
14
|
```ts
|
|
15
|
-
import { envelop } from '@envelop/core'
|
|
16
|
-
import { usePreloadAssets } from '@envelop/preload-asset'
|
|
17
|
-
import { makeExecutableSchema } from 'graphql'
|
|
15
|
+
import { envelop } from '@envelop/core'
|
|
16
|
+
import { usePreloadAssets } from '@envelop/preload-asset'
|
|
17
|
+
import { makeExecutableSchema } from 'graphql'
|
|
18
18
|
|
|
19
19
|
const schema = makeExecutableSchema({
|
|
20
20
|
typeDefs: /* GraphQL */ `
|
|
@@ -25,17 +25,17 @@ const schema = makeExecutableSchema({
|
|
|
25
25
|
resolvers: {
|
|
26
26
|
Query: {
|
|
27
27
|
imageUrl: (_: unknown, __: unknown, context: any) => {
|
|
28
|
-
const imageUrl = 'https://localhost/some-asset.png'
|
|
29
|
-
context.registerPreloadAsset(imageUrl)
|
|
30
|
-
return Promise.resolve(imageUrl)
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
})
|
|
28
|
+
const imageUrl = 'https://localhost/some-asset.png'
|
|
29
|
+
context.registerPreloadAsset(imageUrl)
|
|
30
|
+
return Promise.resolve(imageUrl)
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
})
|
|
35
35
|
|
|
36
36
|
const getEnveloped = envelop({
|
|
37
|
-
plugins: [usePreloadAssets()]
|
|
38
|
-
})
|
|
37
|
+
plugins: [usePreloadAssets()]
|
|
38
|
+
})
|
|
39
39
|
```
|
|
40
40
|
|
|
41
41
|
**Example response**
|
|
@@ -54,27 +54,27 @@ const getEnveloped = envelop({
|
|
|
54
54
|
**Example client prefetch logic**
|
|
55
55
|
|
|
56
56
|
```ts
|
|
57
|
-
const preloadAsset =
|
|
58
|
-
var request = new XMLHttpRequest()
|
|
59
|
-
request.open('GET', url)
|
|
60
|
-
request.responseType = 'blob'
|
|
57
|
+
const preloadAsset = url => {
|
|
58
|
+
var request = new XMLHttpRequest()
|
|
59
|
+
request.open('GET', url)
|
|
60
|
+
request.responseType = 'blob'
|
|
61
61
|
request.onload = () => {
|
|
62
|
-
if
|
|
63
|
-
console.error(
|
|
62
|
+
if (request.status !== 200) {
|
|
63
|
+
console.error(new Error(`Image preload failed; error code '${request.statusText}'.`))
|
|
64
64
|
}
|
|
65
|
-
}
|
|
65
|
+
}
|
|
66
66
|
request.onerror = () => {
|
|
67
|
-
console.error(new Error(`There was a network error while preloading '${url}'.`))
|
|
68
|
-
}
|
|
69
|
-
request.send()
|
|
67
|
+
console.error(new Error(`There was a network error while preloading '${url}'.`))
|
|
68
|
+
}
|
|
69
|
+
request.send()
|
|
70
70
|
}
|
|
71
71
|
|
|
72
72
|
// call this function with the execution result within your network layer.
|
|
73
|
-
const onExecutionResult =
|
|
73
|
+
const onExecutionResult = result => {
|
|
74
74
|
if (Array.isArray(result?.extensions?.preloadAssets)) {
|
|
75
|
-
result.extension.preloadAssets.forEach(
|
|
76
|
-
preloadAsset(url)
|
|
77
|
-
})
|
|
75
|
+
result.extension.preloadAssets.forEach(url => {
|
|
76
|
+
preloadAsset(url)
|
|
77
|
+
})
|
|
78
78
|
}
|
|
79
|
-
}
|
|
79
|
+
}
|
|
80
80
|
```
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@envelop/preload-assets",
|
|
3
|
-
"version": "3.4.0",
|
|
3
|
+
"version": "3.4.1-alpha-72027859.0",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"peerDependencies": {
|
|
6
|
-
"@envelop/core": "
|
|
6
|
+
"@envelop/core": "2.4.1-alpha-72027859.0",
|
|
7
7
|
"graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {},
|