Created
July 10, 2017 12:44
-
-
Save ptiswitz/ef40a69eb8b3d730f7d9853c77fd6a24 to your computer and use it in GitHub Desktop.
sw-toolbox issue
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
((global) => { | |
let database; | |
global.addEventListener('activate', () => { | |
database = new ydn.db.Storage('webfolio', { | |
stores: [{ | |
name: 'building-blocks', | |
keyPath: 'id', | |
indexes: [{ | |
name: 'eventId', | |
multiEntry: true | |
}, { | |
name: 'definitionId', | |
multiEntry: true | |
}] | |
}] | |
}); | |
}); | |
global.addEventListener('message', (event) => { | |
global | |
.processMessage(event.data) | |
.then(function (data) { | |
global.sendMessage(event.ports[0], { | |
status: 'success', | |
response: data | |
}); | |
}, function (err) { | |
global.sendMessage(event.ports[0], { | |
status: 'failed', | |
response: err | |
}); | |
}); | |
}); | |
global.sendMessage = (sw, message) => { | |
if (sw) { | |
sw.postMessage(message); | |
} else { | |
console.error('Unable to send message to client'); | |
} | |
}; | |
global.processMessage = (data) => { | |
console.log(data); | |
return new Promise((resolve, reject) => { | |
if (data.command === 'event::download') { | |
global | |
.downloadEvent(parseInt(data.message, 10)) | |
.then(() => { | |
resolve() | |
}, (err) => { | |
reject(err) | |
}); | |
} else if (data.command === 'event::upload') { | |
global | |
.uploadEvent(data.message) | |
.then(() => { | |
resolve() | |
}, (err) => { | |
reject() | |
}); | |
} else if (data.command === 'event::is-downloaded') { | |
global | |
.isEventDownloaded(data.message) | |
.then((result) => { | |
resolve(result); | |
}, (err) => { | |
reject(err); | |
}); | |
} else { | |
resolve('pong'); | |
} | |
}); | |
}; | |
global.downloadEvent = (eventId) => { | |
let urlsToCache = [ | |
'/rest/events/' + eventId, | |
'/rest/events/' + eventId + '/building-blocks', | |
'/rest/events/' + eventId + '/attendees', | |
'/rest/events/' + eventId + '/tasks', | |
'/rest/tasks/statuses', | |
'/rest/tasks/priorities' | |
]; | |
return new Promise((resolve, reject) => { | |
global | |
.cacheUrls(urlsToCache) | |
.then((urls) => { | |
global | |
.fetchUrls(urls, eventId) | |
.then(function () { | |
resolve(); | |
}, function (err) { | |
reject(err); | |
}); | |
}, (err) => { | |
reject(err); | |
}) | |
}); | |
}; | |
global.fetchUrls = (urls, eventId) => { | |
return new Promise((resolve, reject) => { | |
Promise | |
.all( | |
urls.map(url => { | |
fetch(url) | |
.then(response => { | |
global.processResponse(response, eventId); | |
}) | |
}) | |
) | |
.then(() => { | |
resolve(urls); | |
}, () => { | |
reject('Unable to fetch urls') | |
}); | |
}); | |
}; | |
global.processResponse = (response, eventId) => { | |
return new Promise((resolve, reject) => { | |
if (response.url.match(/building-blocks$/) !== null && response.status === 200) { | |
response | |
.json() | |
.then((blocks) => { | |
Promise | |
.all( | |
blocks.map((block) => { | |
global.storeBuildingBlock(block, eventId) | |
}) | |
) | |
.then(function () { | |
resolve(); | |
}, function (err) { | |
reject(err); | |
}); | |
}, () => { | |
reject('Unable to retrieve json'); | |
}) | |
} else { | |
resolve(); | |
} | |
}); | |
}; | |
global.storeBuildingBlock = (block, eventId) => { | |
return new Promise((resolve, reject) => { | |
database | |
.put('building-blocks', Object.assign({}, block, { | |
eventId: eventId | |
})) | |
.done(() => { | |
resolve(); | |
}) | |
.fail(function (err) { | |
reject(err); | |
}); | |
}); | |
}; | |
global.cacheUrls = (urls) => { | |
return new Promise((resolve, reject) => { | |
Promise | |
.all( | |
urls.map(url => { | |
global.toolbox.cache(url) | |
}) | |
) | |
.then(function () { | |
resolve(urls); | |
}, function () { | |
console.log('HIT'); | |
reject('Unable to cache urls'); | |
}) | |
}); | |
} | |
global.uploadEvent = (event) => { | |
let urlsToFlush = [ | |
'/rest/events/' + event.id, | |
'/rest/events/' + event.id + '/building-blocks', | |
'/rest/events/' + event.id + '/attendees', | |
'/rest/events/' + event.id + '/tasks', | |
'/rest/tasks/statuses', | |
'/rest/tasks/priorities' | |
]; | |
return new Promise((resolve, reject) => { | |
global | |
.uncacheUrls(urlsToFlush) | |
.then(() => { | |
global | |
.deleteBuildingBlocks(event.id) | |
.then(() => { | |
resolve(); | |
}, (err) => { | |
reject(err); | |
}); | |
}, (err) => { | |
reject(err); | |
}); | |
}); | |
}; | |
global.deleteBuildingBlocks = (eventId) => { | |
return new Promise((resolve, reject) => { | |
database | |
.remove('building-blocks', 'eventId', ydn.db.KeyRange.only(eventId)) | |
.done(() => { | |
resolve(); | |
}) | |
.fail((err) => { | |
reject(err); | |
}); | |
}); | |
}; | |
global.uncacheUrls = (urls) => { | |
return new Promise((resolve, reject) => { | |
Promise | |
.all( | |
urls.map(url => { | |
global.toolbox.uncache(url) | |
}) | |
) | |
.then(() => { | |
resolve(); | |
}, () => { | |
reject('Unable to uncache urls'); | |
}) | |
}); | |
}; | |
global.isEventDownloaded = (eventId) => { | |
console.log(global.toolbox.options.cache.name); | |
return new Promise((resolve, reject) => { | |
caches | |
.open(global.toolbox.options.cache.name) | |
.then((cache) => { | |
cache | |
.keys() | |
.then((requests) => { | |
let results = requests.filter(req => { | |
console.log(req.url); | |
req.url.match('events/' + eventId) | |
}); | |
resolve(results.length > 0); | |
}, () => { | |
reject('Unable to get cache keys'); | |
}); | |
}, () => { | |
reject('Unable to open cache'); | |
}); | |
}); | |
}; | |
})(self) |
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
const gulp = require('gulp'); | |
const swPrecache = require('sw-precache'); | |
const config = require('../../config'); | |
gulp.task('service-worker', function (cb) { | |
swPrecache | |
.write(config.path.build('/sw.js'), { | |
staticFileGlobs: [ | |
config.path.build('index.html'), | |
config.path.build('assets/svg/logo.svg'), | |
config.path.build('assets/images/logo.png'), | |
config.path.build('fonts/**/*'), | |
config.path.build('scripts/*.js'), | |
config.path.build('styles/*.css') | |
], | |
stripPrefix: config.paths.build, | |
cacheId: 'webfolio', | |
importScripts: [ | |
'sw/ydn.db.js', | |
'sw/sw-toolbox.js', | |
'sw/toolbox-scripts.js', | |
'sw/events-caching-policy.js' | |
], | |
maximumFileSizeToCacheInBytes: 4194304, | |
verbose: true, | |
ignoreUrlParametersMatching: [/./] | |
}, cb); | |
}); |
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
((global) => { | |
global.toolbox.options.debug = true; | |
global.toolbox.options.cache.name = 'webfolio'; | |
global.toolbox.precache([ | |
'index.html', | |
'assets/svg/logo.svg', | |
'assets/images/logo.png', | |
'scripts/app.js', | |
'scripts/vendors.js', | |
'styles/app.css', | |
'styles/vendors.css' | |
]); | |
global.toolbox.router.get('index.html', global.toolbox.cacheFirst); | |
global.toolbox.router.get('assets/(svg|images)/logo.{png,svg}', global.toolbox.cacheFirst); | |
global.toolbox.router.get('(scripts|styles)/*.{css,js}', global.toolbox.cacheFirst); | |
global.toolbox.router.get('fonts/(.*)', global.toolbox.cacheFirst); | |
global.toolbox.router.default = global.toolbox.networkOnly; | |
})(self); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment