Skip to content

Commit 60c3260

Browse files
author
patched.codes[bot]
committed
Patched /private/var/folders/19/08z9_xys69v3f1xfqtyn9jyw0000gn/T/tmp9nchm_3p/README.md
1 parent e24fb58 commit 60c3260

File tree

1 file changed

+100
-10
lines changed

1 file changed

+100
-10
lines changed

README.md

Lines changed: 100 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,104 @@
1-
# [:] Example Python 2 Project
1+
# File: sw.js
22

3-
An example Python project to demonstrate [srcclr](https://www.srcclr.com) scans.
3+
## Overview
44

5-
Check out the [python3](https://github.com/srcclr/example-python/tree/python3) branch for a project that builds with Python 3.
5+
This JavaScript file is a Service Worker (SW) script that provides caching strategies for CSS, JS, fonts, images, and videos. The caching is managed using the Workbox library, which is conditionally loaded either from a CDN or locally based on the current location URL.
66

7-
## Try me!
7+
## Inputs
88

9-
```
10-
brew tap srcclr/srcclr
11-
brew install srcclr
12-
srcclr activate
13-
srcclr scan --url https://github.com/srcclr/example-python
14-
```
9+
- **location.href**: Determines whether to load Workbox from a CDN or locally.
10+
- **Workbox**: A library for caching resources and managing service workers.
11+
- **Cache configuration objects**: Define strategies for caching specific types of requests.
12+
13+
## Outputs
14+
15+
- **Service Worker registration**: The script installs and activates a service worker that caches resources according to predefined strategies.
16+
17+
## Usage
18+
19+
### Functionality
20+
21+
1. **Conditional Workbox Import**:
22+
- The script checks if the current page URL includes 'howdz.xyz'. If so, it loads Workbox from a CDN. Otherwise, it loads it locally.
23+
24+
2. **Caching Strategies**:
25+
- **CSS/JS/Font Caching**: Uses `CacheFirst` strategy to cache these assets, with a maximum of 50 entries, each valid for 7 days.
26+
- **Image Caching**: Uses `StaleWhileRevalidate` strategy, allowing immediate response while updating the cache in the background, also with a maximum of 50 entries, each valid for 7 days.
27+
- **Video Caching**: Uses `CacheFirst` strategy with range requests support, allowing partial content caching for better video streaming performance.
28+
29+
### Key Components
30+
31+
- **Conditionally Importing Workbox**:
32+
```javascript
33+
if (location.href.includes('howdz.xyz')) {
34+
importScripts('https://cdn.staticfile.org/workbox-sw/7.0.0/workbox-sw.js')
35+
workbox.setConfig({
36+
debug: false,
37+
});
38+
console.log('sw.js is load by CDN!')
39+
} else {
40+
importScripts('./workbox/workbox-sw.js')
41+
workbox.setConfig({
42+
debug: false,
43+
modulePathPrefix: './workbox/'
44+
});
45+
console.log('sw.js is load by local!')
46+
}
47+
```
48+
49+
- **Registering Routes for Caching**:
50+
```javascript
51+
// Cache css/js/font.
52+
workbox.routing.registerRoute(
53+
({ request }) => request.destination === 'style' || request.destination === 'script' || request.destination === 'font',
54+
new workbox.strategies.CacheFirst({
55+
cacheName: 'css-js-font',
56+
plugins: [
57+
new workbox.cacheableResponse.CacheableResponsePlugin({
58+
statuses: [200],
59+
}),
60+
new workbox.expiration.ExpirationPlugin({
61+
maxEntries: 50,
62+
maxAgeSeconds: 60 * 60 * 24 * 7, // 7 Days
63+
}),
64+
]
65+
})
66+
);
67+
68+
// Cache image.
69+
workbox.routing.registerRoute(
70+
({ request }) => request.destination === 'image',
71+
new workbox.strategies.StaleWhileRevalidate({
72+
cacheName: 'image',
73+
plugins: [
74+
new workbox.cacheableResponse.CacheableResponsePlugin({
75+
statuses: [200],
76+
}),
77+
new workbox.expiration.ExpirationPlugin({
78+
maxEntries: 50,
79+
maxAgeSeconds: 60 * 60 * 24 * 7, // 7 Days
80+
})
81+
]
82+
})
83+
);
84+
85+
// Cache video
86+
workbox.routing.registerRoute(
87+
({ request }) => request.destination === 'video',
88+
new workbox.strategies.CacheFirst({
89+
cacheName: 'video',
90+
plugins: [
91+
new workbox.cacheableResponse.CacheableResponsePlugin({
92+
statuses: [200],
93+
}),
94+
new workbox.expiration.ExpirationPlugin({
95+
maxEntries: 50,
96+
maxAgeSeconds: 60 * 60 * 24 * 7, // 7 Days
97+
}),
98+
new workbox.rangeRequests.RangeRequestsPlugin()
99+
]
100+
})
101+
);
102+
```
103+
104+
By using this `sw.js` script, a web application can effectively cache its resources, resulting in better performance and offline capabilities. This is particularly useful for Progressive Web Apps (PWAs) that aim to provide a seamless user experience even in intermittent network conditions.

0 commit comments

Comments
 (0)