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

@jsenv/service-worker

v1.0.2

Published

Service worker that can be integrated with jsenv build

Downloads

5

Readme

service-worker npm package

Short description of a service worker:

Using a service worker allows to put url(s) into the navigator cache. On the next visit navigator requests are served from cache. For every url in cache, navigator won't do a request to the network. If every url are in cache, website works offline.

Jsenv service worker:

  • Ensure cache is reused only when url are versioned
  • Can be connected to a build tool to know urls to put into navigator cache
  • Can be configured with a manual list of urls to cache

How to use

  1. Install @jsenv/service-worker
npm install @jsenv/worker
  1. Create service_worker.js
self.importScripts(
  "./node_modules/@jsenv/service-worker/src/jsenv_service_worker.js",
)

self.__sw__.init({
  name: "product-name",
  // service worker will cache "/" and the "roboto" font
  resources: {
    "/": {},
    "https://fonts.googleapis.com/css2?family=Roboto": {},
  },
})
  1. Register service worker
window.navigator.serviceWorker.register("./service_worker.js")

At this point your website will use jsenv service worker. By default jsenv service worker cache only the root url: "/". It must be configured to cache more urls.

Configuration

Jsenv service worker must be configured during "initJsenvServiceWorker" call.

Check directly src/jsenv_service_worker.js to see the available configuration and what it does.

Cache invalidation

When service worker updates it will refetch all url from network and put them into cache again. This is mandatory to check if ressource behind url has changed. When an url is versioned there is no need to refetch as it is assumed the ressource for that url will never change. If you know the url is versioned, tell it to the service worker as shown below.

self.__sw__.init({
  name: "product-name",
  resources: {
    "/": true,
-   "https://fonts.googleapis.com/css2?family=Roboto": {}
+   "https://fonts.googleapis.com/css2?family=Roboto": { version: '1' }
  },
})

Symbiosis with jsenv build

During build jsenv injects urls to cache at the top of service worker file(s) under a global variable: self.resourcesFromJsenvBuild.

self.importScripts(
  "./node_modules/@jsenv/service-worker/src/jsenv_service_worker.js",
)

self.__sw__.init({
  name: "product-name",
  resources: {
    "/": true,
    ...(self.resourcesFromJsenvBuild || {}),
  },
})

Jsenv url detection

Jsenv will detect all urls referenced in your files such as:

<link rel="preload" href="./src/img.png" as="image" />
body {
  background-image: url("./src/img.png");
}
new URL("./src/img.png", import.meta.url)

Urls detected during build will be in self.generatedUrlsConfig.

The urls with an origin different from your website origin will not. You must add them manually in "urlsConfig".

<link
  rel="preload"
  href="https://fonts.googleapis.com/css2?family=Roboto"
  as="font"
  crossorigin
/>
self.__sw__.init({
  cachePrefix: "product-name",
  resources: {
    "/": {},
+   "https://fonts.googleapis.com/css2?family=Roboto": {}
  },
})