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

相关文章

使用Python构建智能BAT文件生成器的完美解决方案

《使用Python构建智能BAT文件生成器的完美解决方案》这篇文章主要为大家详细介绍了如何使用wxPython构建一个智能的BAT文件生成器,它不仅能够为Python脚本生成启动脚本,还提供了完整的文... 目录引言运行效果图项目背景与需求分析核心需求技术选型核心功能实现1. 数据库设计2. 界面布局设计3

解决pandas无法读取csv文件数据的问题

《解决pandas无法读取csv文件数据的问题》本文讲述作者用Pandas读取CSV文件时因参数设置不当导致数据错位,通过调整delimiter和on_bad_lines参数最终解决问题,并强调正确参... 目录一、前言二、问题复现1. 问题2. 通过 on_bad_lines=‘warn’ 跳过异常数据3

Java.lang.InterruptedException被中止异常的原因及解决方案

《Java.lang.InterruptedException被中止异常的原因及解决方案》Java.lang.InterruptedException是线程被中断时抛出的异常,用于协作停止执行,常见于... 目录报错问题报错原因解决方法Java.lang.InterruptedException 是 Jav

解决RocketMQ的幂等性问题

《解决RocketMQ的幂等性问题》重复消费因调用链路长、消息发送超时或消费者故障导致,通过生产者消息查询、Redis缓存及消费者唯一主键可以确保幂等性,避免重复处理,本文主要介绍了解决RocketM... 目录造成重复消费的原因解决方法生产者端消费者端代码实现造成重复消费的原因当系统的调用链路比较长的时

kkFileView在线预览office的常见问题以及解决方案

《kkFileView在线预览office的常见问题以及解决方案》kkFileView在线预览Office常见问题包括base64编码配置、Office组件安装、乱码处理及水印添加,解决方案涉及版本适... 目录kkFileView在线预览office的常见问题1.base642.提示找不到OFFICE组件

深度解析Nginx日志分析与499状态码问题解决

《深度解析Nginx日志分析与499状态码问题解决》在Web服务器运维和性能优化过程中,Nginx日志是排查问题的重要依据,本文将围绕Nginx日志分析、499状态码的成因、排查方法及解决方案展开讨论... 目录前言1. Nginx日志基础1.1 Nginx日志存放位置1.2 Nginx日志格式2. 499

SpringBoot监控API请求耗时的6中解决解决方案

《SpringBoot监控API请求耗时的6中解决解决方案》本文介绍SpringBoot中记录API请求耗时的6种方案,包括手动埋点、AOP切面、拦截器、Filter、事件监听、Micrometer+... 目录1. 简介2.实战案例2.1 手动记录2.2 自定义AOP记录2.3 拦截器技术2.4 使用Fi

kkFileView启动报错:报错2003端口占用的问题及解决

《kkFileView启动报错:报错2003端口占用的问题及解决》kkFileView启动报错因office组件2003端口未关闭,解决:查杀占用端口的进程,终止Java进程,使用shutdown.s... 目录原因解决总结kkFileViewjavascript启动报错启动office组件失败,请检查of

SpringBoot 异常处理/自定义格式校验的问题实例详解

《SpringBoot异常处理/自定义格式校验的问题实例详解》文章探讨SpringBoot中自定义注解校验问题,区分参数级与类级约束触发的异常类型,建议通过@RestControllerAdvice... 目录1. 问题简要描述2. 异常触发1) 参数级别约束2) 类级别约束3. 异常处理1) 字段级别约束

Python错误AttributeError: 'NoneType' object has no attribute问题的彻底解决方法

《Python错误AttributeError:NoneTypeobjecthasnoattribute问题的彻底解决方法》在Python项目开发和调试过程中,经常会碰到这样一个异常信息... 目录问题背景与概述错误解读:AttributeError: 'NoneType' object has no at