Get all the fingerprinting done!

- Add partials in the resources folder for each of the major resources of my site
- Consolidate .css and .js files via resources.Concat where possible
This commit is contained in:
Eryn Wells 2022-11-08 17:45:38 -08:00
parent d0b223fa33
commit 294fa8343b
14 changed files with 58 additions and 39 deletions

2
.gitignore vendored
View file

@ -1,5 +1,5 @@
public/
resources/
/resources/
.hugo_build.lock
*.log
*.orig

View file

@ -1,5 +1,5 @@
{{ with partial "secure_asset.html" "scripts/lib/railroad.js" }}
import rr from "{{ .Secure.RelPermalink }}";
{{ with resources.Get "scripts/lib/railroad.js" | fingerprint "sha512" }}
import rr from "{{ .RelPermalink }}";
{{ else }}
{{ errorf "Unable to get railroad.js resource" }}
{{ end }}

View file

@ -3,34 +3,30 @@
{{ partial "head.html" . }}
<body>
{{ block "body" . }}
{{ block "body" . -}}
{{ block "header" . }}{{ end }}
<main class="{{ .Type }} {{ .Kind }}{{ if gt (len .Pages) 0 }} list{{ end }}">
{{ block "main" . }}{{ end }}
</main>
{{ partial "development/page_info.html" . }}
{{ block "footer" . }}{{ end }}
{{ end }}
{{- partial "development/page_info.html" . -}}
{{- block "footer" . -}}{{- end -}}
{{- end -}}
</body>
{{- $rootCSS := resources.Get "styles/root.css"
| resources.ExecuteAsTemplate "root.css" .
| resources.Fingerprint "sha512" -}}
<link rel="stylesheet" as="style" href="{{ $rootCSS.RelPermalink }}" integrity="{{ $rootCSS.Data.Integrity }}">
{{ if not hugo.IsProduction }}
<link rel="stylesheet" as="style" href="{{ `/styles/development.css` | relURL }}">
{{ end }}
{{ with partial "secure_asset.html" (printf "styles/%s.css" .Section) }}
<link rel="stylesheet" as="style" href="{{ .Secure.RelPermalink }}" integrity="{{ .Secure.Data.Integrity }}">
{{ end }}
{{ range .Resources.Match "*.css" }}
<link rel="stylesheet" as="style" href="{{ .RelPermalink }}">
{{ end }}
{{ with partial "resources/root_css.html" . -}}
<link rel="stylesheet" as="style" href="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}">
{{- end -}}
{{- with partial "resources/section_css.html" . -}}
<link rel="stylesheet" as="style" href="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}">
{{- end -}}
{{- with partial "resources/page_css.html" . -}}
<link rel="stylesheet" as="style" href="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}">
{{- end -}}
{{ block "styles" . }}{{ end }}
{{ block "scripts" . }}{{ end }}
{{ $concatenatedSiteScript := resources.Get "scripts/site.js"
| resources.Fingerprint "sha512" }}
<script type="module" src="{{ $concatenatedSiteScript.Permalink }}" integrity="{{ $concatenatedSiteScript.Data.Integrity }}"></script>
{{- with partial "resources/site_js.html" . -}}
<script type="module" src="{{ .Permalink }}" integrity="{{ .Data.Integrity }}"></script>
{{ end }}
</html>

View file

@ -6,20 +6,10 @@
{{ partial "single_main.html" . }}
{{ end }}
{{ define "styles" }}
{{- if .HasShortcode "figures/railroad" -}}
<link rel="preload stylesheet" as="style" href="{{ `styles/railroad.css` | absURL }}">
{{- end -}}
<link rel="preload stylesheet" as="style" href="{{ `styles/monokai.css` | absURL }}">
{{ end }}
{{ define "scripts" }}
{{- if .HasShortcode "figures/railroad" -}}
{{- with partial "secure_asset.html" "scripts/lib/railroad.js" -}}
<script defer type="module" src="{{ .Secure.Permalink }}" integrity="{{ .Secure.Data.Integrity }}"></script>
{{- end -}}
{{- with resources.Get "scripts/railroad-utils.js" -}}
<script defer type="module" src="{{ .Secure.Permalink }}" integrity="{{ .Secure.Data.Integrity }}"></script>
{{- with partial "resources/railroad_utils.html" . -}}
<script defer type="module" src="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}"></script>
{{- end -}}
{{- end -}}

View file

@ -0,0 +1,5 @@
{{ $pageCSS := dict }}
{{ with .Resources.Match "*.css" }}
{{ $pageCSS = . | resources.Concat "page.css" | fingerprint "sha512" }}
{{ end }}
{{ return $pageCSS }}

View file

@ -0,0 +1,4 @@
{{- $railroadUtilsJS := resources.Get "scripts/railroad-utils.js"
| resources.ExecuteAsTemplate "scripts/railroad_utils.js" .
| resources.Fingerprint "sha512" -}}
{{ return $railroadUtilsJS }}

View file

@ -0,0 +1,11 @@
{{ $allStylesheets := slice (resources.Get "styles/root.css")
(resources.Get "styles/railroad.css")
(resources.Get "styles/monokai.css") }}
{{ if not hugo.IsProduction }}
{{ $allStylesheets = $allStylesheets | append (resources.Get "styles/development.css") }}
{{ end }}
{{ $rootCSS := $allStylesheets
| resources.Concat "styles/root.css"
| resources.ExecuteAsTemplate "styles/root.css" .
| fingerprint "sha512" }}
{{ return $rootCSS }}

View file

@ -0,0 +1,11 @@
{{ $sectionCSS := dict }}
{{ $sectionStylesheet := "" }}
{{ if .IsHome }}
{{ $sectionStylesheet = printf "styles/home.css" }}
{{ else }}
{{ $sectionStylesheet = printf "styles/%s.css" .Section }}
{{ end }}
{{ with resources.Get $sectionStylesheet }}
{{ $sectionCSS = . | fingerprint "sha512" }}
{{ end }}
{{ return $sectionCSS }}

View file

@ -0,0 +1,2 @@
{{ $siteJS := resources.Get "scripts/site.js" | fingerprint "sha512" }}
{{ return $siteJS }}

View file

@ -1,8 +1,8 @@
{{- $parentID := .Parent.Get "id" -}}
{{- $narrowOnly := .Get "narrow" }}
{{ $parentID := .Parent.Get "id" -}}
{{ $narrowOnly := not (not (.Get "narrow")) | default false }}
<script defer type="module">
import { railroadDiagram } from {{ `/scripts/railroad-utils.js` | relURL }};
import { railroadDiagram } from {{ (partial "resources/railroad_utils.html" .).RelPermalink }};
railroadDiagram(rr => {
{{ .Inner | safeJS }}
}, "{{ $parentID }}", {{ if $narrowOnly }}true{{ else }}false{{ end }});
}, "{{ $parentID }}", {{ cond $narrowOnly "true" "false" | safeJS }});
</script>