feat: Making Quartz available offline by making it a PWA (#465)

* Adding PWA and chaching for offline aviability

* renamed workbox config to fit Quartz' scheme

* Documenting new configuration

* Added missig umami documentation

* Fixed formatting so the build passes, thank you prettier :)

* specified caching strategies to improve performance

* formatting...

* fixing "404 manifest.json not found" on subdirectories by adding a / to manifestpath

* turning it into a plugin

* Removed Workbox-cli and updated @types/node

* Added Serviceworkercode to offline.ts

* formatting

* Removing workbox from docs

* applied suggestions

* Removed path.join for sw path

Co-authored-by: Jacky Zhao <j.zhao2k19@gmail.com>

* Removed path.join for manifest path

Co-authored-by: Jacky Zhao <j.zhao2k19@gmail.com>

* Removing path module import

* Added absolute path to manifests start_url and manifest "import" using baseUrl

* Adding protocol to baseurl

Co-authored-by: Jacky Zhao <j.zhao2k19@gmail.com>

* Adding protocol to start_url too then

* formatting...

* Adding fallback page

* Documenting offline plugin

* formatting...

* merge suggestion

Co-authored-by: Jacky Zhao <j.zhao2k19@gmail.com>

* merge suggestion

Co-authored-by: Jacky Zhao <j.zhao2k19@gmail.com>

* merge suggestion

Co-authored-by: Jacky Zhao <j.zhao2k19@gmail.com>

* merge suggestion

Co-authored-by: Jacky Zhao <j.zhao2k19@gmail.com>

* merge suggestion

Co-authored-by: Jacky Zhao <j.zhao2k19@gmail.com>

* merge suggestion

Co-authored-by: Jacky Zhao <j.zhao2k19@gmail.com>

* merge suggestion

Co-authored-by: Jacky Zhao <j.zhao2k19@gmail.com>

* merge suggestion

Co-authored-by: Jacky Zhao <j.zhao2k19@gmail.com>

* merge suggestion

Co-authored-by: Jacky Zhao <j.zhao2k19@gmail.com>

* merge suggestion

Co-authored-by: Jacky Zhao <j.zhao2k19@gmail.com>

* merge suggestion

Co-authored-by: Jacky Zhao <j.zhao2k19@gmail.com>

* merge suggestion

Co-authored-by: Jacky Zhao <j.zhao2k19@gmail.com>

* formatting...

* Fixing manifest path, all these nits hiding the actual issues .-.

* Offline fallback page through plugins, most things taken from 404 Plugin

* adding Offline Plugin to config

* formatting...

* Turned offline off as default and removed offline.md

---------

Co-authored-by: Jacky Zhao <j.zhao2k19@gmail.com>
This commit is contained in:
Adam Brangenberg 2023-09-20 20:38:13 +02:00 committed by GitHub
parent ae57715822
commit 9c322802fa
13 changed files with 235 additions and 6 deletions

View File

@ -21,10 +21,12 @@ const config: QuartzConfig = {
This part of the configuration concerns anything that can affect the whole site. The following is a list breaking down all the things you can configure:
- `pageTitle`: title of the site. This is also used when generating the [[RSS Feed]] for your site.
- `description`: description of the site. This will be used when someone installs your site as an App.
- `enableSPA`: whether to enable [[SPA Routing]] on your site.
- `enablePopovers`: whether to enable [[popover previews]] on your site.
- `analytics`: what to use for analytics on your site. Values can be
- `null`: don't use analytics;
- `{ provider: "umami", websiteId: <your-umami-id> }`: easy, privacy-friendly, open source, GDPR Compliant analytics;
- `{ provider: 'plausible' }`: use [Plausible](https://plausible.io/), a privacy-friendly alternative to Google Analytics; or
- `{ provider: 'google', tagId: <your-google-tag> }`: use Google Analytics
- `baseUrl`: this is used for sitemaps and RSS feeds that require an absolute URL to know where the canonical 'home' of your site lives. This is normally the deployed URL of your site (e.g. `quartz.jzhao.xyz` for this site). Do not include the protocol (i.e. `https://`) or any leading or trailing slashes.

View File

@ -0,0 +1,31 @@
---
title: "Offline Access (PWA)"
tags:
- plugin/emitter
---
This plugin allows your website to be accessible offline and be installed as an app. You can use it by adding `Plugin.Offline(),` to the `emitters` in `quartz.config.ts`
## Offline Capability
Whenever you visit a page it gets cached for offline use. Depending on the kind of content, the process for caching is diffent:
- **Pages** (HTML, your converted Markdown files): Quartz first tries to get them over the Network. If that fails, your browser attempts to fetch it from the cache.
- **Static Resources** (Fonts, CSS Styling, JavaScript): Quartz uses cached resources by default and updates the cache over the network in the background.
- **Images**: Images are saved once and then served from cache. Quartz uses a limited cache of 60 images and images remain in the cache for 30 days
You can edit the fallback page by changing the `offline.md` file in the root of your `content` directory
## Progressive Web App (PWA)
Progressive Web Apps can have [many properties](https://developer.mozilla.org/en-US/docs/Web/Manifest). We're only going to mention the ones Quartz supports by default, however you can edit the offline plugins file to add more in case required.
- **icons**: the `icon.svg` file in the `quartz/static` directory is used for all the icons. This makes it easier to scale the image since you don't need to provide an png for every size
- **name**, **short_name**: Uses the `pageTitle` configured in `quartz.config.ts`
- **description**: Uses the `description` configured in `quartz.config.ts`
- **background_color**, **theme_color**: Uses the `lightMode.light` color configured in `quartz.config.ts`.
- **start_url**: Uses the `baseUrl` configured in `quartz.config.ts`
### Default values
- **display**: this is set to `minimal-ui`

View File

@ -30,7 +30,7 @@ This will guide you through initializing your Quartz with content. Once you've d
## 🔧 Features
- [[Obsidian compatibility]], [[full-text search]], [[graph view]], note transclusion, [[wikilinks]], [[backlinks]], [[Latex]], [[syntax highlighting]], [[popover previews]], and [many more](./features) right out of the box
- [[Obsidian compatibility]], [[full-text search]], [[graph view]], note transclusion, [[wikilinks]], [[backlinks]], [[Latex]], [[syntax highlighting]], [[popover previews]], [[offline access]] and [many more](./features) right out of the box
- Hot-reload for both configuration and content
- Simple JSX layouts and [[creating components|page components]]
- [[SPA Routing|Ridiculously fast page loads]] and tiny bundle sizes

9
package-lock.json generated
View File

@ -73,7 +73,7 @@
"@types/flexsearch": "^0.7.3",
"@types/hast": "^2.3.4",
"@types/js-yaml": "^4.0.5",
"@types/node": "^20.1.2",
"@types/node": "^20.6.2",
"@types/pretty-time": "^1.1.2",
"@types/source-map-support": "^0.5.6",
"@types/workerpool": "^6.4.0",
@ -113,6 +113,7 @@
},
"node_modules/@clack/prompts/node_modules/is-unicode-supported": {
"version": "1.3.0",
"extraneous": true,
"inBundle": true,
"license": "MIT",
"engines": {
@ -1463,9 +1464,9 @@
}
},
"node_modules/@types/node": {
"version": "20.3.3",
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.3.3.tgz",
"integrity": "sha512-wheIYdr4NYML61AjC8MKj/2jrR/kDQri/CIpVoZwldwhnIrD/j9jIU5bJ8yBKuB2VhpFV7Ab6G2XkBjv9r9Zzw==",
"version": "20.6.2",
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.6.2.tgz",
"integrity": "sha512-Y+/1vGBHV/cYk6OI1Na/LHzwnlNCAfU3ZNGrc1LdRe/LAIbdDPTTv/HU3M7yXN448aTVDq3eKRm2cg7iKLb8gw==",
"dev": true
},
"node_modules/@types/parse5": {

View File

@ -94,7 +94,7 @@
"@types/flexsearch": "^0.7.3",
"@types/hast": "^2.3.4",
"@types/js-yaml": "^4.0.5",
"@types/node": "^20.1.2",
"@types/node": "^20.6.2",
"@types/pretty-time": "^1.1.2",
"@types/source-map-support": "^0.5.6",
"@types/workerpool": "^6.4.0",

View File

@ -4,6 +4,7 @@ import * as Plugin from "./quartz/plugins"
const config: QuartzConfig = {
configuration: {
pageTitle: "🪴 Quartz 4.0",
description: "Quartz Documentation Page and Demo",
enableSPA: true,
enablePopovers: true,
analytics: {

View File

@ -19,6 +19,7 @@ export type Analytics =
export interface GlobalConfiguration {
pageTitle: string
description: string
/** Whether to enable single-page-app style rendering. this prevents flashes of unstyled content and improves smoothness of Quartz */
enableSPA: boolean
/** Whether to display Wikipedia-style popovers when hovering over links */

View File

@ -14,6 +14,8 @@ export default (() => {
const iconPath = joinSegments(baseDir, "static/icon.png")
const ogImagePath = `https://${cfg.baseUrl}/static/og-image.png`
const manifest =
cfg.baseUrl == undefined ? "/manifest.json" : `https://${cfg.baseUrl}/manifest.json`
return (
<head>
@ -25,7 +27,9 @@ export default (() => {
{cfg.baseUrl && <meta property="og:image" content={ogImagePath} />}
<meta property="og:width" content="1200" />
<meta property="og:height" content="675" />
<meta name="theme-color" content="#faf8f8" />
<link rel="icon" href={iconPath} />
<link rel="manifest" href={manifest} />
<meta name="description" content={description} />
<meta name="generator" content="Quartz" />
<link rel="preconnect" href="https://fonts.googleapis.com" />

View File

@ -0,0 +1,12 @@
import { QuartzComponentConstructor } from "../types"
function OfflineFallbackPage() {
return (
<article class="popover-hint">
<h1>Offline</h1>
<p>This page isn't offline available yet.</p>
</article>
)
}
export default (() => OfflineFallbackPage) satisfies QuartzComponentConstructor

View File

@ -116,6 +116,11 @@ function addGlobalPageResources(
document.dispatchEvent(event)`)
}
componentResources.afterDOMLoaded.push(`
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/sw.js');
}`)
let wsUrl = `ws://localhost:${ctx.argv.wsPort}`
if (ctx.argv.remoteDevHost) {

View File

@ -7,3 +7,4 @@ export { Assets } from "./assets"
export { Static } from "./static"
export { ComponentResources } from "./componentResources"
export { NotFoundPage } from "./404"
export { Offline } from "./offline"

View File

@ -0,0 +1,97 @@
import { QuartzEmitterPlugin } from "../types"
import { FilePath, FullSlug } from "../../util/path"
import { FullPageLayout } from "../../cfg"
import { sharedPageComponents } from "../../../quartz.layout"
import OfflineFallbackPage from "../../components/pages/OfflineFallbackPage"
import BodyConstructor from "../../components/Body"
import { pageResources, renderPage } from "../../components/renderPage"
import { defaultProcessedContent } from "../vfile"
import { QuartzComponentProps } from "../../components/types"
export const Offline: QuartzEmitterPlugin = () => {
const opts: FullPageLayout = {
...sharedPageComponents,
pageBody: OfflineFallbackPage(),
beforeBody: [],
left: [],
right: [],
}
const { head: Head, pageBody, footer: Footer } = opts
const Body = BodyConstructor()
return {
name: "OfflineSupport",
getQuartzComponents() {
return [Head, Body, pageBody, Footer]
},
async emit({ cfg }, _content, resources, emit): Promise<FilePath[]> {
const manifest = {
short_name: cfg.configuration.pageTitle,
name: cfg.configuration.pageTitle,
description: cfg.configuration.description,
background_color: cfg.configuration.theme.colors.lightMode.light,
theme_color: cfg.configuration.theme.colors.lightMode.light,
display: "minimal-ui",
icons: [
{
src: "static/icon.svg",
sizes: "any",
purpose: "maskable",
},
{
src: "static/icon.svg",
sizes: "any",
purpose: "any",
},
],
start_url:
cfg.configuration.baseUrl == undefined ? "/" : `https://${cfg.configuration.baseUrl}/`,
}
const serviceWorker =
"importScripts('https://storage.googleapis.com/workbox-cdn/releases/6.4.1/workbox-sw.js');" +
"const {pageCache, imageCache, staticResourceCache, googleFontsCache, offlineFallback} = workbox.recipes;" +
"pageCache(); googleFontsCache(); staticResourceCache(); imageCache(); offlineFallback();"
const slug = "offline" as FullSlug
const url = new URL(`https://${cfg.configuration.baseUrl ?? "example.com"}`)
const path = url.pathname as FullSlug
const externalResources = pageResources(path, resources)
const [tree, vfile] = defaultProcessedContent({
slug,
text: "Offline",
description: "This page isn't offline available yet.",
frontmatter: { title: "Offline", tags: [] },
})
const componentData: QuartzComponentProps = {
fileData: vfile.data,
externalResources,
cfg: cfg.configuration,
children: [],
tree,
allFiles: [],
}
return Promise.all([
emit({
content: JSON.stringify(manifest),
slug: "manifest" as FullSlug,
ext: ".json",
}),
emit({
content: serviceWorker,
slug: "sw" as FullSlug,
ext: ".js",
}),
emit({
content: renderPage(slug, componentData, opts, externalResources),
slug,
ext: ".html",
}),
])
},
}
}

74
quartz/static/icon.svg Normal file
View File

@ -0,0 +1,74 @@
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg version="1.0" xmlns="http://www.w3.org/2000/svg"
width="200.000000pt" height="200.000000pt" viewBox="0 0 200.000000 200.000000"
preserveAspectRatio="xMidYMid meet">
<g transform="translate(0.000000,200.000000) scale(0.100000,-0.100000)"
fill="#000000" stroke="none">
<path d="M990 1852 c-30 -20 -147 -97 -260 -171 -113 -74 -207 -136 -209 -138
-2 -1 20 -93 49 -203 29 -110 51 -202 49 -204 -2 -2 -35 3 -72 11 -37 8 -69
14 -71 12 -1 -2 -24 -66 -50 -141 l-47 -137 124 -175 125 -174 -40 -66 -40
-67 26 -32 c13 -18 36 -45 49 -62 20 -24 23 -34 16 -60 -15 -50 -13 -52 74
-65 54 -8 89 -19 103 -32 21 -19 26 -19 126 -5 97 13 111 13 192 -4 l86 -19
57 28 c52 27 58 33 77 87 20 56 24 60 90 96 60 32 74 46 103 96 18 32 33 61
33 65 0 3 -37 20 -82 36 -67 24 -82 34 -80 49 2 10 47 154 99 321 l94 303 -46
138 -46 138 -89 47 c-89 47 -90 48 -90 85 0 36 -6 43 -142 159 -79 67 -145
122 -148 121 -3 0 -30 -17 -60 -37z m60 0 c0 -4 18 -91 40 -192 22 -101 40
-192 40 -201 0 -11 -15 -22 -42 -31 l-43 -15 -5 -202 -5 -202 -84 -55 c-46
-30 -86 -53 -88 -51 -2 3 -58 522 -75 710 -3 24 13 41 122 137 120 105 140
119 140 102z m163 -121 c96 -83 107 -96 107 -126 l0 -33 -90 -22 c-50 -12 -92
-21 -94 -19 -1 2 -15 65 -30 139 -15 74 -30 146 -33 160 -7 29 -21 39 140 -99z
m-333 16 c-20 -17 -53 -47 -75 -66 l-39 -34 38 -373 c21 -205 36 -380 33 -388
-10 -25 -21 -20 -89 38 l-63 54 -75 277 c-62 232 -72 278 -60 285 8 5 92 61
185 124 94 63 172 115 175 115 3 0 -11 -14 -30 -32z m423 -279 c-8 -46 -16
-84 -18 -87 -6 -5 -169 21 -178 29 -5 4 4 11 19 14 24 6 26 10 21 46 -5 39 -4
40 31 49 21 5 53 14 72 19 70 20 69 22 53 -70z m122 39 l80 -43 37 -115 c21
-62 38 -117 38 -121 0 -12 -29 -9 -124 15 l-90 22 -31 52 -32 53 16 90 c8 49
18 90 21 90 3 0 41 -20 85 -43z m-124 -168 c8 -12 24 -36 34 -55 l18 -34 -57
-203 c-32 -111 -61 -205 -65 -210 -4 -4 -20 25 -37 64 -28 69 -31 72 -81 93
l-53 21 0 191 0 191 113 -19 c87 -15 116 -24 128 -39z m184 -121 c55 -12 101
-22 103 -23 4 -3 -189 -619 -197 -627 -7 -8 -102 -28 -131 -28 -17 0 -17 4 2
67 l21 68 -22 60 -21 60 60 210 c72 249 67 235 77 235 4 0 53 -10 108 -22z
m-901 -171 c50 -45 119 -107 154 -136 34 -30 62 -57 62 -60 -1 -3 -19 -18 -41
-33 -34 -24 -49 -28 -110 -28 -66 0 -79 4 -161 47 -49 26 -87 52 -85 57 2 6
21 62 42 124 21 61 40 112 43 112 3 0 46 -37 96 -83z m41 62 c11 -11 36 -91
30 -98 -3 -2 -34 24 -70 59 l-66 62 48 -7 c26 -3 52 -11 58 -16z m478 -129
l46 -20 -36 -61 c-32 -53 -53 -73 -182 -163 l-145 -103 -56 15 c-30 8 -59 18
-64 23 -13 12 59 99 122 148 52 39 260 181 267 181 1 0 23 -9 48 -20z m111
-170 c25 -63 46 -121 46 -129 0 -24 -64 -251 -70 -251 -4 0 -27 90 -53 200
l-46 200 32 54 c18 30 35 52 39 48 3 -4 27 -59 52 -122z m-605 -85 c18 -25 29
-49 26 -55 -3 -5 5 -47 19 -92 18 -59 21 -77 10 -63 -37 47 -234 324 -234 328
0 2 33 -13 73 -34 53 -27 82 -51 106 -84z m471 80 c0 -3 20 -92 45 -197 25
-106 45 -200 45 -210 0 -16 -12 -18 -139 -18 -101 0 -142 3 -148 13 -4 6 -25
59 -45 117 -28 82 -34 108 -25 117 18 19 251 182 260 183 4 0 7 -2 7 -5z
m-397 -64 c-24 -39 -33 -39 -62 -2 l-24 31 53 0 52 0 -19 -29z m61 -115 c37
-11 39 -14 72 -106 19 -52 37 -103 39 -112 5 -14 -5 -12 -67 11 -81 31 -80 31
-116 158 l-20 73 26 -7 c15 -3 44 -11 66 -17z m642 -118 c-3 -17 -8 -33 -10
-35 -2 -3 -27 6 -56 18 -54 23 -55 35 -5 42 76 11 78 10 71 -25z m89 10 c64
-24 59 -33 -23 -44 -48 -6 -52 -5 -52 14 0 20 9 52 15 52 2 0 29 -10 60 -22z
m-814 -30 c27 -33 23 -43 -20 -56 -59 -18 -62 -15 -33 33 15 25 28 45 30 45 2
0 12 -10 23 -22z m647 -9 c60 -25 60 -25 -42 -64 l-64 -24 16 60 c9 33 21 58
27 55 5 -2 34 -14 63 -27z m228 -36 c-31 -58 -30 -57 -75 -28 -23 16 -40 29
-38 31 5 5 122 32 125 29 2 -2 -3 -16 -12 -32z m-863 -74 c-10 -29 -19 -56
-21 -59 -5 -6 -72 70 -72 80 0 7 101 40 107 36 1 -1 -5 -27 -14 -57z m176 5
c3 -3 1 -8 -5 -12 -6 -4 -49 -35 -96 -69 -47 -35 -88 -63 -91 -63 -4 0 34 153
48 188 3 9 21 6 72 -13 37 -14 69 -28 72 -31z m520 24 c-1 -18 -4 -45 -8 -60
l-6 -27 -62 28 c-35 15 -65 30 -68 33 -6 6 111 57 133 58 7 0 12 -12 11 -32z
m72 10 c22 -12 39 -25 39 -29 0 -10 -90 -62 -97 -56 -6 7 7 107 14 107 3 0 23
-10 44 -22z m-346 -58 c-22 -10 -63 -28 -92 -39 l-52 -20 -26 33 c-14 18 -25
36 -25 39 0 4 53 7 118 6 l117 -1 -40 -18z m-198 -17 c36 -47 36 -51 -14 -87
-27 -20 -51 -36 -54 -36 -4 0 5 32 19 70 15 39 28 70 31 70 2 0 10 -8 18 -17z
m256 4 c-61 -97 -96 -147 -102 -147 -4 0 -26 19 -49 43 l-43 43 88 36 c97 40
118 45 106 25z m134 -14 c32 -15 59 -28 61 -29 2 -1 -2 -22 -8 -45 l-12 -44
-24 30 c-42 52 -85 115 -80 115 3 0 32 -12 63 -27z m-464 -62 l-27 -73 -60 7
c-59 6 -74 11 -65 19 28 24 173 126 175 123 2 -2 -8 -36 -23 -76z m371 -12 c4
-45 10 -88 13 -96 6 -16 -16 -14 -100 11 l-38 11 53 82 c29 45 55 80 59 77 3
-2 10 -40 13 -85z m116 -59 c0 -7 -65 -40 -79 -40 -5 0 -12 30 -15 68 -4 37
-9 81 -12 97 -5 25 3 18 50 -44 31 -41 56 -77 56 -81z m-317 25 c21 -25 32
-45 25 -45 -7 0 -42 -5 -78 -10 -35 -5 -66 -7 -68 -5 -6 5 68 105 77 105 4 0
24 -20 44 -45z m-124 -24 c-21 -28 -49 -42 -49 -23 0 8 63 62 68 57 2 -2 -6
-17 -19 -34z"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 5.0 KiB