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

vite-plugin-html-version

v0.0.7

Published

解决浏览器缓存问题。给html文件添加版本号,并生成version.txt文件。

Downloads

108

Readme

vite-plugin-html-version

A vite plugin to add version to html.

解决新版本发布后,用户浏览器缓存html导致页面不更新的问题。

Install

npm i vite-plugin-html-version -D

Usage

// vite.config.js
import { defineConfig } from 'vite'
import htmlVersion from 'vite-plugin-html-version'

export default defineConfig({
  plugins: [
    htmlVersion({
      version: '1.0.0',
    }),
  ],
})

Result

<!DOCTYPE html>
<html data-version="1667206656289" lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script type="module" src="/src/index.a3s5rdf2f3.js"></script>
  </head>
  <body>
    <div id="app"></div>
  </body>
</html>

Options

| Name | Type | Default | Description | | ----------- | ------------------- | ------------------- | ------------------------------ | | version | string |Date.now().toString()| version | | path | string | ./ | 存储版本信息的文件夹路径 | | filename | string | version.txt | 存储版本信息的文件名 | | fileContent | string | (version) => string | version| 存储版本信息的文件内容 | | attr | string | data-version | attribute to add version |

Demo

// vite.config.js
import { defineConfig } from 'vite'
import htmlVersion from 'vite-plugin-html-version'

export default defineConfig({
  plugins: [
    htmlVersion(),
  ],
})
  • 通过data-version属性,可以在页面中获取当前版本号。
  • 通过 fetch(/version.txt?_t=${Date.now}) 获取最新版本号。
  • 对比版本号,如果版本号不一致,即有新版本发布,刷新页面。
// 比较版本号
async function diffVersion() {
  const newVersion = await getVersion();
  const oldVersion = document.documentElement?.dataset?.version?.trim();
  // 有新版本
  if (newVersion !== oldVersion) return reload("检测到新版本,即将刷新页面");
}

// 获取最新版本号
function getVersion() {
  return fetch(`/version.txt?_t=${Date.now()}`).then((res) => {
    if (res.ok) {
      return res.body
        ?.getReader()
        .read()
        .then(({ value }) => {
          return new TextDecoder().decode(value);
        });
    } else {
      throw new Error("获取版本号失败");
    }
  });
}

自定义版本文件内容

// vite.config.js
import { defineConfig } from 'vite'
import htmlVersion from 'vite-plugin-html-version'

export default defineConfig({
  plugins: [
    htmlVersion({
      version: '1.0.0',
      path: './config', // 存储版本信息的文件夹路径,相对于部署后的根目录
      filename: 'version.json',
      fileContent: (version) => `{
        version: ${version},
        time: ${Date.now()},
        env: 'test',
        forceUpdate: true
      }`,
    }),
  ],
})
// 比较版本号
async function diffVersion() {
  const newVersion = await getVersion();
  const oldVersion = document.documentElement?.dataset?.version?.trim();
  // 有新版本
  if (newVersion !== oldVersion) return reload("检测到新版本,即将刷新页面");
}

// 获取最新版本号
function getVersion() {
  return fetch(`/config/version.json?_t=${Date.now()}`).then((res) => {
    if (res.ok) {
      return res.json().then((json) => {
        const oldVersion = document.documentElement?.dataset?.version?.trim();
        const newVersion = json.version;
        // 有新版本
        if (newVersion !== oldVersion) {
          // 强制更新
          if (json.forceUpdate) {
            return reload("检测到新版本,即将刷新页面");
          }
          // 非强制更新
          return newVersion;
        }
      });
    } else {
      throw new Error("获取版本号失败");
    }
  });
}