npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@bams-app/version-check

v0.2.1

Published

版本变化检测:监听部署版本变化,全局事件通知并弹窗提示刷新页面

Readme

@bams-app/version-check

版本变化检测包:监听部署版本变化,版本变化时派发全局事件并弹窗提示用户刷新页面,避免用户访问到旧版本。

功能特性

  • 首次打开自动记录:本地无版本记录时,请求版本地址获取当前部署版本并存入本地储存
  • 版本变化检测:本地有版本记录时,与远程最新版本对比,变化则派发全局事件 version:changed
  • 全局事件通知:所有引入本包并注册事件的模块都会收到版本变化消息
  • 全局弹窗:版本变化时默认弹出提示框,引导用户刷新页面、清除缓存
  • 部署唯一标识:以环境变量 BASE_URL 作为部署标识,多套部署共用浏览器也不会互相干扰
  • 轮询检测:默认每 5 分钟检测一次,可配置;部署后打开的旧页面也能及时感知
  • 延迟首次检测:页面加载后延迟 3 秒(可配置)再执行首次检测,避免与页面初始化抢资源
  • 可靠提醒:检测到变化后不立即记录新版本,弹窗被关闭/错过时下次轮询仍会再次提示(最多 maxPromptCount 次),避免遗漏更新提醒
  • 默认关闭,按需开启:仅通过环境变量 VUE_APP_ENABLE_VERSION_CHECK=true 开启;调试模式由代码参数控制且仅在功能开启后生效

版本来源

默认请求 ${BASE_URL}version.json(部署根路径下的版本文件),格式如下:

{
  "version": "1.0.0",
  "build_time": "2026-08-11 10:00:00",
  "image": "bams-energy-web",
  "app": "bams_energy"
}

仓库内的 CI 构建脚本(ci-build.sh / ci-build-multi.sh)打包时会自动生成该文件, 也可自行在部署目录放置,或通过配置项接入任意接口。

快速开始

1. 全局入口注册(main.js)

import { setupVersionCheck } from "@bams-app/version-check";

// 注册全局弹窗事件 + 启动版本检测(默认关闭,需 VUE_APP_ENABLE_VERSION_CHECK=true 开启,见下方“开启方式”)
setupVersionCheck();

2. 业务侧监听版本变化(可选)

任意引入了本包的模块,只要注册了事件,版本变化时都会收到消息:

// 方式一:全局事件 API
import { onVersionChange } from "@bams-app/version-check";

const off = onVersionChange(payload => {
  console.log("版本变化:", payload.oldVersion, "->", payload.newVersion);
});
// 组件卸载或不再需要时取消监听
off();
// 方式二:Vue 组合式 API(组件内使用,自动在卸载时取消监听)
import { useVersionCheck } from "@bams-app/version-check";

const { isVersionChanged, versionInfo } = useVersionCheck();
// 方式三:Vue 插件方式
import versionCheck from "@bams-app/version-check";
app.use(versionCheck, options);

配置项

setupVersionCheck(options) 支持的配置:

| 参数 | 类型 | 默认值 | 说明 | | ----------------- | ------------------ | ------------------------- | ---------------------------------------------------------------------------------------------------- | | versionUrl | string\|function | ${BASE_URL}version.json | 版本请求地址,可为函数 (options) => url | | versionField | string | version | 请求返回 JSON 时读取的版本字段 | | interval | number | 300000(5 分钟) | 轮询间隔(毫秒),0 表示只检测一次 | | delay | number | 3000(3 秒) | 延迟首次检测(毫秒),0 表示页面加载后立即检测 | | maxPromptCount | number | 3 | 同一版本变化最多提示次数;达到上限后记录新版本停止提示,0 表示不限次数(每次轮询都提示,直到刷新) | | enabled | boolean | false | 强制开关;不传时默认关闭,由 VUE_APP_ENABLE_VERSION_CHECK=true 决定开启 | | debug | boolean | false | 调试模式(打印日志、暴露 window.__VERSION_CHECK__),仅在功能开启后生效,不通过环境变量控制 | | silent | boolean | true | 检测失败静默处理,仅派发 version:error 事件 | | emitOnFirstOpen | boolean | false | 首次打开(本地无记录)时是否派发 version:first-open 事件 | | modal | boolean | true | 版本变化时是否显示全局弹窗 | | modalOptions | object | {} | 弹窗配置(title/desc/okText/cancelText 等,透传给弹窗) | | request | function | fetch 实现 | 自定义版本请求函数 (url, options) => Promise<string\|object> | | parseVersion | function | 按 versionField 取值 | 自定义版本解析函数 (data, options) => string | | compareVersions | function | 字符串不相等 | 自定义版本对比函数,返回 true 表示发生变化 | | baseUrl | string | process.env.BASE_URL | 部署标识,一般无需手动设置 |

事件列表

| 事件 | 说明 | 载荷 | | -------------------- | ---------------------------------------------------------- | ----------------------------------------------------------- | | version:changed | 版本发生变化(主要事件) | { oldVersion, newVersion, remoteVersion, deployKey, ... } | | version:refresh | 用户点击弹窗"立即刷新"确认(刷新前触发,适合清理本地缓存) | 弹窗配置载荷 { deployKey, oldVersion, newVersion, ... } | | version:first-open | 首次打开(需开启 emitOnFirstOpen) | 同上 | | version:checked | 每次检测完成(无论是否变化) | 同上 | | version:error | 检测失败 | { error, ... } |

import { emitter, EVENT_NAME } from "@bams-app/version-check";

emitter.on(EVENT_NAME.VERSION_CHANGED, payload => {
  // 自定义处理
});

开启方式

版本检测默认关闭,仅通过环境变量 VUE_APP_ENABLE_VERSION_CHECK=true 开启(写入 .env.dev-xxx 或 .env.production):

VUE_APP_ENABLE_VERSION_CHECK=true

调试模式不通过环境变量控制,由代码参数 debug: true 开启,且仅在功能开启后生效(打印检测日志并暴露 window.__VERSION_CHECK__ 调试句柄):

setupVersionCheck({ debug: process.env.NODE_ENV === "development" });

开启后会在控制台打印检测日志,并暴露全局调试句柄:

// 模拟版本变化:先写入一个旧版本,再手动触发检测,即可看到弹窗
window.__VERSION_CHECK__.setLocalVersion("0.0.1");
window.__VERSION_CHECK__.check();

API 汇总

| API | 说明 | | -------------------------- | ---------------------------------------- | | setupVersionCheck() | 一键初始化(弹窗 + 检测) | | onVersionChange() | 注册版本变化事件监听 | | offVersionChange() | 取消版本变化事件监听 | | onVersionRefresh() | 注册"立即刷新"确认事件监听(刷新前触发) | | offVersionRefresh() | 取消"立即刷新"确认事件监听 | | useVersionCheck() | 组件内监听(响应式) | | checkVersion() | 手动检测一次 | | triggerVersionCheck() | 手动触发检测(使用启动时的配置) | | startVersionCheck() | 启动检测 | | stopVersionCheck() | 停止检测 | | getLocalVersion() | 读取本地记录版本 | | setLocalVersion() | 写入本地记录版本 | | clearLocalVersion() | 清除本地记录版本 | | fetchRemoteVersion() | 请求远程最新版本 | | showVersionChangeModal() | 手动展示全局弹窗 | | hideVersionChangeModal() | 关闭全局弹窗 | | VersionChangeModal | 弹窗组件(可自定义使用) |

注意事项

  • 检测到版本变化时不立即记录新版本,弹窗被关闭/错过时下次轮询仍会检测到并再次提示(最多 maxPromptCount 次),点击"立即刷新"或达到提示上限后才记录新版本。
  • 本地储存通过 @bams-app/utils 的 localCache 实现,key 为 version:deploy:${BASE_URL}。
  • version.json 请求携带时间戳参数并禁用缓存,确保拿到最新版本号。
  • 首次打开只记录版本、不弹窗;再次部署后版本变化才会弹窗提示。
  • 默认关闭,仅通过 VUE_APP_ENABLE_VERSION_CHECK=true 开启;调试模式由代码参数 debug: true 控制,且仅在功能开启后生效。