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 🙏

© 2024 – Pkg Stats / Ryan Hefner

unplugin-monitor-update

v0.1.2

Published

[English](./README.md) | 简体中文

Downloads

1

Readme

English | 简体中文

unplugin-monitor-update

监控网页更新并通知用户重新加载页面

以 git commit hash、 package.json version、build timestamp、custom version 为版本号,编译时将版本号写入 json 文件。客户端轮询监测服务器上的版本号,或者是浏览器窗口的 visibilitychange、focus 事件等。如果不相同则通知用户重新加载页面。

什么时候会检测更新(fetch version.json) ?

  1. 首次加载页面。
  2. 轮询 (default: 10 * 60 * 1000 ms)。
  3. script 脚本资源加载失败 (404 ?)。
  4. 标签页 refocus or revisible。

安装

# npm
npm i unplugin-monitor-update -D

# pnpm
pnpm add unplugin-monitor-update -D

# yarn
yarn add unplugin-monitor-update -D
// vite.config.ts
import unpluginMonitorUpdate from 'unplugin-monitor-update/vite'

export default defineConfig({
  plugins: [
    unpluginMonitorUpdate({ /* options */ }),
  ],
})

Example: playground/

// rollup.config.js
import unpluginMonitorUpdate from 'unplugin-monitor-update/rollup'

export default {
  plugins: [
    unpluginMonitorUpdate({ /* options */ }),
  ],
}
// webpack.config.js
module.exports = {
  /* ... */
  plugins: [
    require('unplugin-monitor-update/webpack')({ /* options */ })
  ]
}
// nuxt.config.js
export default defineNuxtConfig({
  modules: [
    ['unplugin-monitor-update/nuxt', { /* options */ }],
  ],
})

This module works for both Nuxt 2 and Nuxt Vite

// vue.config.js
module.exports = {
  configureWebpack: {
    plugins: [
      require('unplugin-monitor-update/webpack')({ /* options */ }),
    ],
  },
}
// esbuild.config.js
import { build } from 'esbuild'
import unpluginMonitorUpdate from 'unplugin-monitor-update/esbuild'

build({
  plugins: [unpluginMonitorUpdate()],
})

关键:禁用 index.html 缓存!!!

如果 index.html 存在缓存,可能刷新后,更新提示还会存在,所以需要禁用 index.html 的缓存。这也是 SPA 应用部署的一个最佳实践吧。

通过 nginx ,禁用缓存:

# nginx.conf
location / {
  index index.html index.htm;

  if ( $uri = '/index.html' ) { # disabled index.html cache
    add_header Cache-Control "no-cache, no-store, must-revalidate";
  }

  try_files $uri $uri/ /index.html;
}

直接通过 html meta 标签禁用缓存:

<!DOCTYPE html>
<html lang="en">
<head>

  <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
  <meta http-equiv="Pragma" content="no-cache" />
  <meta http-equiv="Expires" content="0" />

</head>
</html>

监听通知事件

// 监听更新事件
document.body.addEventListener('unplugin_monitor_update_event', (e) => {
  const { version, options } = e.detail
  // write some code, show your custom notification and etc.
  console.log('System needs to be updated!')
})

Options

export interface Options {
  /**
   * The type of generated version
   * * commit_hash: git commit hash
   * * package: package.json version
   * * timestamp: timestamp
   * * custom: custom version
   * */
  versionType?: VersionType
  /**
   * custom version, if versionType is 'custom', this option is required
   */
  customVersion?: string;
  /**
   * Monitoring modalities
   * * polling: Polling monitoring
   * * windowFocus: Monitor when window is focused
   * * immediately: Monitoring immediately after page load
   * * loadScriptError: Page script load failure(404)
   * * windowVisibility: window visible
   */
  monitorMode?: MonitorMode | MonitorMode[]

  /**
   * Monitoring intervals
   * @default 10 * 60 * 1000
   */
  checkInterval?: number
  /**
   * 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)
  /**
   *
   * 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
  /**
   * extra data in version.json
   *
   * @default {}
   */
  extra?: Record<string, any>

}

export type VersionType = 'commit_hash' | 'package' | 'timestamp' | 'custom'

export type MonitorMode = 'polling' | 'windowFocus' | 'immediate' | 'loadScriptError' | 'windowVisibility'

方法

| name | params | describe | | ----------------------------------------------- | ----------------------------------- | ------------------------------------------------------------ | | window.monitorSystemUpdate | | manual check update, a function wrap by debounce(10 * 60 * 1000ms) |

interface Window {
  /** version number */
  __UNPLUGIN_MONITOR_UPDATE_VERSION__: string
  /**
   * don't call this function in manual。
   */
  setupMonitorPlugin: (options: Options) => void
  monitorSystemUpdate: () => void

}

interface GlobalEventHandlersEventMap {
  unplugin_monitor_update_notice: CustomEvent<{ version: string; options: Options }>;
}

License

MIT