Socket
Socket
Sign inDemoInstall

workbox-strategies

Package Overview
Dependencies
1
Maintainers
6
Versions
82
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    workbox-strategies

A service worker helper library implementing common caching strategies.


Version published
Weekly downloads
4.4M
decreased by-0.33%
Maintainers
6
Created
Weekly downloads
 

Package description

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

Readme

Source

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

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