前端 项目更新通知 (plugin-web-update-notification)

项目版本更新迭代时,需提示用户更新系统,不然早时间不更新对用户体验很不好,所以在每次部署后需要提示用户,刷新静态资源。推荐插件 plugin-web-update-notification


.具体配置  vite.config.js文件中

import { webUpdateNotice } from '@plugin-web-update-notification/vite';
export default defineConfig(async ({ command, mode }) => {
 
  return {
 plugins: [
 webUpdateNotice({
        versionType: 'git_commit_hash',
        logVersion: true,
        hiddenDismissButton: true,
        notificationProps: {
          title: '📢系统更新通知',
          description: '检测到当前系统版本已更新,请刷新页面后使用',
          buttonText: '刷新',
        },
        notificationConfig: {
          placement: 'topRight',
        },
      }),

]

}
function webUpdateNotice(options?: Options): Plugin

export interface Options {
  /**
   * support 'git_commit_hash' | 'svn_revision_number' | 'pkg_version' | 'build_timestamp' | 'custom'
   * * if repository type is 'Git', default is 'git_commit_hash'
   * * if repository type is 'SVN', default is 'svn_revision_number'
   * * if repository type is 'unknown', default is 'build_timestamp'
   * */
  versionType?: VersionType
  /**
   * custom version, if versionType is 'custom', this option is required
   */
  customVersion?: string
  /** polling interval(ms)
   * if set to 0, it will not polling
   * @default 10 * 60 * 1000
   */
  checkInterval?: number
  /**
   * check update when window focus
   * @default true
   */
  checkOnWindowFocus?: boolean
  /**
   * check update immediately after page loaded
   * @default true
   */
  checkImmediately?: boolean
  /**
   * check update when load js file error
   * @default true
   */
  checkOnLoadFileError?: boolean
  /**
   * whether to output version in console
   *
   * you can also pass a function to handle the version
   * ```ts
   * logVersion: (version) => {
   *  console.log(`version: %c${version}`, 'color: #1890ff') // this is the default behavior
   * }
   * ```
   * @default true
   */
  logVersion?: boolean | ((version: string) => void)
  /**
   * whether to silence the notification.
   * such as when local version is v1.0, you can set this option to true and build a new version v1.0.1, then the notification will not show
   */
  silence?: boolean
  /**
   * @deprecated
   */
  customNotificationHTML?: string
  /** notificationProps have higher priority than locale */
  notificationProps?: NotificationProps
  notificationConfig?: NotificationConfig
  /**
   * preset: zh_CN | zh_TW | en_US
   * @default 'zh_CN'
   * */
  locale?: string
  /**
   * custom locale data
   * @link default data: https://github.com/GreatAuk/plugin-web-update-notification/blob/main/packages/core/src/locale.ts
   */
  localeData?: LocaleData
  /**
   * Whether to hide the default notification, if you set it to true, you need to custom behavior by yourself
   * ```ts
    document.body.addEventListener('plugin_web_update_notice', (e) => {
      const { version, options } = e.detail
      // write some code, show your custom notification and etc.
      alert('System update!')
    })
   * ```
   * @default false
   */
  hiddenDefaultNotification?: boolean
  /**
   * Whether to hide the dismiss button
   * @default false
   */
  hiddenDismissButton?: boolean
  /**
   * After version 1.2.0, you not need to set this option, it will be automatically detected from the base of vite config、publicPath of webpack config or publicPath of umi config
   *
   * Base public path for inject file, Valid values include:
   * * Absolute URL pathname, e.g. /foo/
   * * Full URL, e.g. https://foo.com/
   * * Empty string(default) or ./
   *
   * !!! Don't forget / at the end of the path
  */
  injectFileBase?: string
}

export type VersionType = 'git_commit_hash' | 'pkg_version' | 'build_timestamp' | 'custom'

export interface NotificationConfig {
  /**
   * refresh button color
   * @default '#1677ff'
  */
  primaryColor?: string
  /**
   * dismiss button color
   * @default 'rgba(0,0,0,.25)'
  */
  secondaryColor?: string
  /** @default 'bottomRight' */
  placement?: 'topLeft' | 'topRight' | 'bottomLeft' | 'bottomRight'
}

export interface NotificationProps {
  title?: string
  description?: string
  /** refresh button text */
  buttonText?: string
  /** dismiss button text */
  dismissButtonText?: string
}

export type LocaleData = Record<string, NotificationProps>

 

<think>好的,我现在需要帮助用户解决他们遇到的npm ERESOLVE overriding peer dependency问题,特别是关于vite和@plugin-web-update-notification/vite的版本冲突。让我先仔细分析这个问题。 首先,用户遇到的错误是npm的peer dependency冲突。这意味着用户的项目中安装的某个包(这里是@plugin-web-update-notification/vite)要求的vite版本与项目中现有的vite版本不兼容。根据用户提供的引用[1],npm建议使用--legacy-peer-deps或--force来绕过这个错误,但这可能只是暂时忽略问题,而不是真正的解决方案。 接下来,我需要确认用户的具体情况。用户提到使用了vite和@plugin-web-update-notification/vite插件,所以需要检查这两个包的版本兼容性。首先,我应该指导用户检查他们的vite版本和插件所需的vite版本范围。用户需要查看插件的文档或npm页面,确认支持的vite版本,然后对比项目中安装的vite版本是否在支持范围内。 如果版本不匹配,可能的解决方案包括升级或降级vite,或者寻找插件的兼容版本。例如,如果插件要求vite 3.x,而用户使用的是vite 4.x,可能需要升级插件到支持vite 4.x的版本,或者降级vite到3.x。如果插件没有兼容版本,可能需要寻找替代插件或联系维护者。 如果版本兼容但仍有冲突,可以尝试手动安装推荐的vite版本,然后重新安装插件。另外,使用--legacy-peer-deps选项可能暂时解决问题,但需注意潜在风险。用户还需要清理npm缓存,确保没有旧数据干扰安装过程。 此外,用户之前的问题涉及vite命令未找到,可能最近安装了新版本的Node.js或npm,导致环境变化。需要确保npm配置正确,没有全局安装的旧版本包干扰。 最后,用户可能需要更新package-lock.json或yarn.lock文件,确保依赖树正确解析。如果所有方法都失败,联系插件维护者反馈问题也是一个选项。总结步骤时,要条理清晰,逐步引导用户检查版本、调整安装命令,并注意后续验证。</think>### 解决 `npm WARN ERESOLVE overriding peer dependency` 版本冲突的完整方案 (针对 `vite` 和 `@plugin-web-update-notification/vite` 的冲突) --- #### 1. 定位版本冲突根源 1. **查看插件依赖要求** 在插件文档或 npm 页面中确认 `@plugin-web-update-notification/vite` 支持的 Vite 版本范围,例如: ```bash npm view @plugin-web-update-notification/vite peerDependencies ``` 若输出类似 `"vite": "^3.0.0"`,表示插件要求 Vite 3.x 版本。 2. **检查本地 Vite 版本** 在项目目录下运行: ```bash npm list vite ``` 若当前安装的 Vite 版本为 `4.x`,则与插件的 `3.x` 要求冲突。 --- #### 2. 针对性解决方案 ##### 方案一:升级插件或降级 Vite - **升级插件** 寻找支持 Vite 4.x 的插件版本: ```bash npm install @plugin-web-update-notification/vite@latest ``` - **降级 Vite** 若插件无新版本,降级 Vite 至兼容版本: ```bash npm install vite@3.2.5 # 安装指定兼容版本 ``` ##### 方案二:强制安装(临时修复) 添加 `--legacy-peer-deps` 忽略 peerDependency 冲突: ```bash npm install @plugin-web-update-notification/vite --legacy-peer-deps ``` **注意**:此操作可能导致依赖树不完整,仅建议临时使用[^1]。 ##### 方案三:手动锁定依赖版本 在 `package.json` 中固定版本并清理锁文件: ```json { "dependencies": { "vite": "3.2.5", "@plugin-web-update-notification/vite": "1.0.0" } } ``` 删除 `node_modules` 和 `package-lock.json` 后重新安装: ```bash rm -rf node_modules package-lock.json npm install ``` --- #### 3. 验证依赖兼容性 通过 `npm ls` 检查依赖树是否无冲突: ```bash npm ls vite @plugin-web-update-notification/vite ``` 若显示 `(version matches)` 或兼容版本号,则冲突已解决。 --- #### 4. 备选方案 若无法解决冲突,可尝试: - 使用替代插件(如 `vite-plugin-pwa` 内置更新通知功能) - 联系插件维护者提交版本兼容性 Issue --- ### 相关问题 1. 如何安全地升级项目的 Vite 大版本? 2. `peerDependencies` 和 `dependencies` 的区别是什么? 3. 如何批量检查项目中过期的 npm 依赖? [^1]: npm 官方文档关于 peerDependency 的说明 [^2]: Vite 插件开发最佳实践指南
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值