Socket
Socket
Sign inDemoInstall

workbox-build

Package Overview
Dependencies
350
Maintainers
7
Versions
99
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    workbox-build

A module that integrates into your build process, helping you generate a manifest of local files that workbox-sw should precache.


Version published
Maintainers
7
Install size
38.7 MB
Created

Package description

What is workbox-build?

The workbox-build npm package is a module that integrates with your build process, allowing you to generate service workers and manage assets for offline use in web applications. It provides a set of tools to precache assets, implement efficient caching strategies, and ensure your web app can work offline or on poor network conditions.

What are workbox-build's main functionalities?

Generating a Service Worker

This feature allows you to generate a service worker script automatically. The script will precache specified assets, enabling your application to load faster and work offline. The example demonstrates generating a service worker for a project where assets are located in the 'dist' directory.

const workboxBuild = require('workbox-build');

workboxBuild.generateSW({
  swDest: 'service-worker.js',
  globDirectory: 'dist',
  globPatterns: ['**\/*.{html,js,css}'],
  skipWaiting: true,
  clientsClaim: true
}).then(({count, size}) => {
  console.log(`Generated a service worker, which will precache ${count} files, totaling ${size} bytes.`);
});

Injecting a Manifest into an Existing Service Worker

This feature is useful when you already have a service worker and want to inject a precache manifest into it. The manifest will include a list of assets to be precached, based on the patterns provided. The example shows how to inject a manifest into an existing service worker.

const workboxBuild = require('workbox-build');

workboxBuild.injectManifest({
  swSrc: 'src/service-worker.js',
  swDest: 'service-worker.js',
  globDirectory: 'dist',
  globPatterns: ['**\/*.{html,js,css,png}']
}).then(({count, size}) => {
  console.log(`Injected a manifest into the service worker, adding ${count} files, totaling ${size} bytes.`);
});

Customizing Caching Strategies

Workbox-build allows for the customization of caching strategies for different types of requests. This example demonstrates how to set up caching strategies for CSS and JavaScript files using Workbox's routing and strategies modules.

// This example assumes you are writing inside a service worker file

importScripts('https://storage.googleapis.com/workbox-cdn/releases/5.1.2/workbox-sw.js');

workbox.routing.registerRoute(
  /\.css$/,
  new workbox.strategies.StaleWhileRevalidate({
    cacheName: 'css-cache',
  })
);

workbox.routing.registerRoute(
  /\.js$/,
  new workbox.strategies.NetworkFirst({
    cacheName: 'js-cache',
  })
);

Other packages similar to workbox-build

Readme

Source

This module's documentation can be found at https://developers.google.com/web/tools/workbox/modules/workbox-build

Keywords

FAQs

Last updated on 23 Apr 2024

Did you know?

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc