react h5 发版白屏问题解决方案

2023-10-28 23:45

本文主要是介绍react h5 发版白屏问题解决方案,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

背景:h5应用每次发布之后会有部分用户白屏
React v5

1. 优化路由加载时间 app.jsx
render() {if(this.state.loading) {return <PendingPage />}return (<Router><Suspense fallback={<PendingPage />}>{this.state.showPage ?<Switch>{routes.map((route, i) => (<RouteWithSubRoutes key={i} {...route} />))}</Switch>: <PendingPage />}</Suspense></Router>);}
2. 自刷新模式build_version-react路由
const path = require("path");
const fs = require("fs");// 将当前时间戳写入json文件
let json_obj = { build_version: new Date().getTime().toString(),status:200,success:true };
fs.writeFile(path.resolve(__dirname, "./public/json/build_version.json"),JSON.stringify(json_obj),function(err) {if (err) {return console.error(err);}console.log("打包字符串写入文件:public/json/build_version.json,成功!");}
);
// "build": "node create_build_version_json.js && react-app-rewired build",
// 写入package.json 打包先执行次node.js

增加react路由守卫,在每次切换页面去请求生成的时间戳

// FrontendAuth.js
import React, { Component } from "react";
import { Route, Redirect } from "react-router-dom";
import axios from "axios";class FrontendAuth extends Component {constructor(props) {super(props);}render() {const { routerConfig, location } = this.props;const { pathname } = location;axios.get("/json/build_version.json?v=" + new Date().getTime().toString()).then(res=>{let newBuildStr = res.data.build_version;let oldBuildStr = localStorage.getItem("build_version") || "";if (oldBuildStr !== newBuildStr) {localStorage.setItem("build_version", newBuildStr);window.location.reload();}})const targetRouterConfig = routerConfig.find((item) => item.path === pathname);if (targetRouterConfig) {const { component } = targetRouterConfig;return <Route exact path={pathname} component={component} />;}}
}export default FrontendAuth;
// app.jsx
render() {return (<Router><Suspense fallback={<PendingPage />}><Switch><FrontendAuth routerConfig={routerMap} /></Switch></Suspense></Router>);
}
// router/routerMap.js-路由集合
const Index= React.lazy(() => import('../pages/index/index.jsx'));
export default [{path: '/index',component: Index,}
]
3. webpack打包 生成的js版本号_v=hash值(config-overrides.js)
config.output.filename = 'static/js/[name].[contenthash].js?_v=[contenthash]'
config.output.chunkFilename = 'static/js/[name].[contenthash].chunk.js?_v=[contenthash]'
// 某些浏览器可能会忽略.js?v=xxxx
4. ng配置不缓存html(已存在)
#对html文件限制缓存        
location ~ .*\.(html)$ {            add_header Cache-Control no-store;add_header Pragma no-cache;        
}
5. 前端html header(index.html)(已存在)
<metahttp-equiv="cache-control"content="no-cache,no-store, must-revalidate"
/>
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Cache" content="no-cache" />
6. 灰度发布 / 服务器增加一台灰度服务器,运维控制流量切换服务器,着步切换

注: 目前灰度方案由前端cookie控制,但是必须要先全量发布之后才能走灰度,灰度方案不支持。
根据自己的情况。

7. 新增js报错监控,如果检测到js报错,跳转空页面。手动点击登录。(app.jsx)
componentDidCatch(error, info) {if (String(error).includes('Loading chunk')) {// 解决灰度切换js缺失的问题window.location.reload();} else {// 容错机制-会导致所有的报错回到此页面-再考虑一下!// window.location.replace('/404')}}

这篇关于react h5 发版白屏问题解决方案的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/296780

相关文章

部署Vue项目到服务器后404错误的原因及解决方案

《部署Vue项目到服务器后404错误的原因及解决方案》文章介绍了Vue项目部署步骤以及404错误的解决方案,部署步骤包括构建项目、上传文件、配置Web服务器、重启Nginx和访问域名,404错误通常是... 目录一、vue项目部署步骤二、404错误原因及解决方案错误场景原因分析解决方案一、Vue项目部署步骤

mybatis和mybatis-plus设置值为null不起作用问题及解决

《mybatis和mybatis-plus设置值为null不起作用问题及解决》Mybatis-Plus的FieldStrategy主要用于控制新增、更新和查询时对空值的处理策略,通过配置不同的策略类型... 目录MyBATis-plusFieldStrategy作用FieldStrategy类型每种策略的作

linux下多个硬盘划分到同一挂载点问题

《linux下多个硬盘划分到同一挂载点问题》在Linux系统中,将多个硬盘划分到同一挂载点需要通过逻辑卷管理(LVM)来实现,首先,需要将物理存储设备(如硬盘分区)创建为物理卷,然后,将这些物理卷组成... 目录linux下多个硬盘划分到同一挂载点需要明确的几个概念硬盘插上默认的是非lvm总结Linux下多

前端原生js实现拖拽排课效果实例

《前端原生js实现拖拽排课效果实例》:本文主要介绍如何实现一个简单的课程表拖拽功能,通过HTML、CSS和JavaScript的配合,我们实现了课程项的拖拽、放置和显示功能,文中通过实例代码介绍的... 目录1. 效果展示2. 效果分析2.1 关键点2.2 实现方法3. 代码实现3.1 html部分3.2

Python Jupyter Notebook导包报错问题及解决

《PythonJupyterNotebook导包报错问题及解决》在conda环境中安装包后,JupyterNotebook导入时出现ImportError,可能是由于包版本不对应或版本太高,解决方... 目录问题解决方法重新安装Jupyter NoteBook 更改Kernel总结问题在conda上安装了

pip install jupyterlab失败的原因问题及探索

《pipinstalljupyterlab失败的原因问题及探索》在学习Yolo模型时,尝试安装JupyterLab但遇到错误,错误提示缺少Rust和Cargo编译环境,因为pywinpty包需要它... 目录背景问题解决方案总结背景最近在学习Yolo模型,然后其中要下载jupyter(有点LSVmu像一个

解决jupyterLab打开后出现Config option `template_path`not recognized by `ExporterCollapsibleHeadings`问题

《解决jupyterLab打开后出现Configoption`template_path`notrecognizedby`ExporterCollapsibleHeadings`问题》在Ju... 目录jupyterLab打开后出现“templandroidate_path”相关问题这是 tensorflo

如何解决Pycharm编辑内容时有光标的问题

《如何解决Pycharm编辑内容时有光标的问题》文章介绍了如何在PyCharm中配置VimEmulator插件,包括检查插件是否已安装、下载插件以及安装IdeaVim插件的步骤... 目录Pycharm编辑内容时有光标1.如果Vim Emulator前面有对勾2.www.chinasem.cn如果tools工

在MySQL执行UPDATE语句时遇到的错误1175的解决方案

《在MySQL执行UPDATE语句时遇到的错误1175的解决方案》MySQL安全更新模式(SafeUpdateMode)限制了UPDATE和DELETE操作,要求使用WHERE子句时必须基于主键或索引... mysql 中遇到的 Error Code: 1175 是由于启用了 安全更新模式(Safe Upd

Python安装时常见报错以及解决方案

《Python安装时常见报错以及解决方案》:本文主要介绍在安装Python、配置环境变量、使用pip以及运行Python脚本时常见的错误及其解决方案,文中介绍的非常详细,需要的朋友可以参考下... 目录一、安装 python 时常见报错及解决方案(一)安装包下载失败(二)权限不足二、配置环境变量时常见报错及