@servicetitan/docs-uikit 32.3.2 → 32.4.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.
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: usePrefetch
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Use `usePrefetch` to improve perceived performance by preloading MFE resources just before they're needed.
|
|
6
|
+
It returns a function that prefetches and adds MFE resources to the [Loader cache](./loader#cache-strategy), so subsequent loads are faster.
|
|
7
|
+
|
|
8
|
+
- Returns a `prefetch` function you can call with an MFE URL
|
|
9
|
+
- Automatically caches resources for subsequent loads
|
|
10
|
+
- Safely handles multiple prefetch calls for the same URL
|
|
11
|
+
- Integrates with the existing Loader caching strategy
|
|
12
|
+
|
|
13
|
+
:::tip
|
|
14
|
+
You are encouraged to use prefetching broadly, for example to prefetch all significant MFEs and any others that are commonly used.
|
|
15
|
+
:::
|
|
16
|
+
|
|
17
|
+
## Return Value
|
|
18
|
+
|
|
19
|
+
| Name | Description |
|
|
20
|
+
| :--------- | :--------------------------------------------------------------- |
|
|
21
|
+
| `prefetch` | A function that takes an MFE's URL and prefetches its resources. |
|
|
22
|
+
|
|
23
|
+
### Prefetch Parameters
|
|
24
|
+
|
|
25
|
+
The `prefetch` function returned by `usePrefetch` takes the following arguments:
|
|
26
|
+
|
|
27
|
+
| Name | Type | Description |
|
|
28
|
+
| :-------- | :------- | :-------------------------------------------- |
|
|
29
|
+
| `src` | `string` | The `src` URL passed to [`Loader`](./loader). |
|
|
30
|
+
| `options` | `object` | (Optional) Prefetch options |
|
|
31
|
+
|
|
32
|
+
### Prefetch Options
|
|
33
|
+
|
|
34
|
+
| Option | Type | Default | Description |
|
|
35
|
+
| ------- | ------------------- | ------- | ---------------------------------------------------------------------------- |
|
|
36
|
+
| `cache` | `boolean \| number` | `-1` | (Optional) The [cache strategy](./loader#cache-strategy) passed to `Loader`. |
|
|
37
|
+
|
|
38
|
+
## Usage
|
|
39
|
+
|
|
40
|
+
This example demonstrates prefetching resources when the user hovers over a button that displays an MFE.
|
|
41
|
+
|
|
42
|
+
```tsx
|
|
43
|
+
import { useState } from 'react';
|
|
44
|
+
import { Button, Flex } from '@servicetitan/anvil2';
|
|
45
|
+
import { Loader, usePrefetch } from '@servicetitan/web-components';
|
|
46
|
+
|
|
47
|
+
export const MFEPage = () => {
|
|
48
|
+
const mfeUrl = 'http://localhost:8888';
|
|
49
|
+
const [showMfe, setShowMfe] = useState(false);
|
|
50
|
+
const { prefetch } = usePrefetch();
|
|
51
|
+
|
|
52
|
+
return (
|
|
53
|
+
<Flex direction="column" gap="4">
|
|
54
|
+
<Button onMouseEnter={() => prefetch(mfeUrl)} onClick={() => setShowMfe(show => !show)}>
|
|
55
|
+
{showMfe ? 'Hide MFE' : 'Show MFE'}
|
|
56
|
+
</Button>
|
|
57
|
+
{showMfe && <Loader src={mfeUrl} />}
|
|
58
|
+
</Flex>
|
|
59
|
+
);
|
|
60
|
+
};
|
|
61
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@servicetitan/docs-uikit",
|
|
3
|
-
"version": "32.
|
|
3
|
+
"version": "32.4.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -16,5 +16,5 @@
|
|
|
16
16
|
"cli": {
|
|
17
17
|
"webpack": false
|
|
18
18
|
},
|
|
19
|
-
"gitHead": "
|
|
19
|
+
"gitHead": "836bcc0dc0859bf1ac0d3c679996e7b753893674"
|
|
20
20
|
}
|