Skip to content

Instantly share code, notes, and snippets.

@GlauberF
Created February 4, 2019 11:12
Show Gist options
  • Save GlauberF/8524b1bb06d98793de0f7a0ef764561c to your computer and use it in GitHub Desktop.
Save GlauberF/8524b1bb06d98793de0f7a0ef764561c to your computer and use it in GitHub Desktop.
// cache storage API
workbox.routing.registerRoute(
// new RegExp('https://api-production.vimbo.com.br/storage/empresas/*.*'),
new RegExp('https://api-production.vimbo.com.br/storage/empresas/*'),
workbox.strategies.networkFirst({
cacheName: 'vimbo-cache-api-principal-v1-storage',
method: 'GET',
plugins: [
new workbox.expiration.Plugin({
// maxAgeSeconds: 168 * 60 * 60 // 7 dias
maxAgeSeconds: 24 * 60 * 60
}),
]
})
);
// cache storage API S3
workbox.routing.registerRoute(
new RegExp('https://storage-php-api.s3.sa-east-1.amazonaws.com/*'),
workbox.strategies.networkFirst({
cacheName: 'vimbo-cache-api-principal-v1-storage-S3',
method: 'GET',
plugins: [
new workbox.expiration.Plugin({
maxAgeSeconds: 24 * 60 * 60
}),
]
})
);
// cache chamada API Principal
workbox.routing.registerRoute(
// new RegExp('https://api-production.vimbo.com.br/v1/*'),
new RegExp('https://api-production.vimbo.com.br/*'),
workbox.strategies.networkFirst({
cacheName: 'vimbo-cache-api-principal-v1',
method: 'GET',
plugins: [
new workbox.expiration.Plugin({
maxAgeSeconds: 24 * 60 * 60
}),
]
})
// cache chamada API Auxiliar
workbox.routing.registerRoute(
new RegExp('https://consultas.vimbo.com.br/*'),
workbox.strategies.networkFirst({
cacheName: 'vimbo-cache-api-auxiliar-v1',
// method: 'GET',
plugins: [
new workbox.expiration.Plugin({
maxAgeSeconds: 24 * 60 * 60
}),
]
})
);
// cache chamada API Realtime
workbox.routing.registerRoute(
new RegExp('https://notificacao-production.vimbo.com.br/*'),
workbox.strategies.networkFirst({
cacheName: 'vimbo-cache-api-realtime-v1',
// method: 'GET',
plugins: [
new workbox.expiration.Plugin({
maxAgeSeconds: 24 * 60 * 60
}),
]
})
);
/* ---------------------------- */
// Notificação HTML5
/* --------------------------- */
const showNotification = () => {
self.registration.showNotification('Background sync success!', {
body: '🎉`🎉`🎉`'
});
};
/* ---------------------------- */
// QUEUE
// sincronização em background
// em caso de falha
/* --------------------------- */
// registrar queue
const vbSyncPost = new workbox.backgroundSync.Plugin('postQueueAPI', {
maxRetentionTime: 24 * 60, // Retry for max of 24 Hours
callbacks: {
requestWillEnqueue: async(storableRequest) => {
console.log('callback post queue [requestWillEnqueue] ', storableRequest);
let dados = storableRequest;
let message = {
'status': 'fila',
'identificacao': "postQueueAPI",
'data': dados
};
// emite notificação ao cliente window
let clients = await self.clients.matchAll();
for (const client of clients) {
client.postMessage(message);
}
},
requestWillReplay: async(storableRequest) => {
console.log('callback post queue [requestWillReplay] ', storableRequest);
let dados = storableRequest;
let message = {
'status': 'replay',
'identificacao': "postQueueAPI",
'data': dados
};
// emite notificação ao cliente window
let clients = await self.clients.matchAll();
console.log('clientes', clients)
for (const client of clients) {
client.postMessage(message);
console.log('cliente', client)
}
},
queueDidReplay: async(storableRequestArray) => {
// showNotification()
console.log('callback post queue [queueDidReplay]', storableRequestArray)
// let dados = storableRequestArray;
let message = {
'status': 'executado',
'identificacao': "postQueueAPI"
// 'data' : dados
};
// emite notificação ao cliente window
let clients = await self.clients.matchAll();
for (const client of clients) {
client.postMessage(message);
}
},
}
});
const vbSyncPut = new workbox.backgroundSync.Plugin('putQueueAPI', {
maxRetentionTime: 24 * 60,
callbacks: {
requestWillEnqueue: async(storableRequest) => {
console.log('callback post queue [requestWillEnqueue] ', storableRequest);
let dados = storableRequest;
let message = {
'status': 'fila',
'identificacao': "putQueueAPI",
'data': dados
};
// emite notificação ao cliente window
let clients = await self.clients.matchAll();
for (const client of clients) {
client.postMessage(message);
}
},
requestWillReplay: async(storableRequest) => {
console.log('callback post queue [requestWillReplay] ', storableRequest);
let dados = storableRequest;
let message = {
'status': 'replay',
'identificacao': "putQueueAPI",
'data': dados
};
// emite notificação ao cliente window
let clients = await self.clients.matchAll();
console.log('clientes', clients)
for (const client of clients) {
client.postMessage(message);
console.log('cliente', client)
}
},
queueDidReplay: async(storableRequestArray) => {
console.log('callback post queue [queueDidReplay]', storableRequestArray)
// let dados = storableRequestArray;
let message = {
'status': 'executado',
'identificacao': "putQueueAPI"
// 'data' : dados
};
// emite notificação ao cliente window
let clients = await self.clients.matchAll();
for (const client of clients) {
client.postMessage(message);
}
},
}
});
const vbSyncDelete = new workbox.backgroundSync.Plugin('deleteQueueAPI', {
maxRetentionTime: 24 * 60,
callbacks: {
requestWillEnqueue: async(storableRequest) => {
console.log('callback post queue [requestWillEnqueue] ', storableRequest);
let dados = storableRequest;
let message = {
'status': 'fila',
'identificacao': "deleteQueueAPI",
'data': dados
};
// emite notificação ao cliente window
let clients = await self.clients.matchAll();
for (const client of clients) {
client.postMessage(message);
}
},
requestWillReplay: async(storableRequest) => {
console.log('callback post queue [requestWillReplay] ', storableRequest);
let dados = storableRequest;
let message = {
'status': 'replay',
'identificacao': "deleteQueueAPI",
'data': dados
};
// emite notificação ao cliente window
let clients = await self.clients.matchAll();
console.log('clientes', clients)
for (const client of clients) {
client.postMessage(message);
console.log('cliente', client)
}
},
queueDidReplay: async(storableRequestArray) => {
console.log('callback post queue [queueDidReplay]', storableRequestArray)
// let dados = storableRequestArray;
let message = {
'status': 'executado',
'identificacao': "deleteQueueAPI"
// 'data' : dados
};
// emite notificação ao cliente window
let clients = await self.clients.matchAll();
for (const client of clients) {
client.postMessage(message);
}
},
}
});
/* -------------------- */
// API PRINCIPAL
/* -------------------- */
workbox.routing.registerRoute(
// new RegExp('https://api-production.vimbo.com.br/v1/*.*'),
// new RegExp('https://api-production.vimbo.com.br/v1/*'),
new RegExp('https://api-production.vimbo.com.br/*'),
workbox.strategies.networkOnly({
plugins: [vbSyncPost]
}),
'POST'
);
workbox.routing.registerRoute(
new RegExp('https://api-production.vimbo.com.br/*'),
workbox.strategies.networkOnly({
plugins: [vbSyncPut]
}),
'PUT'
);
workbox.routing.registerRoute(
new RegExp('https://api-production.vimbo.com.br/*'),
workbox.strategies.networkOnly({
plugins: [vbSyncDelete]
}),
'DELETE'
);
/* -------------------- */
// API AUXILIAR
/* -------------------- */
workbox.routing.registerRoute(
// new RegExp('https://api-production.vimbo.com.br/v1/*.*'),
new RegExp('https://consultas.vimbo.com.br/*'),
workbox.strategies.networkOnly({
plugins: [vbSyncPost]
}),
'POST'
);
workbox.routing.registerRoute(
new RegExp('https://consultas.vimbo.com.br/*'),
workbox.strategies.networkOnly({
plugins: [vbSyncPut]
}),
'PUT'
);
workbox.routing.registerRoute(
new RegExp('https://consultas.vimbo.com.br/*'),
workbox.strategies.networkOnly({
plugins: [vbSyncDelete]
}),
'DELETE'
);
/* -------------------- */
// API REALTIME
/* -------------------- */
workbox.routing.registerRoute(
// new RegExp('https://api-production.vimbo.com.br/v1/*.*'),
new RegExp('https://notificacao-production.vimbo.com.br/*'),
workbox.strategies.networkOnly({
plugins: [vbSyncPost]
}),
'POST'
);
workbox.routing.registerRoute(
new RegExp('https://notificacao-production.vimbo.com.br/*'),
workbox.strategies.networkOnly({
plugins: [vbSyncPut]
}),
'PUT'
);
workbox.routing.registerRoute(
new RegExp('https://notificacao-production.vimbo.com.br/*'),
workbox.strategies.networkOnly({
plugins: [vbSyncDelete]
}),
'DELETE'
);
/* -------------------- */
// URL TERCEIROS
/* -------------------- */
workbox.routing.registerRoute(
new RegExp('https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-glyphicons.css'),
workbox.strategies.cacheFirst()
);
workbox.routing.registerRoute(
new RegExp('https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-glyphicons.css'),
workbox.strategies.cacheFirst()
);
workbox.routing.registerRoute(
new RegExp('https://www.googletagmanager.com/*'),
workbox.strategies.cacheFirst()
);
workbox.routing.registerRoute(
new RegExp('https://fonts.gstatic.com/*'),
workbox.strategies.cacheFirst()
);
workbox.routing.registerRoute(
new RegExp('https://static.hotjar.com/*'),
workbox.strategies.cacheFirst()
);
workbox.routing.registerRoute(
new RegExp('https://script.hotjar.com/*'),
workbox.strategies.cacheFirst()
);
workbox.routing.registerRoute(
new RegExp('https://rawgit.com/GlauberF/emojionearea/master/dist/*'),
workbox.strategies.cacheFirst()
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment