Security News
Cloudflare Adds Security.txt Setup Wizard
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
workbox-routing
Advanced tools
The workbox-routing package is part of the Workbox suite of libraries, which are designed to make it easier to build offline-first Progressive Web Apps (PWAs). The workbox-routing module provides tools to intercept network requests and determine how they should be handled, such as serving them from a cache or fetching a response from the network.
Registering a route
This code registers a route that intercepts requests for images and applies a cache-first strategy, meaning it will serve the images from the cache if available, otherwise it will fetch them from the network and cache them for future use.
workbox.routing.registerRoute(
({request}) => request.destination === 'image',
new workbox.strategies.CacheFirst()
);
Using a custom handler
This code registers a route for requests to an API endpoint. It uses a custom handler to fetch the request from the network and provides an offline fallback response in case the network request fails.
workbox.routing.registerRoute(
({url}) => url.pathname.startsWith('/api/'),
async ({event}) => {
try {
return await fetch(event.request);
} catch (error) {
return new Response('Offline fallback', { status: 200 });
}
}
);
Navigation route
This code registers a navigation route that serves an app shell HTML file for navigation requests, which is useful for single-page applications that want to ensure an app shell is returned for navigational requests.
workbox.routing.registerNavigationRoute(
workbox.precaching.getCacheKeyForURL('/app-shell.html')
);
sw-toolbox is a legacy library that was a precursor to Workbox. It offers similar functionalities for caching and serving content in service workers. However, Workbox has since superseded sw-toolbox with more features and improvements.
sw-precache is another legacy library that integrates with build processes to generate a service worker that precaches resources. It has been deprecated in favor of Workbox, which provides a more modular and flexible approach to service worker strategies.
A service worker helper library to route request URLs to handlers.
npm install --save-dev workbox-routing
Read more at this module's documentation page.
View the live example to see this module put to use.
This module is a part of Workbox, which is a collection of JavaScript libraries for Progressive Web Apps.
Visit https://workboxjs.org/ to learn more about what Workbox can do for you.
FAQs
A service worker helper library to route request URLs to handlers.
The npm package workbox-routing receives a total of 3,400,814 weekly downloads. As such, workbox-routing popularity was classified as popular.
We found that workbox-routing demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 6 open source maintainers collaborating on the project.
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.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.
Security News
ENISA’s 2024 report highlights the EU’s top cybersecurity threats, including rising DDoS attacks, ransomware, supply chain vulnerabilities, and weaponized AI.