Implement fingerprinting site and page resources

This will keep browsers from caching files that have changed. I've also added
SRI information to the <script> and <link> elements for browsers to verify
resources.

Squashed commit of the following:

commit f3fcb16388
Author: Eryn Wells <eryn@erynwells.me>
Date:   Tue Nov 8 17:47:42 2022 -0800

    Remove static/styles/root.css

commit 53a30624a0
Author: Eryn Wells <eryn@erynwells.me>
Date:   Tue Nov 8 17:47:26 2022 -0800

    Add back the basic table styles that got ditched when root.css was moved to assets/styles

commit 294fa8343b
Author: Eryn Wells <eryn@erynwells.me>
Date:   Tue Nov 8 17:45:38 2022 -0800

    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

commit d0b223fa33
Author: Eryn Wells <eryn@erynwells.me>
Date:   Mon Nov 7 08:36:39 2022 -0800

    All the template updates for fingerprinting

commit 1751abadac
Author: Eryn Wells <eryn@erynwells.me>
Date:   Mon Nov 7 08:36:27 2022 -0800

    Add secure_asset.html template

commit 94ea8068c9
Author: Eryn Wells <eryn@erynwells.me>
Date:   Mon Nov 7 08:35:48 2022 -0800

    Move a bunch of scripts and CSS to assets so they can be processed with Hugo Pipes

    Use Pipes to fingerprint and add SLI information to <script> and <link> tags.
This commit is contained in:
Eryn Wells 2022-11-08 17:50:11 -08:00
parent f8174ab27c
commit 9601e1e283
24 changed files with 106327 additions and 55 deletions

2
.gitignore vendored
View file

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

106247
assets/scripts/lib/p5-1.5.0.js Normal file

File diff suppressed because one or more lines are too long

View file

@ -1,4 +1,8 @@
import rr from "./railroad.js";
{{ with resources.Get "scripts/lib/railroad.js" | fingerprint "sha512" }}
import rr from "{{ .RelPermalink }}";
{{ else }}
{{ errorf "Unable to get railroad.js resource" }}
{{ end }}
class RailroadDiagramManager {
constructor() {

View file

@ -93,6 +93,14 @@
}
}
@font-face {
font-family: "Museo_Slab";
src: url("{{ `/fonts/Museo_Slab_500.woff2` | relURL }}") format("woff2"),
url("{{ `/fonts/Museo_Slab_500.woff` | relURL }}") format("woff");
font-weight: normal;
font-style: normal;
}
@layer reset {
body, button, h1, h2, h3, h4, h5, h6, input, ol, ul, p, pre, textarea {
padding: 0; margin: 0;

View file

@ -3,50 +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>
<style>
@font-face {
font-family: "Museo_Slab";
src: url("{{ `/fonts/Museo_Slab_500.woff2` | relURL }}") format("woff2"),
url("{{ `/fonts/Museo_Slab_500.woff` | relURL }}") format("woff");
font-weight: normal;
font-style: normal;
}
</style>
<link rel="stylesheet" as="style" href="{{ `/styles/root.css` | relURL }}">
{{- if not hugo.IsProduction -}}
<link rel="stylesheet" as="style" href="{{ `/styles/development.css` | relURL }}">
{{ with partial "resources/root_css.html" . -}}
<link rel="stylesheet" as="style" href="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}">
{{- end -}}
{{- $includedCSS := slice -}}
{{- if not .FirstSection.IsHome -}}
{{- range .FirstSection.Resources.Match "*.css" -}}
{{- if not (in $includedCSS .Permalink) -}}
{{- $includedCSS = $includedCSS | append .Permalink -}}
<link rel="stylesheet" as="style" href="{{ .RelPermalink }}">
{{- end -}}
{{- end -}}
{{- end -}}
{{- range .Resources.Match "*.css" -}}
{{- if not (in $includedCSS .Permalink) -}}
{{- $includedCSS = $includedCSS | append .Permalink -}}
<link rel="stylesheet" as="style" href="{{ .RelPermalink }}">
{{- 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 }}
<script src="{{ `scripts/site.js` | relURL }}"></script>
{{- with partial "resources/site_js.html" . -}}
<script type="module" src="{{ .Permalink }}" integrity="{{ .Data.Integrity }}"></script>
{{ end }}
</html>

View file

@ -6,22 +6,20 @@
{{ 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" -}}
<script defer type="module" src="{{ `scripts/railroad.js` | absURL }}"></script>
<script defer type="module" src="{{ `scripts/railroad-utils.js` | absURL }}"></script>
{{- with partial "resources/railroad_utils.html" . -}}
<script defer type="module" src="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}"></script>
{{- end -}}
{{- end -}}
{{- if .HasShortcode "figures/p5" -}}
<script defer src="{{ `scripts/p5-1.4.1.min.js` | absURL }}"></script>
<script defer src="{{ `scripts/sketch-utils.js` | absURL }}"></script>
{{- with partial "secure_asset.html" "scripts/lib/p5-1.5.0.js" -}}
<script defer src="{{ .Secure.Permalink }}" integrity="{{ .Secure.Data.Integrity }}"></script>
{{- end -}}
{{- with partial "secure_asset.html" "scripts/sketch-utils.js" -}}
<script defer src="{{ .Secure.Permalink }}" integrity="{{ .Secure.Data.Integrity }}"></script>
{{- end -}}
{{- end -}}
{{- range $script := .Resources.Match "*.js" -}}

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

@ -0,0 +1,5 @@
{{ $asset := dict }}
{{ with resources.Get . }}
{{ $asset = dict "Resource" . "Secure" (. | resources.Fingerprint "sha512") }}
{{ end }}
{{ return $asset }}

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>

File diff suppressed because one or more lines are too long