You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

sw-precache

Package Overview
Dependencies
Maintainers
4
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sw-precache

Generates a service worker to cache your local App Shell resources.

5.2.1
latest
Source
npmnpm
Version published
Weekly downloads
179K
53.53%
Maintainers
4
Weekly downloads
 
Created

What is sw-precache?

The sw-precache npm package is a tool for generating a service worker that precaches resources to enable offline-first web applications. It automates the creation of a service worker script that caches specified resources, allowing web applications to function offline or with unreliable network connections.

What are sw-precache's main functionalities?

Precaching Resources

This feature allows you to specify which resources should be precached by the service worker. The code sample demonstrates how to use sw-precache to generate a service worker that caches all JavaScript, HTML, CSS, and image files in the 'public' directory.

const swPrecache = require('sw-precache');
swPrecache.write('service-worker.js', {
  staticFileGlobs: ['public/**/*.{js,html,css,png,jpg,gif}'],
  stripPrefix: 'public/'
});

Runtime Caching

This feature allows you to define runtime caching strategies for specific URL patterns. The code sample shows how to configure the service worker to use a 'networkFirst' strategy for requests to URLs that match the '/api/' pattern.

const swPrecache = require('sw-precache');
swPrecache.write('service-worker.js', {
  runtimeCaching: [{
    urlPattern: /api/,
    handler: 'networkFirst'
  }]
});

Custom Service Worker Logic

This feature allows you to extend the generated service worker with custom logic by importing additional scripts. The code sample demonstrates how to include a custom script named 'custom-service-worker.js' in the generated service worker.

const swPrecache = require('sw-precache');
swPrecache.write('service-worker.js', {
  importScripts: ['custom-service-worker.js']
});

Other packages similar to sw-precache

Keywords

caching

FAQs

Package last updated on 24 Jan 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts