Skip to content

Commit

Permalink
Update dependencies and improve service worker documentation; bump ve…
Browse files Browse the repository at this point in the history
…rsion to 0.0.18
  • Loading branch information
tech_e committed Jan 15, 2025
1 parent b7cba5a commit bfc22b2
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 38 deletions.
43 changes: 23 additions & 20 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"vite": "^3.1.0"
},
"files": [
"pacakge.json",
"package.json",
"readme.md"
],
"homepage": "https://github.com/chrisdurfee/multisplode/"
Expand Down
35 changes: 30 additions & 5 deletions public/sw.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,38 @@
importScripts('./worker/cache-controller.js', './worker/service.js');

const appName = 'multisplode',
version = '0.0.17';
/**
* This is the name of the app and the version. This is used to
* create the cache name.
*
* @const
* @type {string} appName
*/
const APP_NAME = 'multisplode',

const files =
/**
* This is the version of the app. This is used to create the cache
*
* @const
* @type {string} version
*/
version = '0.0.18';

/**
* This will add these files to cache. Add any files you want to
* cache here. As files are fetched, they will be added to the cache.
*
* @const
* @type {Array<string>} DEFAULT_FILES
*/
const DEFAULT_FILES =
[
'./',
'./index.html'
];

const appNameId = appName + '-v' + version;
const service = new Service(appNameId, files);
/**
* This will set up the service worker controller with
* the app name and the files to cache.
*/
const appNameId = `${APP_NAME}-${version}`;
const service = new Service(appNameId, DEFAULT_FILES);
18 changes: 17 additions & 1 deletion public/worker/cache-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,12 @@ class CacheController
});
}


/**
* This will add files to the cache.
*
* @param {array} files
* @returns {Promise}
*/
addFiles(files)
{
return this.open(this.cacheName, (cache) =>
Expand All @@ -98,6 +103,11 @@ class CacheController
});
}

/**
* This will delete files from the cache.
*
* @returns {void}
*/
deleteFiles()
{
caches.delete(this.cacheName).then((success) =>
Expand All @@ -106,6 +116,12 @@ class CacheController
});
}

/**
* This will get files from the cache.
*
* @param {object} e
* @returns {Promise}
*/
fetchFile(e)
{
const request = e.request;
Expand Down
29 changes: 18 additions & 11 deletions public/worker/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,24 @@
*
* @class Service
*/
class Service extends CacheController
class Service
{
/**
* This will create a service that will cache files.
*
* @param {string} prefix
* @param {Array} files
*/
* This will create a new service.
*
* @param {string} prefix
* @param {Array<string>} files
*/
constructor(prefix, files = [])
{
super(prefix);
/**
* @member {CacheController} cache
*/
this.cache = new CacheController(prefix);

/**
* @member {Array<string>} files
*/
this.files = files;

this.addEvents();
Expand All @@ -33,14 +40,14 @@ class Service extends CacheController
self.skipWaiting();

e.waitUntil(
this.addFiles(this.files)
this.cache.addFiles(this.files)
);
});

self.addEventListener('activate', (e) =>
{
e.waitUntil(
this.refresh()
this.cache.refresh()
);

return self.clients.claim();
Expand All @@ -50,7 +57,7 @@ class Service extends CacheController
{
if(e.data === 'delete')
{
this.deleteFiles();
this.cache.deleteFiles();
}
});

Expand All @@ -76,7 +83,7 @@ class Service extends CacheController
return false;
}

const response = this.fetchFile(e);
const response = this.cache.fetchFile(e);
e.respondWith(response);
});
}
Expand Down

0 comments on commit bfc22b2

Please sign in to comment.