Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Service Worker cache #58

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 104 additions & 64 deletions sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,50 +120,92 @@ const _resolveFollowUrl = u => fetch(_rewriteUrlToProxy(u), {
method: 'HEAD',
}).then(res => _rewriteUrlToRaw(res.url));

self.addEventListener('install', event => {
const cacheName = 'proxy';
let cache = null;
self.addEventListener('install', event => event.waitUntil(

(async () => {
// console.log('sw install');
self.skipWaiting();

await caches.delete(cacheName);
cache = await caches.open(cacheName);

await cache.addAll([
'core.js',
'Document.js',
'Event.js',
'GlobalContext.js',
'Graphics.js',
'HelioWebXRPolyfill.js',
'History.js',
'index.js',
'Location.js',
'Navigator.js',
'symbols.js',
'USKeyboardLayout.js',
'utils.js',
'VR.js',
'webxr-polyfill.module.js',
'WindowBase.js',
'Window.js',
'WindowVm.js',
'xr-iframe.js',
'XR.js',
'xr-scene.js',
].map(n => `/src/${n}`));

/* event.waitUntil(
caches.open(PRECACHE)
.then(cache => cache.addAll(PRECACHE_URLS))
.then(self.skipWaiting())
); */
});
})()

));

self.addEventListener('activate', event => {
// console.log('sw activate');
self.clients.claim();
});
self.addEventListener('fetch', event => {

self.addEventListener('fetch', event => event.respondWith(

(async () => {
// console.log('got request', event.request.url);

const permanentRedirect = permanentRedirects[event.request.url];
if (permanentRedirect) {
event.respondWith(
fetch(permanentRedirect)
);
return;
}
let u = event.request.url;
const dst = redirects.get(u);
if (dst) {
redirects.delete(event.request.url);

if (event.request.method === 'HEAD' && event.request.url === event.request.referrer) {
event.respondWith(new Response('', {
const res = new Response(dst, {
headers: {
'Content-Type': 'text/html',
},
});
return _rewriteResExt(u, u, res.headers, res);
} else if (event.request.method === 'HEAD' && event.request.url === event.request.referrer) {
return new Response('', {
headers: {
'Content-Type': 'text/html',
'Date': new Date().toUTCString(),
},
}));
} else {
let u = event.request.url;
const dst = redirects.get(u);
if (dst) {
redirects.delete(event.request.url);

const res = new Response(dst, {
headers: {
'Content-Type': 'text/html',
},
});
event.respondWith(_rewriteResExt(u, u, res.headers, res));
});
}

if (!cache) {
cache = await caches.open(cacheName);
}

const cachedRes = await cache.match(event.request);
if (cachedRes) {
return cachedRes;
}

const res = await (() => {
const permanentRedirect = permanentRedirects[event.request.url];
if (permanentRedirect) {
return fetch(permanentRedirect);
} else {
let match = u.match(/^[a-z]+:\/\/[a-zA-Z0-9\-\.:]+(.+)$/);
if (match) {
Expand All @@ -172,58 +214,56 @@ self.addEventListener('fetch', event => {
const originalUrl = match2[1];
const permanentRedirect = permanentRedirects[originalUrl];
if (permanentRedirect) {
event.respondWith(
fetch(permanentRedirect)
);
return fetch(permanentRedirect);
} else {
const proxyUrl = _rewriteUrlToProxy(originalUrl);
event.respondWith(
fetch(proxyUrl).then(res => {
res.originalUrl = originalUrl;
return _rewriteRes(res);
})
);
return fetch(proxyUrl).then(res => {
res.originalUrl = originalUrl;
return _rewriteRes(res);
});
}
} else if (match2 = match[1].match(/^\/.d\/(.+)$/)) {
event.respondWith(fetch(match2[1]));
return fetch(match2[1]);
} else if (match2 = match[1].match(/^\/.f\/(.+)$/)) {
event.respondWith(
_resolveFollowUrl(match2[1])
.then(u => new Response(u, {
headers: {
'Content-Type': 'text/plain',
},
}))
);
return _resolveFollowUrl(match2[1])
.then(u => new Response(u, {
headers: {
'Content-Type': 'text/plain',
},
}));
} else {
event.respondWith(
fetch(event.request)
.then(res => {
if (res.type === 'opaque') {
const proxyUrl = _rewriteUrlToProxy(u);
return fetch(proxyUrl).then(res => {
res.originalUrl = u;
return _rewriteRes(res);
});
} else {
res.originalUrl = u;
return _rewriteRes(res);
}
})
.catch(err => {
return fetch(event.request)
.then(res => {
if (res.type === 'opaque') {
const proxyUrl = _rewriteUrlToProxy(u);
return fetch(proxyUrl).then(res => {
res.originalUrl = u;
return _rewriteRes(res);
});
})
);
})
} else {
res.originalUrl = u;
return _rewriteRes(res);
}
})
.catch(err => {
const proxyUrl = _rewriteUrlToProxy(u);
return fetch(proxyUrl).then(res => {
res.originalUrl = u;
return _rewriteRes(res);
});
})
}
} else {
event.respondWith(new Response('invalid url', {
return Promise.resolve(new Response('invalid url', {
status: 500,
}));
}
}
})();
if (event.request.method !== 'HEAD') {
cache.put(event.request, res.clone());
}
});
return res;
})()

));