Skip to content

Instantly share code, notes, and snippets.

View jonchenn's full-sized avatar

Jonathan Chen jonchenn

View GitHub Profile
self.addEventListener('fetch', async (event) => {
if (event.request.method === 'POST') {
// Respond with cached data and update from network in the background.
event.respondWith(staleWhileRevalidate(event));
}
});
{
restaurants(type: French, stars: 3) {
name
type
map
stars
}
}
// Workbox with a custom handler to use IndexedDB for caching.
workbox.routing.registerRoute(
'/graphql',
async ({event}) => staleWhileRevalidate(event),
'POST'
);
data = await idbKeyval.get(id, store);
if (!data) return null;
// Check cache max age.
let cacheControl = request.headers.get('Cache-Control');
let maxAge = cacheControl ? parseInt(cacheControl.split('=')[1]) : 3600;
if (Date.now() - data.timestamp > maxAge * 1000) {
return null; // Invalidate the cache data.
}
fetch(url, {
method: 'POST',
headers: {
'AMP-Same-Origin': 'true',
'Accept': 'application/json',
'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundaryzCA5AbNQoBPgNVsS',
'Origin': 'http://qa.bosley.com',
'Referer': 'http://qa.bosley.com/lpamp/gbh1/?amp',
'User-Agent': 'Mozilla/5.0 (Linux; Android 8.0; Pixel 2 Build/OPD3.170816.012) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Mobile Safari/537.36}',
},
fetch(url, {
method: 'POST',
headers: {
'AMP-Same-Origin': 'true',
'Accept': 'application/json',
'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundaryzCA5AbNQoBPgNVsS',
'Origin': 'http://qa.bosley.com',
'Referer': 'http://qa.bosley.com/lpamp/gbh1/?amp',
'User-Agent': 'Mozilla/5.0 (Linux; Android 8.0; Pixel 2 Build/OPD3.170816.012) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Mobile Safari/537.36}',
},