mirror of
https://github.com/dilllxd/gitfolio.git
synced 2024-08-14 22:28:09 +00:00
+ Service Worker (#47)
* chore: lint markup & Load SW if browsers support * chore: Get SW and copy to dist on populateCss * SW stuff!
This commit is contained in:
parent
1341be7e39
commit
8441691434
3 changed files with 136 additions and 76 deletions
|
@ -1,77 +1,90 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
||||
<title></title>
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.7.0/animate.min.css">
|
||||
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.1/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
|
||||
<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
|
||||
<link rel="stylesheet" href="index.css">
|
||||
</head>
|
||||
<body>
|
||||
<div id="loading">
|
||||
<div id="spinner"></div>
|
||||
</div>
|
||||
<div id="profile">
|
||||
<div id="profile_img"></div>
|
||||
<div id="username">
|
||||
<div id="fullname"></div>
|
||||
</div>
|
||||
<div id="userbio"></div>
|
||||
<div id="about"></div>
|
||||
</div>
|
||||
<div id="display">
|
||||
<div id="work">
|
||||
<h1>Work.</h1>
|
||||
<div class="projects" id="work_section"></div>
|
||||
</div>
|
||||
<div id="forks" style="display:none;">
|
||||
<h1>Forks.</h1>
|
||||
<div class="projects" id="forks_section"></div>
|
||||
</div>
|
||||
<div id="blog_section">
|
||||
<h1>Blog.</h1>
|
||||
<div id="blogs"></div>
|
||||
</div>
|
||||
<div id="footer">
|
||||
<a href="https://github.com/imfunniee" target="_blank">made on earth by a human</a>
|
||||
</div>
|
||||
</div>
|
||||
<script type="text/javascript">
|
||||
setTimeout(function(){
|
||||
document.getElementById("loading").classList.add("animated");
|
||||
document.getElementById("loading").classList.add("fadeOut");
|
||||
setTimeout(function(){
|
||||
document.getElementById("loading").classList.remove("animated");
|
||||
document.getElementById("loading").classList.remove("fadeOut");
|
||||
document.getElementById("loading").style.display = "none";
|
||||
},800);
|
||||
},1500);
|
||||
$.getJSON("blog.json", function(blog){
|
||||
blog = blog || [];
|
||||
if(blog.length == 0){
|
||||
return document.getElementById("blog_section").style.display = "none";
|
||||
}
|
||||
for(var i = 0; i < blog.length; i++){
|
||||
$("#blogs").append(`
|
||||
<a href="./blog/${blog[i].url_title}/" target="_blank">
|
||||
<section>
|
||||
<img src="${blog[i].top_image}">
|
||||
<div class="blog_container">
|
||||
<div class="section_title">${blog[i].title}</div>
|
||||
<div class="about_section">
|
||||
${blog[i].sub_title}
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
</a>
|
||||
`);
|
||||
}
|
||||
}).fail(function(){
|
||||
return document.getElementById("blog_section").style.display = "none";
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
||||
<title></title>
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.7.0/animate.min.css">
|
||||
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.1/css/all.css"
|
||||
integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
|
||||
<script src="https://code.jquery.com/jquery-3.4.1.min.js"
|
||||
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
|
||||
<link rel="stylesheet" href="index.css">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div id="loading">
|
||||
<div id="spinner"></div>
|
||||
</div>
|
||||
<div id="profile">
|
||||
<div id="profile_img"></div>
|
||||
<div id="username">
|
||||
<div id="fullname"></div>
|
||||
</div>
|
||||
<div id="userbio"></div>
|
||||
<div id="about"></div>
|
||||
</div>
|
||||
<div id="display">
|
||||
<div id="work">
|
||||
<h1>Work.</h1>
|
||||
<div class="projects" id="work_section"></div>
|
||||
</div>
|
||||
<div id="forks" style="display:none;">
|
||||
<h1>Forks.</h1>
|
||||
<div class="projects" id="forks_section"></div>
|
||||
</div>
|
||||
<div id="blog_section">
|
||||
<h1>Blog.</h1>
|
||||
<div id="blogs"></div>
|
||||
</div>
|
||||
<div id="footer">
|
||||
<a href="https://github.com/imfunniee" target="_blank">made on earth by a human</a>
|
||||
</div>
|
||||
</div>
|
||||
<script type="text/javascript">
|
||||
setTimeout(function () {
|
||||
document.getElementById("loading").classList.add("animated");
|
||||
document.getElementById("loading").classList.add("fadeOut");
|
||||
setTimeout(function () {
|
||||
document.getElementById("loading").classList.remove("animated");
|
||||
document.getElementById("loading").classList.remove("fadeOut");
|
||||
document.getElementById("loading").style.display = "none";
|
||||
}, 800);
|
||||
}, 1500);
|
||||
$.getJSON("blog.json", function (blog) {
|
||||
blog = blog || [];
|
||||
if (blog.length == 0) {
|
||||
return document.getElementById("blog_section").style.display = "none";
|
||||
}
|
||||
for (var i = 0; i < blog.length; i++) {
|
||||
$("#blogs").append(`
|
||||
<a href="./blog/${blog[i].url_title}/" target="_blank">
|
||||
<section>
|
||||
<img src="${blog[i].top_image}">
|
||||
<div class="blog_container">
|
||||
<div class="section_title">${blog[i].title}</div>
|
||||
<div class="about_section">
|
||||
${blog[i].sub_title}
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
</a>
|
||||
`);
|
||||
}
|
||||
}).fail(function () {
|
||||
return document.getElementById("blog_section").style.display = "none";
|
||||
});
|
||||
</script>
|
||||
|
||||
<script type="text/javascript">
|
||||
if ('serviceWorker' in navigator) {
|
||||
window.addEventListener('load', () => {
|
||||
navigator.serviceWorker.register('service-worker.js');
|
||||
});
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
42
assets/service-worker.js
Normal file
42
assets/service-worker.js
Normal file
|
@ -0,0 +1,42 @@
|
|||
importScripts('https://storage.googleapis.com/workbox-cdn/releases/3.6.1/workbox-sw.js');
|
||||
|
||||
if (workbox) {
|
||||
workbox.setConfig({
|
||||
debug: false
|
||||
});
|
||||
|
||||
var defaultStrategy = workbox.strategies.networkFirst({
|
||||
cacheName: "fallback",
|
||||
plugins: [
|
||||
new workbox.expiration.Plugin({
|
||||
maxEntries: 128,
|
||||
maxAgeSeconds: 7 * 24 * 60 * 60, // 1 week
|
||||
purgeOnQuotaError: true, // Opt-in to automatic cleanup
|
||||
}),
|
||||
new workbox.cacheableResponse.Plugin({
|
||||
statuses: [0, 200] // for opague requests
|
||||
}),
|
||||
],
|
||||
});
|
||||
workbox.routing.setDefaultHandler(
|
||||
(args) => {
|
||||
if (args.event.request.method === 'GET') {
|
||||
return defaultStrategy.handle(args); // use default strategy
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
workbox.routing.registerRoute(
|
||||
new RegExp(/.*\.(?:js|css)/g),
|
||||
workbox.strategies.networkFirst()
|
||||
);
|
||||
|
||||
workbox.routing.registerRoute(
|
||||
new RegExp(/.*\.(?:png|jpg|jpeg|svg|gif|webp)/g),
|
||||
workbox.strategies.cacheFirst()
|
||||
);
|
||||
} else {
|
||||
console.log(`No workbox on this browser 😬`);
|
||||
}
|
5
build.js
5
build.js
|
@ -27,6 +27,8 @@ async function populateCSS({
|
|||
let template = path.resolve(assetDir, 'index.css');
|
||||
let stylesheet = path.join(outDir, 'index.css');
|
||||
|
||||
let serviceWorker = path.resolve(assetDir, 'service-worker.js');
|
||||
|
||||
try {
|
||||
await fs.accessAsync(outDir, fs.constants.F_OK);
|
||||
} catch (err) {
|
||||
|
@ -35,6 +37,9 @@ async function populateCSS({
|
|||
/* Copy over the template CSS stylesheet */
|
||||
await fs.copyFileAsync(template, stylesheet);
|
||||
|
||||
/* Add Service Worker */
|
||||
await fs.copyFileSync(serviceWorker, `${outDir}/service-worker.js`);
|
||||
|
||||
/* Get an array of every available theme */
|
||||
let themes = await fs.readdirAsync(path.join(assetDir, 'themes'));
|
||||
|
||||
|
|
Loading…
Reference in a new issue