/fetch.js Secret
Last active
August 17, 2018 21:43
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
function getProductData (productId) { | |
let productCount = 0 | |
indexedDB.open('store', 1).then ( (db) => { | |
let transaction = db.transaction(['products']) | |
let productData = transaction.ObjectStore('products').get(productId).then (item => productCount = item.count) | |
}) | |
return productCount | |
} | |
self.addEventListener('fetch', (event) => { | |
event.waitUnitil( | |
if (event.request.url.includes('/product') { | |
let productId = event.data.productId | |
let productCount = getProductData(productId) | |
indexedDB.open('store', 1, (db) => { | |
let productStore = db.createObjectStore('products', { keyPath: 'id' }) | |
productStore.put({ id: productId, count: ++productCount }) | |
}) | |
} | |
) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 12 is invalid.