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

cache-handler

v0.8.3

Published

Library that will take care of cache handling for any rich enterprise application

Downloads

184

Readme

Introduction

Library that will take care of cache handling for any rich enterprise application. The purpose of this library is to forcefully treat a new deployment of a web application as if it were the first time that the user has ever visited the site. The overall goal is to have as few steps as possible to reload everything. All the forced refresh/cache clearing stuff should happen only 1 time for any given application deployment. And this needs to be reloaded on user confirmation even if the user is in the middle of doing something as the application is a single page rich application. The user should not continually experience this behaviour on a periodic basis. Only when a new version of the application

Service worker introduction

At its simplest, a service worker is a script that runs in the web browser and manages caching for an application. Service workers function as a network proxy. They intercept all outgoing HTTP requests made by the application and can choose how to respond to them. For example, they can query a local cache and deliver a cached response if one is available. Proxying isn't limited to requests made through programmatic APIs, such as fetch; it also includes resources referenced in HTML and even the initial request to index.html. Service worker-based caching is thus completely programmable and doesn't rely on server-specified caching headers. The Service worker's behaviour follows that design goal:

� Caching an application is like installing a native application. The application is cached as one unit, and all files update together.

� A running application continues to run with the same version of all files. It does not suddenly start receiving cached files from a newer version, which are likely incompatible.

� When users refresh the application, they see the latest fully cached version. New tabs load the latest cached code.

� Updates happen in the background, relatively quickly after changes are published. The previous version of the application is served until an update is installed and ready.

� The service worker conserves bandwidth when possible. Resources are only downloaded if they've changed.

To support these behaviours, the Angular service worker loads a manifest file (ngsw-config.json) from the server. The manifest describes the resources to cache and includes hashes of every file's contents. When an update to the application is deployed, the contents of the manifest change, informing the service worker that a new version of the application should be downloaded and cached.

Getting started with cache handler

To set up the Service worker in your project, use the below CLI command. It takes care of configuring your app to use service workers by adding the service-worker package along with setting up the necessary support files.

npm config set project project-name

ng add cache-handler --project project-name

Now, build the project:

ng build --prod

To serve the directory containing your web files with http-server, run the following command

http-server -p 8080 -c-1 dist/

Initial load

With the server running, you can point your browser at http://localhost:8080/. Your application should load normally.

What's being cached?

Notice that all of the files the browser needs to render this application are cached. The ngsw-config.json boilerplate configuration is set up to cache the specific resources:

� index.html.

� favicon.ico.

� Build artifacts (JS and CSS bundles).

� Anything under assets.

� Images and fonts directly under the configured outputPath (by default ./dist//)

You can modify the default configuration to your need and add Rest API URLs as well.

Available and activated updates

Export Class CacheService {

�����..

updates.available.subscribe(event => { console.log('current version is', event.current); console.log('available version is', event.available); }); updates.activated.subscribe(event => { console.log('old version was', event.previous); console.log('new version is', event.current); });

������

}

Checking for updates

Export Class CacheService {

�����..

const appIsStable$ = appRef.isStable.pipe(first(isStable => isStable === true)); const everySixHours$ = interval(6 * 60 * 60 * 1000); const everySixHoursOnceAppIsStable$ = concat(appIsStable$, everySixHours$); everySixHoursOnceAppIsStable$.subscribe(() => updates.checkForUpdate());

������

}

Forcing update activation

Export Class CacheService {

�����..

updates.available.subscribe(event => { if (promptUser(event)) { updates.activateUpdate().then(() => document.location.reload()); } });

������

}