# html-webpack-prefetch-rpc

> ## Introduction 一种 rpc请求时机 提前到 head中 的 预加载方案

Latest version **1.0.0** (published 2021-04-08) · ISC license · 0 weekly downloads

## Install

```sh
npm install html-webpack-prefetch-rpc
pnpm add html-webpack-prefetch-rpc
yarn add html-webpack-prefetch-rpc
bun add html-webpack-prefetch-rpc
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2021-04-08 |
| First published | 2021-03-14 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 4.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | immortalz |
| Maintainers | immortalz |

## Links

- npm: https://www.npmjs.com/package/html-webpack-prefetch-rpc
- npm.io page: https://npm.io/package/html-webpack-prefetch-rpc

## Dependencies (2)

- [@babel/runtime](https://npm.io/package/@babel/runtime.md) ^7.13.10
- [@babel/runtime-corejs3](https://npm.io/package/@babel/runtime-corejs3.md) ^7.13.10

## Recent versions

- 1.0.0 (latest) — 2021-04-08
- 0.0.18-0 — 2021-03-25
- 0.0.17-0 — 2021-03-24
- 0.0.16-0 — 2021-03-24
- 0.0.15-0 — 2021-03-24
- 0.0.13-0 — 2021-03-18
- 0.0.12-0 — 2021-03-15
- 0.0.11-0 — 2021-03-15
- 0.0.10-0 — 2021-03-15
- 0.0.9-0 — 2021-03-15
- 0.0.8-0 — 2021-03-15
- 0.0.7-0 — 2021-03-15
- 0.0.6-0 — 2021-03-14
- 0.0.5-0 — 2021-03-14
- 0.0.4-0 — 2021-03-14
- … 8 more at https://npm.io/package/html-webpack-prefetch-rpc/versions

## README

# html-webpack-prefetch-rpc

## Introduction
一种 rpc请求时机 提前到 head中 的 预加载方案

## usage

```javascript
npm i -D html-webpack-prefetch-rpc
```

### step1、webpack.config#plugins 添加插件
```javascript
plugins: [
  ...,
  new HtmlWebpackPrefetchRpcPlugin({
    files: [{
      match: /homework\/index\.html/, // 设置匹配的页面
      dataUrlQueryRegx: /data_url=([^&]*)/ // 设置 url query中获取 data cdn url字段的正则
    }]
  })
]
```
### step2、项目中添加 prefetchRpc 方法
```javascript 
  const prefetchRpc = (
    dataCdnUrl: string,
    fetchRpcPromise: () => Promise<any>,
  ) => {
    return new Promise((resolve, reject) => {
      const prefecthData = window.prefetchRpcData?.[dataCdnUrl];
      if (window.enablePrefetchRpc && prefecthData) {
        // 开启缓存 && 缓存数据存在
        resolve(prefecthData);
      } else {
        // 从线上rpc读取
        fetchRpcPromise()
          .then(data => {
            resolve(data);
          })
          .catch(err => {
            reject(err);
          });
      }
    });
  };
```

### step3、项目中使用 prefetchRpc 来代替原来的 获取数据方法

原来 获取rpc数据的方法
```javascript
const dataUrl = 'xxxx';
axios.get(dataUrl, { timeout: 20000 }).then(res => {
  if (res.status === 200 && res.data) {
    resolve(res.data);
  } else {
    reject(new Error('获取数据异常'));
  }
});
```


```javascript
const dataUrl = 'xxxx';
prefetchRpc(dataUrl, () => {
  return new Promise((resolve, reject) => {
    // 原来 获取rpc数据的方法
    axios.get(dataUrl, { timeout: 20000 }).then(res => {
      if (res.status === 200 && res.data) {
        resolve(res.data);
      } else {
        reject(new Error('获取数据异常'));
      }
    });
  });
})
```

## License

---
_Source: https://npm.io/package/html-webpack-prefetch-rpc · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
