Add PWA support
continuous-integration/drone Build is passing Details

This commit is contained in:
Marko Korhonen 2022-09-14 21:45:03 +03:00
parent 6c4e9fc6d9
commit bef6c948f4
Signed by: FunctionalHacker
GPG Key ID: A7F78BCB859CD890
3 changed files with 486 additions and 0 deletions

242
assets/sw.js Normal file
View File

@ -0,0 +1,242 @@
const CACHE_VERSION = 1;
const BASE_CACHE_FILES = ["/site.webmanifest"];
const OFFLINE_CACHE_FILES = [
//"/images/logo.png",
];
const NOT_FOUND_CACHE_FILES = ["/404.html"];
const OFFLINE_PAGE = "/offline/index.html";
const NOT_FOUND_PAGE = "/404.html";
const CACHE_VERSIONS = {
assets: "assets-v" + CACHE_VERSION,
content: "content-v" + CACHE_VERSION,
offline: "offline-v" + CACHE_VERSION,
notFound: "404-v" + CACHE_VERSION,
};
// Define MAX_TTL's in SECONDS for specific file extensions
const MAX_TTL = {
"/": 3600,
html: 3600,
json: 86400,
js: 86400,
css: 86400,
};
const CACHE_BLACKLIST = [
(str) => {
return !str.startsWith("http://localhost");
},
];
const SUPPORTED_METHODS = ["GET"];
/**
* isBlackListed
* @param {string} url
* @returns {boolean}
*/
function isBlacklisted(url) {
return CACHE_BLACKLIST.length > 0
? !CACHE_BLACKLIST.filter((rule) => {
if (typeof rule === "function") {
return !rule(url);
} else {
return false;
}
}).length
: false;
}
/**
* getFileExtension
* @param {string} url
* @returns {string}
*/
function getFileExtension(url) {
let extension = url.split(".").reverse()[0].split("?")[0];
return extension.endsWith("/") ? "/" : extension;
}
/**
* getTTL
* @param {string} url
*/
function getTTL(url) {
if (typeof url === "string") {
let extension = getFileExtension(url);
if (typeof MAX_TTL[extension] === "number") {
return MAX_TTL[extension];
} else {
return null;
}
} else {
return null;
}
}
/**
* installServiceWorker
* @returns {Promise}
*/
function installServiceWorker() {
return Promise.all([
caches.open(CACHE_VERSIONS.assets).then((cache) => {
return cache.addAll(BASE_CACHE_FILES);
}),
caches.open(CACHE_VERSIONS.offline).then((cache) => {
return cache.addAll(OFFLINE_CACHE_FILES);
}),
caches.open(CACHE_VERSIONS.notFound).then((cache) => {
return cache.addAll(NOT_FOUND_CACHE_FILES);
}),
]);
}
/**
* cleanupLegacyCache
* @returns {Promise}
*/
function cleanupLegacyCache() {
let currentCaches = Object.keys(CACHE_VERSIONS).map((key) => {
return CACHE_VERSIONS[key];
});
return new Promise((resolve, reject) => {
caches
.keys()
.then((keys) => {
return (legacyKeys = keys.filter((key) => {
return !~currentCaches.indexOf(key);
}));
})
.then((legacy) => {
if (legacy.length) {
Promise.all(
legacy.map((legacyKey) => {
return caches.delete(legacyKey);
})
)
.then(() => {
resolve();
})
.catch((err) => {
reject(err);
});
} else {
resolve();
}
})
.catch(() => {
reject();
});
});
}
self.addEventListener("install", (event) => {
event.waitUntil(installServiceWorker());
});
// The activate handler takes care of cleaning up old caches.
self.addEventListener("activate", (event) => {
event.waitUntil(
Promise.all([cleanupLegacyCache()]).catch((err) => {
event.skipWaiting();
})
);
});
self.addEventListener("fetch", (event) => {
event.respondWith(
caches.open(CACHE_VERSIONS.content).then((cache) => {
return cache
.match(event.request)
.then((response) => {
if (response) {
let headers = response.headers.entries();
let date = null;
for (let pair of headers) {
if (pair[0] === "date") {
date = new Date(pair[1]);
}
}
if (date) {
let age = parseInt(
(new Date().getTime() - date.getTime()) / 1000
);
let ttl = getTTL(event.request.url);
if (ttl && age > ttl) {
return new Promise((resolve) => {
return fetch(event.request)
.then((updatedResponse) => {
if (updatedResponse) {
cache.put(event.request, updatedResponse.clone());
resolve(updatedResponse);
} else {
resolve(response);
}
})
.catch(() => {
resolve(response);
});
}).catch((err) => {
return response;
});
} else {
return response;
}
} else {
return response;
}
} else {
return null;
}
})
.then((response) => {
if (response) {
return response;
} else {
return fetch(event.request)
.then((response) => {
if (response.status < 400) {
if (
~SUPPORTED_METHODS.indexOf(event.request.method) &&
!isBlacklisted(event.request.url)
) {
cache.put(event.request, response.clone());
}
return response;
} else {
return caches.open(CACHE_VERSIONS.notFound).then((cache) => {
return cache.match(NOT_FOUND_PAGE);
});
}
})
.then((response) => {
if (response) {
return response;
}
})
.catch(() => {
return caches
.open(CACHE_VERSIONS.offline)
.then((offlineCache) => {
return offlineCache.match(OFFLINE_PAGE);
});
});
}
})
.catch((error) => {
console.error(" Error in fetch handler:", error);
throw error;
});
})
);
});

View File

@ -0,0 +1,207 @@
{{/* variables for enabling/disabling parts of the footer */}}
{{ $footerEnabled := site.Params.footer.enable | default true }}
{{ $navigationEnabled := site.Params.footer.navigation.enable | default true }}
{{ $customMenusEnabled := site.Params.footer.navigation.customMenus | default true }}
{{ $contactMeEnabled := site.Params.footer.contactMe.enable | default true }}
{{ $newsletterEnabled := site.Params.footer.newsletter.enable | default true }}
{{ $credentialsEnabled := site.Params.footer.credentials.enable | default true }}
{{ $disclaimerEnabled := site.Params.footer.disclaimer.enable | default false }}
{{/* Keep backward compatibility for the newsletter function */}}
{{ if site.Params.newsletter }}
{{ if site.Params.newsletter.enable }}
{{ $newsletterEnabled = true }}
{{ else }}
{{ $newsletterEnabled = false }}
{{ end }}
{{ end }}
{{ if $footerEnabled }}
{{ $author:= site.Data.author }}
{{ if (index site.Data site.Language.Lang).author }}
{{ $author = (index site.Data site.Language.Lang).author }}
{{ end }}
{{ $sections:= site.Data.sections }}
{{ if (index site.Data site.Language.Lang).sections }}
{{ $sections = (index site.Data site.Language.Lang).sections }}
{{ end }}
{{ $customMenus := site.Params.customMenus }}
{{ if (index site.Data site.Language.Lang).site.customMenus }}
{{ $customMenus = (index site.Data site.Language.Lang).site.customMenus }}
{{ end }}
{{ $copyrightNotice := now.Format "2006" | printf "© %s Copyright."}}
{{ if (index site.Data site.Language.Lang).site }}
{{ $siteConfig := (index site.Data site.Language.Lang).site }}
{{ if $siteConfig.copyright }}
{{ $copyrightNotice = $siteConfig.copyright }}
{{ end }}
{{ end }}
{{ $disclaimer := "" }}
{{ $siteConfig := (index site.Data site.Language.Lang).site }}
{{ if $siteConfig.disclaimer }}
{{ $disclaimer = $siteConfig.disclaimer }}
{{ end }}
{{/* footer logos */}}
{{ $themeLogo := "/images/theme-logo.png" }}
{{ $hugoLogo := "/images/hugo-logo.svg" }}
{{/* resize the logos. don't resize svg because it is not supported */}}
{{ $themeLogo:= resources.Get $themeLogo}}
{{ if and $themeLogo (ne $themeLogo.MediaType.SubType "svg") }}
{{ $themeLogo = $themeLogo.Resize "32x" }}
{{ end }}
{{ $themeLogo = $themeLogo.RelPermalink}}
{{ $hugoLogo:= resources.Get $hugoLogo}}
{{ if and $hugoLogo (ne $hugoLogo.MediaType.SubType "svg")}}
{{ $hugoLogo = $hugoLogo.Resize "32x" }}
{{ end }}
{{ $hugoLogo = $hugoLogo.RelPermalink}}
<footer id="footer" class="container-fluid text-center align-content-center footer pb-2">
<div class="container pt-5">
<div class="row text-left">
{{ if $navigationEnabled }}
<div class="col-md-4 col-sm-12">
<h5>{{ i18n "navigation" }}</h5>
{{ if $sections }}
<ul>
{{- range sort $sections "section.weight" }}
{{ if and (.section.enable) (.section.showOnNavbar)}}
{{ $sectionID := replace (lower .section.name) " " "-" }}
{{ if .section.id }}
{{ $sectionID = .section.id }}
{{ end }}
<li class="nav-item">
<a class="smooth-scroll" href="{{ "" | absLangURL }}#{{ $sectionID }}">{{ .section.name }}</a>
</li>
{{ end }}
{{- end }}
{{ if $customMenusEnabled }}
{{ range $customMenus }}
{{ if .showOnFooter }}
<li class="nav-item">
<a class="smooth-scroll" href="{{ .url }}">{{ .name }}</a>
</li>
{{ end }}
{{ end }}
{{ end }}
</ul>
{{ end }}
</div>
{{ end }}
{{ if (and $contactMeEnabled $author) }}
<div class="col-md-4 col-sm-12">
<h5>{{ i18n "contact_me" }}</h5>
<ul>
{{ range $key,$value:= $author.contactInfo }}
{{ if (eq $key "email") }}
<li><a href={{ printf "mailto:%s" $value }} target="_blank" rel="noopener">
<span><i class="fas fa-envelope"></i></span> <span>{{ $value }}</span>
</a></li>
{{ else if (eq $key "phone") }}
<li><span><i class="fas fa-phone-alt"></i></span> <span>{{ $value }}</span></li>
{{ else if (eq $key "linkedin") }}
<li><a href={{ printf "https://www.linkedin.com/in/%s" $value }} target="_blank" rel="noopener">
<span><i class="fab fa-linkedin"></i></span> <span>{{ $author.name }}</span>
</a></li>
{{ else if (eq $key "github") }}
<li><a href={{ printf "https://github.com/%s" $value }} target="_blank" rel="noopener">
<span><i class="fab fa-github"></i></span> <span>{{ $value }}</span>
</a></li>
{{ else }}
<li><span>{{ title $key }}: </span> <span>{{ $value }}</span></li>
{{ end }}
{{ end }}
</ul>
</div>
{{ end }}
<!-------------- Newsletter --------------->
{{ if $newsletterEnabled }}
{{ $provider := site.Params.footer.newsletter.provider }}
<div class="col-md-4 col-sm-12">
<p>{{ i18n "newsletter_text" }}</p>
{{ if and (eq $provider "mailchimp") site.Params.footer.newsletter.mailchimpURL }} <!-- mailchimp -->
<form
action="{{ site.Params.footer.newsletter.mailchimpURL }}"
method="post"
id="mc-embedded-subscribe-form"
name="mc-embedded-subscribe-form"
class="validate"
target="_blank"
novalidate >
<div class="form-group">
<input
type="email"
class="form-control"
id="mce-EMAIL"
name="EMAIL"
aria-describedby="emailHelp"
placeholder="{{ i18n "newsletter_input_placeholder" }}"
/>
<small id="emailHelp" class="form-text text-muted"
>{{ i18n "newsletter_warning" }}</small
>
</div>
<button type="submit" class="btn btn-info">{{ i18n "submit" }}</button>
</form>
{{ else }}
<form method='post' action='https://blogtrottr.com'>
<div class="form-group">
<input type='email' class="form-control" name='btr_email' placeholder="{{ i18n "newsletter_input_placeholder" }}"/><br />
<input type='hidden' name='btr_url' value='{{ "" | absLangURL }}index.xml' />
<input type='hidden' name='schedule_type' value='1' />
<small id="emailHelp" class="form-text text-muted">{{ i18n "newsletter_warning" }}</small>
<button type="submit" class="btn btn-info"> {{ i18n "submit" }} </button>
</div>
</form>
{{ end }}
</div>
{{ end }}
</div>
</div>
{{ if and $disclaimerEnabled $disclaimer}}
<hr />
<div class="container">
<p id="disclaimer"><strong>{{ i18n "disclaimer_text" }}:</strong> {{ $disclaimer | markdownify }}</p>
</div>
{{ end }}
{{ if $credentialsEnabled }}
<hr />
<div class="container">
<div class="row text-left">
<div class="col-md-4">
<a id="theme" href="https://github.com/hossainemruz/toha" target="_blank" rel="noopener">
<img src="{{ $themeLogo }}" alt="Toha Theme Logo">
Toha
</a>
</div>
<div class="col-md-4 text-center">{{ $copyrightNotice | markdownify }}</div>
<div class="col-md-4 text-right">
<a id="hugo" href="https://gohugo.io/" target="_blank" rel="noopener">{{ i18n "hugoAttributionText" }}
<img
src="{{ $hugoLogo }}"
alt="Hugo Logo"
height="18"
/>
</a>
</div>
</div>
</div>
{{ end }}
<!-- ==== Add ServiceWorker for PWA support ===== -->
<script>
if('serviceWorker' in navigator) {
navigator.serviceWorker
.register('/sw.js', { scope: '/' });
}
</script>
</footer>
{{end}}

View File

@ -0,0 +1,37 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<!-- ==== Add manifest for PWA support ===== -->
<link rel="manifest" href="/site.webmanifest" />
<!-- ============ import common css ========== -->
<link rel="stylesheet" href="{{ "/css/bootstrap.min.css" | relURL }}"/> <link
rel="stylesheet" href="{{ "/css/layouts/main.css" | relURL }}"/> <link
rel="stylesheet" href="{{ "/css/navigators/navbar.css" | relURL }}"/> <link
rel="stylesheet" href="{{ "/css/plyr.css" | relURL }}"/> <link rel="stylesheet"
href="{{ "/css/flag-icon.min.css" | relURL }}"/>
<!--=================== fonts ==============================-->
<link
rel="stylesheet"
href="https://fonts.googleapis.com/css2?family=Muli:wght@300;400;500;600"
/>
<!--=================== icons ==============================-->
<link rel="stylesheet" href="{{ "/fontawesome/css/all.min.css" | relURL }}"/>
<!--=================== dark mode ==========================-->
{{ if site.Params.darkMode.enable }} <link rel="stylesheet" href="{{
"/css/colortheme/colortheme.css" | relURL }}"/> {{ end }}
<!--================= fab-icon =========================-->
{{/* add favicon only if the site author has provided the the favicon */}} {{ if
site.Params.logo.favicon }} {{ $favicon := site.Params.logo.favicon }} {{/*
resize the favicon. don't resize svg because it is not supported */}} {{
$favicon = resources.Get $favicon }} {{ if and $favicon (ne
$favicon.MediaType.SubType "svg") }} {{ $favicon = $favicon.Resize "42x" }} {{
end }} {{ $favicon = $favicon.RelPermalink}}
<link rel="icon" type="image/png" href="{{ $favicon }}" />
{{end}}