Adam Brangenberg
2023-09-20 d6301fae90d9f922618bf0f413e273156731eef7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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",
        }),
      ])
    },
  }
}