Skip to content

Commit c503548

Browse files
committed
Remove gatsby-plugin-offline, support for PWA added
1 parent 4c3603a commit c503548

File tree

6 files changed

+107
-269
lines changed

6 files changed

+107
-269
lines changed

gatsby-wordpress-theme-libre/gatsby-config.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ module.exports = (themeOptions) => {
8888
`/404`,
8989
`/404.html`,
9090
`/offline-plugin-app-shell-fallback`,
91+
'/offline',
92+
'/offline.html'
9193
],
9294
createLinkInHead: true,
9395
addUncaughtPages: true,
@@ -96,7 +98,6 @@ module.exports = (themeOptions) => {
9698
`gatsby-plugin-catch-links`,
9799
`gatsby-plugin-react-helmet`,
98100
`gatsby-plugin-force-trailing-slashes`,
99-
`gatsby-plugin-offline`,
100101
{
101102
resolve: `gatsby-plugin-manifest`,
102103
options: {
@@ -109,6 +110,7 @@ module.exports = (themeOptions) => {
109110
icon: "static/favicon.png",
110111
},
111112
},
113+
'gatsby-plugin-remove-serviceworker',
112114
{
113115
resolve: `gatsby-plugin-feed`,
114116
options: {
@@ -161,7 +163,7 @@ module.exports = (themeOptions) => {
161163
options: {
162164
canonicalBaseUrl: siteConfig.siteUrl,
163165
components: [`amp-form`],
164-
excludedPaths: [`/404*`, `/`],
166+
excludedPaths: [`/404*`, `/`, `/offline*`],
165167
pathIdentifier: `amp/`,
166168
relAmpHtmlPattern: `{{canonicalBaseUrl}}{{pathname}}{{pathIdentifier}}`,
167169
useAmpClientIdApi: true,
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const React = require("react");
2+
3+
exports.onRenderBody = ({ pathname, setHeadComponents }) => {
4+
setHeadComponents([
5+
<script
6+
type="module"
7+
dangerouslySetInnerHTML={{
8+
__html: `import 'https://cdn.jsdelivr.net/npm/@pwabuilder/pwaupdate';`,
9+
}}
10+
></script>,
11+
<style
12+
dangerouslySetInnerHTML={{
13+
__html: `pwa-update {
14+
display: none;
15+
}`,
16+
}}
17+
></style>,
18+
<pwa-update showOfflineToast="false"></pwa-update>,
19+
]);
20+
};

gatsby-wordpress-theme-libre/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@
5656
"gatsby-plugin-feed": "^2.4.1",
5757
"gatsby-plugin-force-trailing-slashes": "^1.0.4",
5858
"gatsby-plugin-manifest": "^2.2.48",
59-
"gatsby-plugin-offline": "^3.0.40",
6059
"gatsby-plugin-react-helmet": "^3.1.23",
6160
"gatsby-plugin-remove-generator": "^1.0.5",
61+
"gatsby-plugin-remove-serviceworker": "^1.0.0",
6262
"gatsby-plugin-sharp": "^2.4.11",
6363
"gatsby-plugin-typescript": "^2.2.3",
6464
"gatsby-source-filesystem": "^2.1.53",
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React from "react";
2+
import { Link } from "gatsby";
3+
4+
const NotFound = () => {
5+
return (
6+
<div className="error-message-container">
7+
<div className="error-message">
8+
<h2 className="error-title">Offline :(</h2>
9+
<p className="error-description">
10+
Looks like you lost your connection. Please check it and try again.
11+
</p>
12+
<Link to="/" className="error-link">
13+
Try again →
14+
</Link>
15+
</div>
16+
</div>
17+
);
18+
};
19+
20+
export default NotFound;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// This is the "Offline page" service worker
2+
3+
importScripts('https://storage.googleapis.com/workbox-cdn/releases/5.0.0/workbox-sw.js');
4+
5+
const CACHE = "libre-theme-cache";
6+
7+
// TODO: replace the following with the correct offline fallback page i.e.: const offlineFallbackPage = "offline.html";
8+
const offlineFallbackPage = "offline/index.html";
9+
10+
self.skipWaiting();
11+
// self.addEventListener("message", (event) => {
12+
// if (event.data && event.data.type === "SKIP_WAITING") {
13+
// self.skipWaiting();
14+
// }
15+
// });
16+
17+
self.addEventListener('install', async (event) => {
18+
event.waitUntil(
19+
caches.open(CACHE)
20+
.then((cache) => cache.add(offlineFallbackPage))
21+
);
22+
});
23+
24+
if (workbox.navigationPreload.isSupported()) {
25+
workbox.navigationPreload.enable();
26+
}
27+
28+
self.addEventListener('fetch', (event) => {
29+
if (event.request.mode === 'navigate') {
30+
event.respondWith((async () => {
31+
try {
32+
const preloadResp = await event.preloadResponse;
33+
34+
if (preloadResp) {
35+
return preloadResp;
36+
}
37+
38+
const networkResp = await fetch(event.request);
39+
return networkResp;
40+
} catch (error) {
41+
42+
const cache = await caches.open(CACHE);
43+
const cachedResp = await cache.match(offlineFallbackPage);
44+
return cachedResp;
45+
}
46+
})());
47+
}
48+
});

0 commit comments

Comments
 (0)