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

sw-register-webpack-plugin

v1.0.22

Published

a webpack plugin for serviceWorker register and serviceWorker no-cache soultion

Downloads

143

Readme

sw-register-webpack-plugin

npm version npm download Build Status

NPM

该插件解决 service-worker.js 文件 no-cache 问题,如果在服务端能做到对 service-worker.jsno-cache, 可以忽略此方案。 主要解决服务端不能对 service-worker.js 进行 no-cache 设置的问题,并且要求 service-worker.js 实时监测更新的场景。

Usage

安装

npm install sw-register-webpack-plugin --save-dev

在 webpack 插件里应用

// ...
import SwRegisterWebpackPlugin from 'sw-register-webpack-plugin';
// ...

webpack({
    // ...
    plugins: [
        new SwRegisterWebpackPlugin(/* options */);
    ]
    // ...
});

// ...

Options 参数

version

{
    version: 'this is a version string'
}

version 默认值是当前时间的时间版本字符串

filePath

{
    filePath: './src/sw-register.js'
}

filePath 默认值是 ./src/sw-register.js 如果没有配置这个字段,插件会优先寻找 ./src/sw-register.js 文件 如果还是没找到这个文件,插件会使用内置的 sw-resgiter.js 文件进行 service worker 文件注册。

prefix

{
    prefix: '/some_scope'
}

includes

{
    includes: [
        'a.html.tpl', // 文件名为 a.html.tpl 的文件,
        /b\.html\.tpl$/, //也可以是正则
        // 也可以是一个 callback function, 参数为待验证的每一个文件的 path
        function (asset) {
            return asset.endsWith('c.html.tpl')
        }
    ]
}

includes 是指定工程中非 html 文件也需要注册 sw 的,通常为模版文件。 需要注意的是,如果要指定非 html 文件也自动注册 sw, 该文件内容必须包含 <body></body> 标签

excludes

{
    excludes: [
        'a.html', // 文件名为 a.html 的文件,
        /b\.html$/, //也可以是正则
        // 也可以是一个 callback function, 参数为待验证的每一个文件的 path
        function (asset) {
            return asset.endsWith('c.html')
        }
    ]
}

excludes 是指定工程中符合自动注册的文件中的某一些文件可以不自动注册。

插件会默认使用 webpack output 中的 publicPath 来做为 service-worker.js 的 scope, 如果不想使用这个 scope, 可以通过 prefix 指定自定义的 scope。

output

{
    output: '/module/sw-register.js'
}

更改输出的位置,建议和prefix搭配使用。

注意事项

sw-register-webpack-plugin 有一个内置的 message 时间处理函数,其作用是专门接受 service-worker.js 发送的 postMessage 事件 我们会借助这个事件完成 service-worker.js 文件更新的实时监测。

service-worker.js 进入 activate 状态,可以做如下操作:

clients[0].postMessage('sw.update')

service-worker.js 需要保证能够被访问到