Skip to content

Commit 3c35ac6

Browse files
authored
add progressive web app functionality, improve lighthouse audit metrics (hasura#47)
1 parent 1a59754 commit 3c35ac6

File tree

18 files changed

+1244
-380
lines changed

18 files changed

+1244
-380
lines changed

Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ FROM node:buster
44
WORKDIR /app
55

66
# Install app dependencies
7+
# RUN npm -g install serve
78
RUN npm -g install gatsby-cli
89

910
COPY package*.json ./
@@ -17,4 +18,5 @@ COPY . .
1718
RUN npm run build
1819

1920
# serve on port 8080
21+
# CMD ["serve", "-l", "tcp://0.0.0.0:8080", "public"]
2022
CMD ["gatsby", "serve", "--verbose", "--prefix-paths", "-p", "8080", "--host", "0.0.0.0"]

README.md

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ We also wanted to serve these tutorials in sub paths of [learn.hasura.io](https:
1515
- GitBook style theme
1616
- Syntax Highlighting using Prism [`Bonus`: Code diff highlighting]
1717
- Search Integration with Algolia
18+
- Progressive Web App, Works Offline
1819
- Google Analytics Integration
1920
- Automatically generated sidebar navigation, table of contents, previous/next
2021
- Edit on Github
@@ -74,17 +75,45 @@ Open `config.js` for templating variables. Broadly configuration is available fo
7475
### Algolia Configuration
7576

7677
To setup Algolia, go to `config.js` and update the `search` object to look like the one below:
78+
7779
```...,
78-
"search": {
79-
"enabled": true,
80-
"indexName": "MY_INDEX_NAME",
81-
"algoliaAppId": process.env.GATSBY_ALGOLIA_APP_ID,
82-
"algoliaSearchKey": process.env.GATSBY_ALGOLIA_SEARCH_KEY,
83-
"algoliaAdminKey": process.env.ALGOLIA_ADMIN_KEY
84-
}...,
80+
"search": {
81+
"enabled": true,
82+
"indexName": "MY_INDEX_NAME",
83+
"algoliaAppId": process.env.GATSBY_ALGOLIA_APP_ID,
84+
"algoliaSearchKey": process.env.GATSBY_ALGOLIA_SEARCH_KEY,
85+
"algoliaAdminKey": process.env.ALGOLIA_ADMIN_KEY
86+
},
8587
```
88+
8689
Values for Algolia App ID, Search Key, and Admin Key can be obtained from Algolia Dashboard with the right set of permissions. Replace `MY_INDEX_NAME` with the Algolia Index name of your choice. To build the Algolia index, you need to run `npm run build` which will do a gatsby build along with content indexing in Algolia.
8790

91+
### Progressive Web App, Offline
92+
93+
To enable PWA, go to `config.js` and update the `pwa` object to look like the one below:
94+
95+
```
96+
"pwa": {
97+
"enabled": false, // disabling this will also remove the existing service worker.
98+
"manifest": {
99+
"name": "Gatsby Gitbook Starter",
100+
"short_name": "GitbookStarter",
101+
"start_url": "/",
102+
"background_color": "#6b37bf",
103+
"theme_color": "#6b37bf",
104+
"display": "standalone",
105+
"crossOrigin": "use-credentials",
106+
icons: [
107+
{
108+
src: "src/pwa-512.png",
109+
sizes: `512x512`,
110+
type: `image/png`,
111+
},
112+
],
113+
},
114+
}
115+
```
116+
88117
## Live Code Editor
89118

90119
To render react components for live editing, add the `react-live=true` to the code section. For example:

config.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ const config = {
1515
{ "text": "", "link": ""}
1616
],
1717
"search": {
18-
"enabled": true,
19-
"indexName": "learn-hasura-test",
18+
"enabled": false,
19+
"indexName": "",
2020
"algoliaAppId": process.env.GATSBY_ALGOLIA_APP_ID,
2121
"algoliaSearchKey": process.env.GATSBY_ALGOLIA_SEARCH_KEY,
2222
"algoliaAdminKey": process.env.ALGOLIA_ADMIN_KEY
@@ -40,6 +40,25 @@ const config = {
4040
"docsLocation": "https://github.com/hasura/gatsby-gitbook-boilerplate/tree/master/content",
4141
"favicon": "https://graphql-engine-cdn.hasura.io/img/hasura_icon_black.svg"
4242
},
43+
"pwa": {
44+
"enabled": false, // disabling this will also remove the existing service worker.
45+
"manifest": {
46+
"name": "Gatsby Gitbook Starter",
47+
"short_name": "GitbookStarter",
48+
"start_url": "/",
49+
"background_color": "#6b37bf",
50+
"theme_color": "#6b37bf",
51+
"display": "standalone",
52+
"crossOrigin": "use-credentials",
53+
icons: [
54+
{
55+
src: "src/pwa-512.png",
56+
sizes: `512x512`,
57+
type: `image/png`,
58+
},
59+
],
60+
},
61+
}
4362
};
4463

4564
module.exports = config;

gatsby-browser.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export const onServiceWorkerUpdateReady = () => {
2+
const answer = window.confirm(
3+
`This tutorial has been updated. ` +
4+
`Reload to display the latest version?`
5+
)
6+
if (answer === true) {
7+
window.location.reload()
8+
}
9+
}

gatsby-config.js

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ const plugins = [
1111
}
1212
},
1313
'gatsby-plugin-emotion',
14+
'gatsby-plugin-remove-trailing-slashes',
15+
'gatsby-plugin-react-helmet',
16+
{
17+
resolve: "gatsby-source-filesystem",
18+
options: {
19+
name: "docs",
20+
path: `${__dirname}/content/`
21+
}
22+
},
1423
{
1524
resolve: 'gatsby-plugin-mdx',
1625
options: {
@@ -29,15 +38,6 @@ const plugins = [
2938
extensions: [".mdx", ".md"]
3039
}
3140
},
32-
'gatsby-plugin-remove-trailing-slashes',
33-
'gatsby-plugin-react-helmet',
34-
{
35-
resolve: "gatsby-source-filesystem",
36-
options: {
37-
name: "docs",
38-
path: `${__dirname}/content/`
39-
}
40-
},
4141
{
4242
resolve: `gatsby-plugin-gtag`,
4343
options: {
@@ -50,6 +50,7 @@ const plugins = [
5050
},
5151
},
5252
];
53+
// check and add algolia
5354
if (config.header.search && config.header.search.enabled && config.header.search.algoliaAppId && config.header.search.algoliaAdminKey) {
5455
plugins.push({
5556
resolve: `gatsby-plugin-algolia`,
@@ -61,6 +62,21 @@ if (config.header.search && config.header.search.enabled && config.header.search
6162
}}
6263
)
6364
}
65+
// check and add pwa functionality
66+
if (config.pwa && config.pwa.enabled && config.pwa.manifest) {
67+
plugins.push({
68+
resolve: `gatsby-plugin-manifest`,
69+
options: {...config.pwa.manifest},
70+
});
71+
plugins.push({
72+
resolve: 'gatsby-plugin-offline',
73+
options: {
74+
appendScript: require.resolve(`./src/custom-sw-code.js`),
75+
},
76+
});
77+
} else {
78+
plugins.push('gatsby-plugin-remove-serviceworker');
79+
}
6480
module.exports = {
6581
pathPrefix: config.gatsby.pathPrefix,
6682
siteMetadata: {

gatsby-node.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ exports.onCreateWebpackConfig = ({ actions }) => {
4949
actions.setWebpackConfig({
5050
resolve: {
5151
modules: [path.resolve(__dirname, "src"), "node_modules"],
52-
alias: { $components: path.resolve(__dirname, "src/components") }
52+
alias: {
53+
$components: path.resolve(__dirname, "src/components"),
54+
buble: '@philpl/buble' // to reduce bundle size
55+
}
5356
}
5457
});
5558
};

0 commit comments

Comments
 (0)