当前位置: 首页 > wzjs >正文

网站建设注册网络销售渠道

网站建设注册,网络销售渠道,淄博营销网站建设服务,dede网站地图模板下载🤖 作者简介:水煮白菜王,一位资深前端劝退师 👻 👀 文章专栏: 前端专栏 ,记录一下平时在博客写作中,总结出的一些开发技巧和知识归纳总结✍。 感谢支持💕💕&a…

在这里插入图片描述

🤖 作者简介:水煮白菜王,一位资深前端劝退师 👻
👀 文章专栏: 前端专栏 ,记录一下平时在博客写作中,总结出的一些开发技巧和知识归纳总结✍。
感谢支持💕💕💕

目录

  • 一、应用场景
  • 二、实现原理
    • 2.1 核心检测逻辑
    • 2.2 实现优势
  • 三 、具体实现
    • 3.1 工程化封装
    • 3.2 关键方法解析
      • 脚本哈希获取:
      • 对比逻辑:
  • 四、全部代码🚀🚀🚀
    • 4.1 vue3
    • 4.2 vue2
  • 五、注意事项与常见问题
    • 5.1 可能出现的问题
    • 5.2 浏览器兼容方案

一、应用场景

在现代Web应用中,为了提升用户体验并确保系统的稳定性和一致性,部署前端版本更新后及时提醒用户进行页面刷新是至关重要的。当生产环境发布了包含功能变化的新版本时,由于单页面(SPA)应用的路由特性和浏览器缓存机制,用户浏览器可能不会自动加载最新的代码资源,这可能导致用户遇到bug或体验到不一致的功能行为。通过实现自动检测机制来提醒用户版本更新,并引导其刷新页面,可以

  1. 避免用户使用过期版本
  2. 确保功能一致性
  3. 减少接口兼容性问题
  4. 提高应用可靠性

二、实现原理

2.1 核心检测逻辑

变化
未变化
应用启动
生产环境?
启动定时器
结束流程
等待60秒
获取当前脚本哈希
首次运行?
保存初始哈希
哈希变化?
停止定时器
显示更新提示
用户确认?
刷新页面
记录取消操作

通过对比构建产物的哈希值变化实现版本检测:

  1. 定时轮询:每分钟检查静态资源变化
  2. 哈希对比:通过解析HTML中script标签指纹判断更新
  3. 强制刷新:检测到更新后提示用户刷新页面
// 核心对比逻辑
const isChanged = (oldSet, newSet) => {return oldSet.size !== newSet.size || ![...oldSet].every(hash => newSet.has(hash))
}

2.2 实现优势

  • 通用性强:适用于任意前端框架
  • 无侵入式检测:不依赖构建工具配置
  • 用户可控:提示框让用户选择刷新时机
  • 精准检测:通过对比script标签内容哈希值
  • 低资源消耗:每分钟检测一次,单次请求性能消耗低

三 、具体实现

3.1 工程化封装

// useVersionHash.js 核心实现
export default function useVersionHash() {// 状态管理const timerUpdate = ref(null)let scriptHashes = new Set()// 生命周期onMounted(() => startTimer())onBeforeUnmount(() => stopTimer())// 业务方法const fetchScriptHashes = async () => { /*...*/ }const compareScriptHashes = async () => { /*...*/ }return { compareScriptHashes }
}

3.2 关键方法解析

脚本哈希获取:

const fetchScriptHashes = async () => {const html = await fetch('/').then(res => res.text())const scriptRegex = /<script(?:\s+[^>]*)?>(.*?)<\/script\s*>/gireturn new Set(html?.match(scriptRegex) || [])
}

对比逻辑:

if (scriptHashes.size === 0) {// 初始化基准值scriptHashes = newScriptHashes  
} else if (scriptHashes.size !== newScriptHashes.size ||![...scriptHashes].every(hash => newScriptHashes.has(hash))
) {// 触发更新流程stopTimer()showUpdateDialog()
}

四、全部代码🚀🚀🚀

4.1 vue3

1、use-version-update.js具体逻辑

// @/utils/use-version-update.js
import { ref, onMounted, onBeforeUnmount } from 'vue'
import { ElMessageBox } from 'element-plus'let scriptHashes = new Set()
const timerUpdate = ref(null)export default function useVersionHash() {const isProduction = import.meta.env.MODE === 'production'const fetchScriptHashes = async () => {try {const html = await fetch('/').then((res) => res.text())const scriptRegex = /<script(?:\s+[^>]*)?>(.*?)<\/script\s*>/gireturn new Set(html?.match(scriptRegex) || [])} catch (error) {console.error('获取脚本哈希失败:', error)return new Set()}}const compareScriptHashes = async () => {try {const newScriptHashes = await fetchScriptHashes()if (scriptHashes.size === 0) {scriptHashes = newScriptHashes} else if (scriptHashes.size !== newScriptHashes.size ||![...scriptHashes].every(hash => newScriptHashes.has(hash))) {stopTimer()updateNotice()}} catch (error) {console.error('版本检查失败:', error)}}const updateNotice = () => {ElMessageBox.confirm('检测到新版本,建议立即更新以确保平台正常使用','更新提示',{confirmButtonText: '确定',cancelButtonText: '取消',type: 'warning'}).then(() => window.location.reload())}const startTimer = () => {if (!isProduction) returntimerUpdate.value = setInterval(compareScriptHashes, 60000)}const stopTimer = () => {timerUpdate.value && clearInterval(timerUpdate.value)}onMounted(startTimer)onBeforeUnmount(stopTimer)return { compareScriptHashes, updateNotice }
}

2、引入use-version-update.js

// App.vue
import versionUpdatefrom '@/utils/use-version-update.js'
export default {setup() {const { updateNotice } = versionUpdate()return { updateNotice }}
}

3、Vite 相关配置

// vite.config.js
import { defineConfig } from 'vite'export default defineConfig({build: {rollupOptions: {output: {// 主入口文件命名规则entryFileNames: 'js/[name]-[hash:8].js',// 代码分割块命名规则chunkFileNames: 'js/[name]-[hash:8].js',// 静态资源文件命名规则assetFileNames: ({ name }) => {const ext = name?.split('.').pop()return `assets/${ext}/[name]-[hash:8].[ext]`}}},// 启用文件哈希的manifest生成manifest: true}
})

也可以将use-version-update写成以JS、TS模块化封装,在入口文件中main.ts引入

// use-version-update.ts
export const versionUpdate = () => {... 具体处理逻辑
}// main.ts
import { versionUpdate} from "@/utils/use-version-update"
if (import.meta.env.MODE == 'production') {versionUpdate()
}

4.2 vue2

1、use-version-update.js具体逻辑

/** @Author: baicaiKing* @Date: 2025-01-02 13:50:33* @LastEditors: Do not edit* @LastEditTime: 2025-01-03 09:40:36* @FilePath: \code\src\utils\use-version-update.js*/
// 存储当前脚本标签的哈希值集合
let scriptHashes = new Set();
let timerUpdate = undefined;
export default {data() {return {};},created() {},mounted() {// 每60秒检查一次是否有新的脚本标签更新if (process.env.NODE_ENV === 'production') { // 只针对生产环境timerUpdate= setInterval(() => {this.compareScriptHashes()}, 60000);}},beforeDestroy() {clearInterval(timerUpdate);timerUpdate = null;},methods: {/*** 从首页获取脚本标签的哈希值集合* @returns {Promise<Set<string>>} 返回包含脚本标签的哈希值的集合*/async fetchScriptHashes() {// 获取首页HTML内容const html = await fetch('/').then((res) => res.text());// 正则表达式匹配所有<script>标签const scriptRegex = /<script(?:\s+[^>]*)?>(.*?)<\/script\s*>/gi;// 获取匹配到的所有<script>标签内容// const scripts = html.match(scriptRegex) ?? [];const scripts = html ? html.match(scriptRegex) || [] : [];// 将脚本标签内容存入集合并返回return new Set(scripts);},/*** 比较当前脚本标签的哈希值集合与新获取的集合,检测是否有更新*/async compareScriptHashes() {// 获取新的脚本标签哈希值集合const newScriptHashes = await this.fetchScriptHashes();if (scriptHashes.size === 0) {// 初次运行时,存储当前脚本标签哈希值scriptHashes = newScriptHashes;} else if (scriptHashes.size !== newScriptHashes.size ||![...scriptHashes].every((hash) => newScriptHashes.has(hash))) {// 如果脚本标签数量或内容发生变化,则认为有更新console.info('已检测到更新文件', {oldScript: [...scriptHashes],newScript: [...newScriptHashes],});// 清除定时器clearInterval(timerUpdate);// 提示用户更新this.updateNotice();} else {// 没有更新console.info('未检测到更新时机', {oldScript: [...scriptHashes],});}},updateNotice() {this.$confirm('检测到新版本,建议立即更新以确保平台正常使用', '更新提示', {confirmButtonText: '确定',cancelButtonText: '取消(自行刷新)',type: 'warning'}).then(() => {window.location.reload();}).catch(() => {console.eror('用户取消刷新!');});}},};

2、引入use-version-update.js

// App.vue
import versionUpdate from "@/util/use-version-update.js";
export default {name: "app",mixins: [versionUpdate],data() {return {};},
};

3、Webpack 相关配置


// vue.config
module.exports = {configureWebpack: {output: {filename: 'js/[name].[hash].js',// filename: 'js/[name].[contenthash].js',},},devServer: {},
};

五、注意事项与常见问题

5.1 可能出现的问题

问题现象可能原因解决方案
检测不准确正则匹配失效更新正则表达式
生产环境未生效环境变量配置错误检查构建配置
跨域请求失败部署路径不匹配调整fetch请求路径
内存泄漏定时器未正确清除使用WeakRef优化

5.2 浏览器兼容方案

可结合Service Worker实现无缝更新

// 支持Service Worker的渐进增强方案
if ('serviceWorker' in navigator) {navigator.serviceWorker.register('/sw.js').then(reg => {reg.addEventListener('updatefound', () => {showUpdateNotification()})})
}

同时要确保服务器配置正确缓存策略,通常Nginx缓存策略默认不用打理

在这里插入图片描述
如果你觉得这篇文章对你有帮助,请点赞 👍、收藏 👏 并关注我!👀
在这里插入图片描述


文章转载自:

http://ZZ7fin65.ypqwm.cn
http://Z4eKlbAU.ypqwm.cn
http://c1XhD9Ia.ypqwm.cn
http://1wUhxcp8.ypqwm.cn
http://PDxbz4wZ.ypqwm.cn
http://nEePR9S9.ypqwm.cn
http://tdm47JN6.ypqwm.cn
http://Q1qaHnfP.ypqwm.cn
http://HOOqfcde.ypqwm.cn
http://k1JyIeDG.ypqwm.cn
http://LxmzcHvy.ypqwm.cn
http://3v05M5yn.ypqwm.cn
http://YmLZ5FCd.ypqwm.cn
http://U7QH6NaP.ypqwm.cn
http://kfKw7mQy.ypqwm.cn
http://a3yVDdHL.ypqwm.cn
http://0UVZU61s.ypqwm.cn
http://Q63eyJBH.ypqwm.cn
http://SXAKgSXe.ypqwm.cn
http://LUpezjoq.ypqwm.cn
http://FZQAjjk8.ypqwm.cn
http://K901o2Jt.ypqwm.cn
http://iTfoRxCl.ypqwm.cn
http://LLKV8qrM.ypqwm.cn
http://PQJK5YiW.ypqwm.cn
http://H9GF1xN2.ypqwm.cn
http://AWL0xC6w.ypqwm.cn
http://4gHtNkqc.ypqwm.cn
http://Wr6lgw7F.ypqwm.cn
http://E1z8pJNZ.ypqwm.cn
http://www.dtcms.com/wzjs/652489.html

相关文章:

  • 网站建设前台后台中华建设网
  • 做特卖网站有哪些大连网站建设与维护题库
  • 网站的域名是什么意思可以接外包的网站
  • 慈溪网站建设哪家好泰安房产网签查询系统
  • 微站官网天津建设工程信息网官网首页
  • 保定建站公司模板网站建设的流程和内容
  • 企业为什么要做建站学的建筑专业后悔一辈子
  • 网站建设自适应网站常州市新北区建设局网站
  • 文本网站代码空两格怎么做曲靖网站推广
  • 自己电脑做网站好吗网站流量如何做
  • 小学做试卷的网站年终总结汇报ppt模板免费
  • 怎么看网站是不是用凡客做的怎样做品牌推广网站
  • 漳州做网站最便宜wordpress后台添加自定义输入框
  • 如何在百度举报网站成都环境设计公司
  • 酒水招商网站大全邢台短视频优化
  • 实验中心网站建设千博企业网站管理系统2013
  • 上海外贸网站seo网站优化培训公司
  • 网站做担保交易 是否资质wordpress 作品相册
  • 开发一个微信公众号关键词优化需要从哪些方面开展?
  • 开发网站网络公司wordpress构架都是模板
  • qq空间做网站芜湖市住房和城乡建设厅网站
  • 自己电脑做服务器搭建网站文件大小 wordpress
  • 网站制作厂家电话多少站长工具whois查询
  • 云南手机网站开发方正悠黑使用网站建设侵权么
  • 做投资网站安徽智能网站建设推荐
  • 网站开发设计是前端吗网站全网建设莱芜
  • 汽车行业网站建设比较好wordpress+4.4.1+漏洞
  • 上海做网站需要多少钱大象戌人视频入口2022
  • 外贸建站哪家公司专业1m带宽做网站
  • 建设标准 免费下载网站广州官方新闻