Skip to content

Commit

Permalink
feat: add PWA support
Browse files Browse the repository at this point in the history
  • Loading branch information
tomowang committed Jul 17, 2024
1 parent ea815ba commit 2a0d54f
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Check [https://hugo-theme-tailwind.tomo.dev/](https://hugo-theme-tailwind.tomo.d
* Search (by using [fuse.js](https://fusejs.io/) and [mark.js](https://markjs.io/))
* Image support for article (using `image` param in front matter)
* Structured data [JSON-LD](https://json-ld.org/) for article (enable `jsonLD` in config file or front matter)
* Simple [PWA](https://web.dev/add-manifest/) support - check `params.pwa` in config file

## Installation

Expand Down Expand Up @@ -102,6 +103,7 @@ Some of the configuration options are:
* `params.search`: search settings
* `params.author`: author used in the JSON-LD
* `params.jsonLD`: enable or disable JSON-LD (default disabled)
* `params.pwa`: enable or disable PWA (default disabled)

For social media link data, you can refer entries in `params.social_media.items`
(You can add more or disable existing entries in `params.social_media.items`).
Expand Down
Binary file added assets/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions config/_default/params.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,14 @@ jsonLD = false
title = 'Github'
icon = 'brand-github'
link = 'https://github.com/tomowang'

[pwa]
enabled = true
icon = "logo.png" # should be in assets
icon_sizes = [192, 512]

# will be used as the manifest.json file and merge the default one
# https://developer.mozilla.org/en-US/docs/Web/Manifest
# https://web.dev/add-manifest/
[pwa.manifest]
description = "Example site for hugo-theme-tailwind"
6 changes: 6 additions & 0 deletions content/manifest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
+++
layout = "manifest"
outputs = ["json"]
url = "/manifest.json"
sitemap = false
+++
11 changes: 11 additions & 0 deletions exampleSite/config/_default/params.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,14 @@ jsonLD = true
title = 'Github'
icon = 'brand-github'
link = 'https://github.com/tomowang'

[pwa]
enabled = true
icon = "logo.png" # should be in assets
icon_sizes = [192, 512]

# will be used as the manifest.json file and merge the default one
# https://developer.mozilla.org/en-US/docs/Web/Manifest
# https://web.dev/add-manifest/
[manifest]
description = "Example site for hugo-theme-tailwind"
26 changes: 26 additions & 0 deletions layouts/_default/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{{- $baseURL := absURL site.BaseURL }}
{{- $iconPath := site.Params.pwa.icon }}
{{- $icon := resources.Get $iconPath }}
{{- $icons := slice }}
{{- with $icon }}
{{- range site.Params.pwa.icon_sizes }}
{{- $sizes := printf "%dx%d" . . }}
{{- $icons = $icons | append (dict
"src" ($icon.Resize $sizes).Permalink
"sizes" $sizes
"type" $icon.MediaType.Type
)
}}
{{- end }}
{{- else }}
{{- warnf "[pwa] the icon image doesn't exist: assets/%s." $iconPath }}
{{- end }}
{{- $default := dict
"name" site.Title
"display" "standalone"
"scope" $baseURL
"start_url" $baseURL
"icons" $icons
}}
{{- $params := site.Params.pwa.manifest }}
{{- merge $default $params | jsonify -}}
1 change: 1 addition & 0 deletions layouts/partials/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
content="{{ with .Params.Keywords }}{{ delimit . ", " }}{{ else }}{{ $.Site.Params.Keywords }}{{ end }}"
/>
<meta name="robots" content="noodp" />
{{- if $.Site.Params.pwa.enabled }}<link rel="manifest" href="/manifest.json" />{{ end -}}
{{ template "_internal/opengraph.html" . }}
{{ template "_internal/twitter_cards.html" . }}

Expand Down

0 comments on commit 2a0d54f

Please sign in to comment.