This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function connectionAwareActivity(WrappedComponent) { | |
return class extends PureComponent { | |
constructor(props) { | |
super(props); | |
this.state = { isConnected: false }; | |
this.switchConnectivity = this.switchConnectivity.bind(this); | |
} | |
static navigationOptions = WrappedComponent.navigationOptions | |
componentDidMount() { | |
NetInfo.addEventListener('connectionChange', this.switchConnectivity); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
self.addEventListener('install', function (event) { | |
event.waitUntil( | |
caches.open('your cache name').then(function (cache) { | |
return cache.addAll([ | |
'/some-route', | |
'js/my-app.js', | |
'css/my-app.css', | |
'imgs/my-app.png', | |
'https://fonts.gstatic.com/s/roboto/v15/2UX7WLTfW3W8TclTUvlFyQ.woff', | |
'https://fonts.gstatic.com/s/roboto/v15/d-6IYplOFocCacKzxwXSOD8E0i7KZn-EPnyo3HZu7kw.woff', |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
purpose: | |
example is for a music player app, where we want to store | |
selective playlists for offline usage | |
on user click to `Save Offline` button, we want to add all | |
songs in playlist to the cache storage | |
*/ | |
document.getElementBy('saveOfflineBtn').addEventListener('click', function(event) { | |
event.preventDefault(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
purpose: | |
for all .png image requests, | |
- look into cache if request-response Pair is already in store | |
- if found, return the object in cache and refresh cache from network | |
- if not found, fetch from network, store in cache and return the response | |
*/ | |
self.addEventListener('fetch', function (event) { | |
const requestUrl = new URL(event.request.url); | |
if(requestUrl.endsWith('.png'){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
caches.open('my-cache-category-name') | |
.then(cache => { | |
/** | |
`cache` is the store of my-cache-category-name cache | |
if it does not exists, it is created and a reference is give here | |
if it exists already, reference to that cache is provided | |
*/ | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
caches.keys() | |
.then(keysArray => { | |
console.log(keysArray) | |
/** | |
["category-1-cache", "some-other-cache", "static-assests-cache", "could be anything"] | |
*/ | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Suppose CacheStorage these named cache - "name 1", "Some-other-name" | |
*/ | |
console.log( | |
await caches.has("name 1") | |
)// <- true | |
console.log( | |
await caches.has("name 2") | |
)// <- false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Suppose, origin has caches named "wittr-static-v8" and "wittr-content-imgs" | |
"wittr-content-imgs" contains a request response pair for craig.jpg, | |
with request url as key i.e. "avatars/craig" | |
*/ | |
console.log( | |
await caches.match("avatars/craig") | |
)//👇 | |
/** | |
body: (ReadableStream) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Suppose, origin has caches named "wittr-static-v8" and "wittr-content-imgs" | |
*/ | |
console.log( | |
caches.delete("wittr-static-v8") | |
)// <- true | |
console.log( | |
caches.delete("not existing cache") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
suppose, static caches are categorized as static-imgs-v1, static-js-v1... | |
*/ | |
const version = 'v2'; | |
const staticPrefix = 'static-' | |
caches.keys().then(function (cacheNames) { | |
return Promise.all( | |
cacheNames.filter(function (cacheName) { | |
return cacheName.startsWith(staticPrefix) && | |
!cacheName.endsWith(version); |
OlderNewer