Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

workbox-precaching

Package Overview
Dependencies
Maintainers
6
Versions
96
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

workbox-precaching

This module efficiently precaches assets.

  • 7.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3.1M
decreased by-29.08%
Maintainers
6
Weekly downloads
 
Created

What is workbox-precaching?

The workbox-precaching package is part of the Workbox suite of tools, which are designed to make it easier to build progressive web apps (PWAs) and improve offline experiences. It provides a way to precache resources during the service worker installation phase, ensuring that they are available offline and can be served instantly on repeat visits.

What are workbox-precaching's main functionalities?

Precaching static resources

This feature allows you to specify a list of URLs with an associated revision detail to be precached when the service worker is installed. The revision helps in cache busting when the content of the files changes.

import { precacheAndRoute } from 'workbox-precaching';

precacheAndRoute([
  {url: '/index.html', revision: '1234567890'},
  {url: '/styles/main.css', revision: '1234567890'},
  {url: '/scripts/main.js', revision: '1234567890'}
]);

Integrating with a build process

This feature is used to integrate Workbox with your build process using workbox-build. It allows you to generate a service worker file that will precache the specified resources.

import { injectManifest } from 'workbox-build';

injectManifest({
  swSrc: 'src/sw.js',
  swDest: 'build/sw.js',
  globDirectory: 'build',
  globPatterns: ['**\/*.{html,js,css}'],
  maximumFileSizeToCacheInBytes: 4 * 1024 * 1024, // 4MB
}).then(({count, size, warnings}) => {
  // Optionally, log any warnings and details.
  console.log('Generated new service worker with', count, 'precached files, totaling', size, 'bytes.');
});

Updating precached assets

This code shows how you can manually add or update assets in the precache during the service worker's install event. This can be useful for custom handling of updates to precached assets.

self.addEventListener('install', (event) => {
  const urls = ['/index.html', '/styles/main.css'];
  const cacheName = workbox.core.cacheNames.precache;
  event.waitUntil(caches.open(cacheName).then((cache) => cache.addAll(urls)));
});

Other packages similar to workbox-precaching

Keywords

FAQs

Package last updated on 23 Apr 2024

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

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc