From b25f28ee29de73b638e6aabcb628121d9dc2ed25 Mon Sep 17 00:00:00 2001 From: pearone <1181019452@qq.com> Date: Tue, 26 Dec 2023 11:05:37 +0800 Subject: [PATCH] update --- packages/axios/README.md | 11 ++ packages/axios/__tests__/axios.test.js | 7 ++ packages/axios/lib/axios.ts | 20 ++++ packages/axios/package.json | 32 ++++++ packages/axios/pnpm-lock.yaml | 70 ++++++++++++ packages/event/src/router/index.ts | 6 + packages/event/src/router/test.ts | 149 +++++++++++++++++++++++++ packages/react-layout/package.json | 2 +- yarn.lock | 33 ++++++ 9 files changed, 329 insertions(+), 1 deletion(-) create mode 100644 packages/axios/README.md create mode 100644 packages/axios/__tests__/axios.test.js create mode 100644 packages/axios/lib/axios.ts create mode 100644 packages/axios/package.json create mode 100644 packages/axios/pnpm-lock.yaml create mode 100644 packages/event/src/router/index.ts create mode 100644 packages/event/src/router/test.ts diff --git a/packages/axios/README.md b/packages/axios/README.md new file mode 100644 index 00000000..5f5fd658 --- /dev/null +++ b/packages/axios/README.md @@ -0,0 +1,11 @@ +# `axios` + +> TODO: description + +## Usage + +``` +const axios = require('axios'); + +// TODO: DEMONSTRATE API +``` diff --git a/packages/axios/__tests__/axios.test.js b/packages/axios/__tests__/axios.test.js new file mode 100644 index 00000000..349c2820 --- /dev/null +++ b/packages/axios/__tests__/axios.test.js @@ -0,0 +1,7 @@ +'use strict'; + +const axios = require('..'); + +describe('axios', () => { + it('needs tests'); +}); diff --git a/packages/axios/lib/axios.ts b/packages/axios/lib/axios.ts new file mode 100644 index 00000000..b11949c2 --- /dev/null +++ b/packages/axios/lib/axios.ts @@ -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 +} diff --git a/packages/axios/package.json b/packages/axios/package.json new file mode 100644 index 00000000..075881e6 --- /dev/null +++ b/packages/axios/package.json @@ -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" + } +} diff --git a/packages/axios/pnpm-lock.yaml b/packages/axios/pnpm-lock.yaml new file mode 100644 index 00000000..40343a79 --- /dev/null +++ b/packages/axios/pnpm-lock.yaml @@ -0,0 +1,70 @@ +lockfileVersion: 5.4 + +specifiers: + axios: ^1.6.2 + +dependencies: + axios: 1.6.2 + +packages: + + /asynckit/0.4.0: + resolution: {integrity: sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==} + dev: false + + /axios/1.6.2: + resolution: {integrity: sha512-7i24Ri4pmDRfJTR7LDBhsOTtcm+9kjX5WiY1X3wIisx6G9So3pfMkEiU7emUBe46oceVImccTEM3k6C5dbVW8A==} + dependencies: + follow-redirects: 1.15.3 + form-data: 4.0.0 + proxy-from-env: 1.1.0 + transitivePeerDependencies: + - debug + dev: false + + /combined-stream/1.0.8: + resolution: {integrity: sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==} + engines: {node: '>= 0.8'} + dependencies: + delayed-stream: 1.0.0 + dev: false + + /delayed-stream/1.0.0: + resolution: {integrity: sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==} + engines: {node: '>=0.4.0'} + dev: false + + /follow-redirects/1.15.3: + resolution: {integrity: sha512-1VzOtuEM8pC9SFU1E+8KfTjZyMztRsgEfwQl44z8A25uy13jSzTj6dyK2Df52iV0vgHCfBwLhDWevLn95w5v6Q==} + engines: {node: '>=4.0'} + peerDependencies: + debug: '*' + peerDependenciesMeta: + debug: + optional: true + dev: false + + /form-data/4.0.0: + resolution: {integrity: sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==} + engines: {node: '>= 6'} + dependencies: + asynckit: 0.4.0 + combined-stream: 1.0.8 + mime-types: 2.1.35 + dev: false + + /mime-db/1.52.0: + resolution: {integrity: sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==} + engines: {node: '>= 0.6'} + dev: false + + /mime-types/2.1.35: + resolution: {integrity: sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==} + engines: {node: '>= 0.6'} + dependencies: + mime-db: 1.52.0 + dev: false + + /proxy-from-env/1.1.0: + resolution: {integrity: sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==} + dev: false diff --git a/packages/event/src/router/index.ts b/packages/event/src/router/index.ts new file mode 100644 index 00000000..afda4ffa --- /dev/null +++ b/packages/event/src/router/index.ts @@ -0,0 +1,6 @@ +/** + * 场景:前后端分离的架构,后端更新,用户长期不刷新,导致接口访问不通 + * 方案: + */ + +export {}; diff --git a/packages/event/src/router/test.ts b/packages/event/src/router/test.ts new file mode 100644 index 00000000..4e42cb6d --- /dev/null +++ b/packages/event/src/router/test.ts @@ -0,0 +1,149 @@ +/** + * 类型定义 + */ +interface ReloadSiteProps { + /** 在黑名单中的url不要刷页面 */ + isInBlacklist?: (url?: string) => boolean; + + /** 检查网站是否有更新(调用后端接口检查,或者直接获取'/'拿到html与当前页面初始加载时获取的'/'进行比对),若未传递,可设置一个默认方法 */ + hasNewVersion?: ( + old_html?: string, + online_url?: string + ) => Promise; + + /** 可以通过获取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; +} diff --git a/packages/react-layout/package.json b/packages/react-layout/package.json index bba0d3c0..a920cba1 100644 --- a/packages/react-layout/package.json +++ b/packages/react-layout/package.json @@ -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", diff --git a/yarn.lock b/yarn.lock index 2cc3aac0..00bc65e9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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 "" @@ -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" @@ -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" @@ -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" @@ -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"