Skip to content
This repository has been archived by the owner on Jul 29, 2022. It is now read-only.

Service worker ctd. + PWA #129

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"semi": true,
"singleQuote": true,
"trailingComma": "es5",
"printWidth": 120,
"printWidth": 80,
"arrowParens": "always"
}
2 changes: 1 addition & 1 deletion netlify.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@
[[headers]]
for = "/*"
[headers.values]
Permissions-Policy = "interest-cohort=()"
Permissions-Policy = "interest-cohort=()"
21 changes: 19 additions & 2 deletions src/_data/site.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
const resolveConfig = require('tailwindcss/resolveConfig');
const tailwindConfig = require('../../tailwind.config.js');

const cssConfig = resolveConfig(tailwindConfig);

module.exports = {
language: 'en-US',
directionality: 'ltr',
name: 'Wild Peaches',
url: 'https://wildpeaches.xyz',
email: '[email protected]',
tagline: 'Mind-sized STEM ideas and experiments, beyond the textbook.',
imgDir: '/assets/img/',
icon: {
url: '/icon.png',
sizes: '512x512',
type: 'image/png',
},
favicon: '/favicon.svg',
themeColor: cssConfig.theme.colors.primary,
backgroundColor: cssConfig.theme.colors.backgroundColor,
imageDirectory: '/assets/img/',

authors: [
{
firstName: 'Jan',
lastName: 'De Wilde',
url: 'https://jandewil.de',
email: '[email protected]',
avatar: 'https://gravatar.com/avatar/0467288626c0cfc671200f96c34dc192?s=512',
avatar:
'https://gravatar.com/avatar/0467288626c0cfc671200f96c34dc192?s=512',
social: {
github: 'https://github.com/jandw',
twitter: 'https://twitter.com/jandw',
Expand Down
8 changes: 7 additions & 1 deletion src/_includes/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<meta name="google-site-verification" content="5Z_KiijxY41bgn9up8h0wpo_2REAC199OsmISONScjY" />
<meta name="theme-color" content="{{ site.themeColor }}" />
{%- include "meta.html" %}

<style>{{ css | safe }}</style>

<link rel="manifest" href="/app.webmanifest">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" integrity="sha384-AfEj0r4/OFrOo5t7NnNe46zW/tFgW6x/bCJG8FqQCEo3+Aro6EYUG4+cU+KJWu/X" crossorigin="anonymous">
<link rel="apple-touch-icon" href="/apple-touch-icon.png">

<link rel="icon" href="{{ site.favicon }}">
<link rel="mask-icon" href="{{ site.favicon }}" color="{{ site.themeColor }}">
<link rel="apple-touch-icon" href="{{ site.icon.url }}">

</head>
2 changes: 1 addition & 1 deletion src/_layouts/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
---

<!DOCTYPE html>
<html lang="en-US" dir="ltr">
<html lang="{{ site.language }}" dir="{{ site.directionality }}">
{% include 'head.html' %}
<body
class="text-base sm:text-lg md:text-xl overflow-x-hidden home-font-features {% if bodyClass %}{{ bodyClass }}{% endif %}"
Expand Down
20 changes: 20 additions & 0 deletions src/app.webmanifest.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
eleventyExcludeFromCollections: true
permalink: /app.webmanifest
---
{
"lang": "{{ site.language }}",
"name": "{{ site.name }}",
"short_name": "{{ site.name }}",
"description": "{{ site.tagline }}",
"theme_color": "{{ site.themeColor }}",
"background_color": "{{ site.backgroundColor }}",
"start_url": "/",
"display": "standalone",
"display_override": ["minimal-ui"],
"icons": [{
"src": "{{ site.icon.url }}",
"sizes": "{{ site.icon.sizes }}",
"type": "{{ site.icon.type }}"
}]
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed src/assets/siteroot/apple-touch-icon.png
Binary file not shown.
Binary file removed src/assets/siteroot/favicon.ico
Binary file not shown.
Binary file added src/assets/siteroot/favicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions src/assets/siteroot/favicon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/siteroot/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 1 addition & 22 deletions src/assets/vectors/no-image.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 17 additions & 1 deletion src/offline.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,23 @@
layout: default
---


<main id="main" class="o-center mb-12">
<h1>Offline!</h1>
<p>You can still read these pages:</p>
<ul id="history"></ul>
<script>
// Open the cache of pages
caches.open('pages').then((pagesCache) => {
pagesCache.keys().then((keys) => {
let markup = '';
// Loop through each item in the cache
keys.forEach((request) => {
// Make a link to the URL of each page
markup += `<li><a href="${request.url}">${request.url}</a></li>`;
});
// Display the list of links
document.getElementById('history').innerHTML = markup;
}); // end keys then
}); // end open
</script>
</main>
107 changes: 74 additions & 33 deletions src/service-worker.js.njk
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,90 @@ permalink: /service-worker.js

const version = 'v0.0.1';

const staticCacheName = version + 'staticfiles';
const staticCacheName = `${version}-staticfiles`;
const imageCacheName = 'images';
const pagesCacheName = 'pages';
const maxPages = 20; // maximum number of pages to cache
const maxImages = 50; // maximum number of images to cache

const cacheList = [staticCacheName, imageCacheName, pagesCacheName];
const noImage =

function updateStaticCache() {
return caches.open(staticCacheName).then((staticCache) => {
staticCache.addAll(['{{ site.icon.url }}']);
return staticCache.addAll([
// @todo add fonts
'/assets/styles/style.min.css',
'/confirmed.html',
'offline.html',
'/assets/vectors/no-image.svg',
]);
});
}

function clearOldCaches() {
return caches.keys().then((cacheNames) => {
Promise.all(
cacheNames
.filter((cacheName) => !cacheList.includes(cacheName))
.map((cacheName) => caches.delete(cacheName))
);
});
}




function trimCaches(cacheName, maxItems) {
cacheName.open( cache => {
cache.keys().then( (items) => {
if items.length > maxItems {
cache.delete(items[0])
.then(
trimCache(cacheName, maxItems)
)
}
})
})
}

addEventListener('install', (installEvent) => {
skipWaiting();
installEvent.waitUntil(
caches
.open(staticCacheName)
.then((staticCache) => {
staticCache.addAll(['/apple-touch-icon.png']);
return staticCache.addAll([
'/assets/styles/style.min.css',
'/confirmed.html',
'offline.html',
'/assets/vectors/no-image.svg',
]);
})
.catch((error) => console.error('Failure'))
);
installEvent.waitUntil(updateStaticCache()).then(skipWaiting());
});

addEventListener('fetch', (fetchEvent) => {
const request = fetchEvent.request;

// is HTML?
if (request.headers.get('Accept').includes('text/html')) {
if (request.url.includes('/blog/')) {
fetchEvent.respondWith(
caches.match(request).then((responseFromCache) => {
if (responseFromCache) {
fetchEvent.waitUntil(
fetch(request).then((responseFromFetch) => {
caches.open(pagesCacheName).then((pagesCache) => {
return pagesCache.put(request, responseFromFetch);
});
})
);
console.info('Returned from cache');
return responseFromCache;
}

return fetch(request).then((responseFromFetch) => {
const copy = responseFromFetch.clone();
fetchEvent.waitUntil(
caches.open(pagesCacheName).then((pagesCache) => {
return pagesCache.put(request, copy);
})
);
return responseFromFetch;
});
})
);
}
fetchEvent.respondWith(
fetch(request)
.then((responseFromFetch) => {
Expand Down Expand Up @@ -103,21 +158,7 @@ addEventListener('fetch', (fetchEvent) => {
})
);
});
addEventListener('activate', (activateEvent) => {
activateEvent.waitUntil(
caches
.keys()
.then((cacheNames) => {
return Promise.all(
cacheNames.map((cacheName) => {
if (!cacheList.includes(cacheName)) {
return caches.delete(cacheName);
}
})
);
})
.then(() => {
return clients.claim();
})
);

self.addEventListener('activate', (activateEvent) => {
activateEvent.waitUntil();
});
19 changes: 17 additions & 2 deletions tailwind.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,24 @@ module.exports = {
// prefix: 't-', /* BEM utility scoping */
important: true,
darkMode: 'media',
purge: ['./.eleventy.js', './src/**/*.html', './src/**/*.md', './src/**/*.json', './lib/shortcodes/**/*.js'],
purge: [
'./.eleventy.js',
'./src/**/*.html',
'./src/**/*.md',
'./src/**/*.json',
'./lib/shortcodes/**/*.js',
],
theme: {
fontFamily: {
sans: ['Inter', 'sans-serif'],
serif: ['ui-serif', 'Georgia', 'Cambria', 'Times New Roman', 'Times', 'serif'],
serif: [
'ui-serif',
'Georgia',
'Cambria',
'Times New Roman',
'Times',
'serif',
],
mono: [
'ui-monospace',
'SFMono-Regular',
Expand All @@ -33,6 +46,8 @@ module.exports = {
green: colors.green,
red: colors.red,
teal: colors.teal,
primary: colors.warmGray[700],
backgroundColor: colors.warmGray[50],
},
listStyleType: {
none: 'none',
Expand Down