forked from jackyzha0/quartz
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Making Quartz available offline by making it a PWA (jackyzha0#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>
- Loading branch information
1 parent
68537a3
commit d3fa6f8
Showing
13 changed files
with
235 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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", | ||
}), | ||
]) | ||
}, | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.