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

workbox-strategies

Package Overview
Dependencies
Maintainers
6
Versions
83
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

workbox-strategies

A service worker helper library implementing common caching strategies.

  • 6.2.4
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4.4M
increased by3.55%
Maintainers
6
Weekly downloads
 
Created

What is workbox-strategies?

The workbox-strategies package is part of the Workbox suite of libraries, which are designed to make it easier to build offline-first, service worker-powered web applications. This package provides various strategies for caching and retrieving resources, allowing developers to control how network requests are handled by the service worker.

What are workbox-strategies's main functionalities?

Cache First Strategy

The Cache First strategy checks the cache for a response before trying to get one from the network. It's useful for assets that don't change often.

import { CacheFirst } from 'workbox-strategies';

const cacheFirst = new CacheFirst();
workbox.routing.registerRoute(
  ({request}) => request.destination === 'image',
  cacheFirst
);

Network First Strategy

The Network First strategy tries to get a response from the network first, falling back to the cache if the network is unavailable. This is good for content that updates frequently.

import { NetworkFirst } from 'workbox-strategies';

const networkFirst = new NetworkFirst();
workbox.routing.registerRoute(
  ({request}) => request.destination === 'document',
  networkFirst
);

Stale While Revalidate Strategy

The Stale While Revalidate strategy serves a response from the cache, while simultaneously fetching an updated version from the network for next time. It's a good compromise for resources where freshness is not critical.

import { StaleWhileRevalidate } from 'workbox-strategies';

const staleWhileRevalidate = new StaleWhileRevalidate();
workbox.routing.registerRoute(
  ({request}) => request.destination === 'script',
  staleWhileRevalidate
);

Network Only Strategy

The Network Only strategy forces the response to be retrieved from the network, without any caching involved. This is useful for requests that need to be fresh and aren't suitable for caching.

import { NetworkOnly } from 'workbox-strategies';

const networkOnly = new NetworkOnly();
workbox.routing.registerRoute(
  ({request}) => request.destination === 'script',
  networkOnly
);

Cache Only Strategy

The Cache Only strategy forces the response to be retrieved from the cache, without going to the network. This is useful for assets that have been precached and are known to be available offline.

import { CacheOnly } from 'workbox-strategies';

const cacheOnly = new CacheOnly();
workbox.routing.registerRoute(
  ({request}) => request.destination === 'style',
  cacheOnly
);

Other packages similar to workbox-strategies

Keywords

FAQs

Package last updated on 11 Aug 2021

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