mirror of
https://github.com/NebulaServices/Nebula.git
synced 2025-05-16 04:50:01 -04:00
Bare Server Switching done (almost)
This commit is contained in:
parent
400070dcb5
commit
75beb0e282
7 changed files with 2909 additions and 19 deletions
59
public/sw.js
59
public/sw.js
|
@ -3,28 +3,49 @@ importScripts("/uv/uv.config.js");
|
|||
importScripts(__uv$config.sw || "/uv/uv.sw.js");
|
||||
importScripts("/dynamic/dynamic.config.js");
|
||||
importScripts("/dynamic/dynamic.worker.js");
|
||||
//import our IDB lib
|
||||
importScripts('/libs/localforage.js');
|
||||
localforage.config({
|
||||
driver: localforage.INDEXEDDB,
|
||||
name: 'Nebula',
|
||||
version: 1.0,
|
||||
storeName: 'nebula_config',
|
||||
description: 'Nebula Config for things reliant on IndexedDB'
|
||||
})
|
||||
|
||||
const sw = new UVServiceWorker();
|
||||
const dynamic = new Dynamic();
|
||||
|
||||
self.dynamic = dynamic;
|
||||
const dynPromise = new Promise(async (resolve) => {
|
||||
try {
|
||||
const bare = await localforage.getItem('bare') || location.origin + "/bare/";
|
||||
self.__dynamic$config.bare.path = bare;
|
||||
self.dynamic = new Dynamic(self.__dynamic$config);
|
||||
}
|
||||
catch (error) { console.log(error); }
|
||||
resolve();
|
||||
});
|
||||
|
||||
const uvPromise = new Promise(async (resolve) => {
|
||||
try {
|
||||
const bare = await localforage.getItem('bare') || location.origin + "/bare/";
|
||||
self.__uv$config.bare = bare;
|
||||
self.uv = new UVServiceWorker(self.__uv$config);
|
||||
}
|
||||
catch (error) { console.log(error); }
|
||||
resolve();
|
||||
});
|
||||
|
||||
self.addEventListener("fetch", (event) => {
|
||||
if (
|
||||
event.request.url.startsWith(location.origin + self.__dynamic$config.prefix)
|
||||
) {
|
||||
event.respondWith(
|
||||
(async function () {
|
||||
if (await dynamic.route(event)) {
|
||||
return await dynamic.fetch(event);
|
||||
}
|
||||
|
||||
return await fetch(event.request);
|
||||
})()
|
||||
);
|
||||
} else if (
|
||||
event.request.url.startsWith(location.origin + __uv$config.prefix)
|
||||
) {
|
||||
event.respondWith(sw.fetch(event));
|
||||
if ( event.request.url.startsWith(location.origin + self.__dynamic$config.prefix )) {
|
||||
event.respondWith((async function() {
|
||||
try { await dynPromise } catch (error) { }
|
||||
if (await self.dynamic.route(event)) { return await self.dynamic.fetch(event); }
|
||||
await fetch(event.request);
|
||||
})());
|
||||
}
|
||||
else if ( event.request.url.startsWith(location.origin + self.__uv$config.prefix )) {
|
||||
event.respondWith((async function() {
|
||||
try { await uvPromise } catch (error) { }
|
||||
return await self.uv.fetch(event);
|
||||
})());
|
||||
}
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue