Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
pearone committed Dec 26, 2023
1 parent 6ebe9f5 commit b25f28e
Show file tree
Hide file tree
Showing 9 changed files with 329 additions and 1 deletion.
11 changes: 11 additions & 0 deletions packages/axios/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# `axios`

> TODO: description
## Usage

```
const axios = require('axios');
// TODO: DEMONSTRATE API
```
7 changes: 7 additions & 0 deletions packages/axios/__tests__/axios.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

const axios = require('..');

describe('axios', () => {
it('needs tests');
});
20 changes: 20 additions & 0 deletions packages/axios/lib/axios.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* 这是axios的cancel服务,
* 支持自定义cancel策略
* 默认支持两个策略
*
* 当发送重复请求时候,且上一条请求还未完成,可取消上一次的请求。(CANCEL_LAST_AT_MOST_ONCE)
* 当发送重复请求时候,且上一条请求还未完成,可不发送本次请求。(CANCEL_CURRENT_AT_MOST_ONCE)
* 当路由切换时,可取消上一个页面的所有请求。(cancelReqs)
*
* export enum AjaxStrategy {
CANCEL_LAST_AT_MOST_ONCE = 'cancel_last_at_most_once',
CANCEL_CURRENT_AT_MOST_ONCE = 'cancel_current_at_most_once',
}
*/

module.exports = axios;

function axios() {
// TODO
}
32 changes: 32 additions & 0 deletions packages/axios/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "axios",
"version": "0.0.1",
"description": "axios strategy",
"keywords": [
"axios"
],
"author": "pearone <1181019452@qq.com>",
"homepage": "https://github.com/DPDFE/react-layout#readme",
"license": "ISC",
"main": "lib/axios.js",
"directories": {
"lib": "lib",
"test": "__tests__"
},
"files": [
"lib"
],
"repository": {
"type": "git",
"url": "git+https://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@github.com/DPDFE/react-layout.git"
},
"scripts": {
"test": "echo \"Error: run tests from root\" && exit 1"
},
"bugs": {
"url": "https://github.com/DPDFE/react-layout/issues"
},
"dependencies": {
"axios": "^1.6.2"
}
}
70 changes: 70 additions & 0 deletions packages/axios/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions packages/event/src/router/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* 场景:前后端分离的架构,后端更新,用户长期不刷新,导致接口访问不通
* 方案:
*/

export {};
149 changes: 149 additions & 0 deletions packages/event/src/router/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/**
* 类型定义
*/
interface ReloadSiteProps {
/** 在黑名单中的url不要刷页面 */
isInBlacklist?: (url?: string) => boolean;

/** 检查网站是否有更新(调用后端接口检查,或者直接获取'/'拿到html与当前页面初始加载时获取的'/'进行比对),若未传递,可设置一个默认方法 */
hasNewVersion?: (
old_html?: string,
online_url?: string
) => Promise<boolean>;

/** 可以通过获取html url是否发生变化来判断是否有新的代码上线 */
online_url?: string;
}

/**
* 当网页有新内容上线的时候,自动更新页面
*
* @param props - Props
*/
export default async function reloadSiteWhenRouteChange(
props = {} as ReloadSiteProps
) {
const {
isInBlacklist = () => false,
hasNewVersion = defaultHasNewVersion
} = props;

const old_html = await fetchHtml(props.online_url);

let check_time = +new Date();

/** 检查页面有更新后,此变量会变成true,下周期中,如果是true,直接刷新页面,实现网页更新 */
let need_load = false;

addRouteChangeListener();

/**
* 监听路由变化时,来检测页面是否有更新
*/
function addRouteChangeListener() {
const origin_pushState = window.history.pushState;
const origin_replaceState = window.history.replaceState;
const origin_hashChange = window.onhashchange;

/**
*重新history的pushState 和 replaceState方法
* @param data - data
* @param title - title
* @param url - url
*/
window.history.pushState = (data, title, url) => {
origin_pushState.apply(window.history, [data, title, url]);
reloadPageWhenUpdate();
};

window.history.replaceState = (data, title, url) => {
origin_replaceState.apply(window.history, [data, title, url]);
reloadPageWhenUpdate();
};

window.onhashchange = (event) => {
origin_hashChange?.apply(window, [event]);
reloadPageWhenUpdate();
};
}

/**
*如果网站有更新,reload page
*/
async function reloadPageWhenUpdate() {
if (need_load) {
window.location.reload();
}
if (
checkTime() &&
!isInBlacklist(window.location.href) &&
(await hasNewVersion(old_html, props.online_url))
) {
need_load = true;
}
}

/**
* 超过1小时后检查页面是否有更新
*
* @returns
*/
function checkTime() {
const now = +new Date();
const time_diff = now - check_time;
if (time_diff > 1000 * 60 * 60) {
check_time = +new Date();
return true;
}
console.log('未超过1小时');
return false;
}
}

/**
* 判断一下是否更新版本,默认通过对比html文件是否相等处理
*
* @param old_html - 旧版本的html
* @param online_url -
*/
async function defaultHasNewVersion(old_html: string, online_url?: string) {
try {
const new_html = await fetchHtml(online_url);

const res = old_html !== new_html;
return res;
} catch (e) {
console.error(e);
return false;
}
}

/**
* 获取html
*
* @param online_url - html url
*/
async function fetchHtml(online_url?: string) {
const res = await fetchWithTimeout(
online_url ?? window.location.protocol + '//' + window.location.host
);

return res.text();
}

/**
* 设置超过3600ms,终止fetch请求
*
* @param url - 请求参数
* @param timeout - 超时时间
* @returns
*/
async function fetchWithTimeout(url: string, timeout = 3600) {
const controller = new AbortController();
const id = setTimeout(() => controller.abort(), timeout);
const response = await fetch(url, {
signal: controller.signal
});
clearTimeout(id);
return response;
}
2 changes: 1 addition & 1 deletion packages/react-layout/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@dpdfe/react-layout",
"description": "react layout",
"author": "pearone stefan-hui",
"version": "2.2.22",
"version": "2.2.23",
"online_version": "2.0.27",
"dev_version": "2.1.21",
"license": "MIT",
Expand Down
33 changes: 33 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1449,6 +1449,11 @@
resolved "https://registry.yarnpkg.com/@discoveryjs/json-ext/-/json-ext-0.5.7.tgz#1d572bfbbe14b7704e0ba0f39b74815b84870d70"
integrity sha512-dBVuXR082gk3jsFp7Rd/JI4kytwGHecnCoTtXFb7DB6CNHp4rg5k1bhg0nWdLGLnOV71lmDzGQaLMy8iPLY0pw==

"@dpdfe/event-utils@^0.0.15":
version "0.0.15"
resolved "https://registry.yarnpkg.com/@dpdfe/event-utils/-/event-utils-0.0.15.tgz#7bcccda666120d72659ca392afd9e05082014f26"
integrity sha512-vxZO8jbFE9kIaZnbZgnTtt20/SGTGUGRS12jrZagsO8UmYZusniqTbeGNJI61xoAhXpW8fqvKPIVt8qFSJpcaQ==

"@dpdfe/react-layout@link:./packages/react-layout":
version "0.0.0"
uid ""
Expand Down Expand Up @@ -5134,6 +5139,15 @@ axios@^0.21.1:
dependencies:
follow-redirects "^1.14.0"

axios@^1.6.2:
version "1.6.2"
resolved "https://registry.yarnpkg.com/axios/-/axios-1.6.2.tgz#de67d42c755b571d3e698df1b6504cde9b0ee9f2"
integrity sha512-7i24Ri4pmDRfJTR7LDBhsOTtcm+9kjX5WiY1X3wIisx6G9So3pfMkEiU7emUBe46oceVImccTEM3k6C5dbVW8A==
dependencies:
follow-redirects "^1.15.0"
form-data "^4.0.0"
proxy-from-env "^1.1.0"

babel-jest@^29.1.2:
version "29.1.2"
resolved "https://registry.yarnpkg.com/babel-jest/-/babel-jest-29.1.2.tgz#540d3241925c55240fb0c742e3ffc5f33a501978"
Expand Down Expand Up @@ -8018,6 +8032,11 @@ follow-redirects@^1.14.0:
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.1.tgz#0ca6a452306c9b276e4d3127483e29575e207ad5"
integrity sha512-yLAMQs+k0b2m7cVxpS1VKJVvoz7SS9Td1zss3XRwXj+ZDH00RJgnuLx7E44wx02kQLrdM3aOOy+FpzS7+8OizA==

follow-redirects@^1.15.0:
version "1.15.3"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.3.tgz#fe2f3ef2690afce7e82ed0b44db08165b207123a"
integrity sha512-1VzOtuEM8pC9SFU1E+8KfTjZyMztRsgEfwQl44z8A25uy13jSzTj6dyK2Df52iV0vgHCfBwLhDWevLn95w5v6Q==

for-in@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/for-in/-/for-in-1.0.2.tgz#81068d295a8142ec0ac726c6e2200c30fb6d5e80"
Expand Down Expand Up @@ -8077,6 +8096,15 @@ form-data@^3.0.0:
combined-stream "^1.0.8"
mime-types "^2.1.12"

form-data@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452"
integrity sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==
dependencies:
asynckit "^0.4.0"
combined-stream "^1.0.8"
mime-types "^2.1.12"

form-data@~2.3.2:
version "2.3.3"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-2.3.3.tgz#dcce52c05f644f298c6a7ab936bd724ceffbf3a6"
Expand Down Expand Up @@ -12322,6 +12350,11 @@ proxy-addr@~2.0.7:
forwarded "0.2.0"
ipaddr.js "1.9.1"

proxy-from-env@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/proxy-from-env/-/proxy-from-env-1.1.0.tgz#e102f16ca355424865755d2c9e8ea4f24d58c3e2"
integrity sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==

prr@~1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/prr/-/prr-1.0.1.tgz#d3fc114ba06995a45ec6893f484ceb1d78f5f476"
Expand Down

0 comments on commit b25f28e

Please sign in to comment.