Skip to content

Commit

Permalink
Fingerprint and minify all resources, move custom css and js to asset…
Browse files Browse the repository at this point in the history
…s dir
  • Loading branch information
Mitrichius committed Nov 4, 2020
1 parent 4c1ba11 commit e9ee82e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ params:
images:
- images/og-featured.png # relative path to "static" directory
customCSS:
- css/my.css # relative path to "static" directory
- css/my.css # relative path to "assets" or "static" directory
customJS:
- js/main.js # relative path to "static" directory
- js/main.js # relative path to "assets" or "static" directory
dateFormat: "2006-01-02"
paginationSinglePost: true
style: light-without-switcher
Expand Down
12 changes: 3 additions & 9 deletions layouts/partials/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,14 @@
<link rel="icon" type="image/x-icon" href="{{ "favicon.ico" | absURL }}">
<link rel="apple-touch-icon-precomposed" href="{{ "favicon.png" | absURL }}">

{{ $cssTemplate := resources.Get "css/main.css" }}
{{ $style := $cssTemplate | resources.ExecuteAsTemplate "css/style.css" . | resources.ToCSS }}
{{ if eq (getenv "HUGO_ENV") "production" | or (eq .Site.Params.env "production") }}
{{ $style = $style | resources.Minify }}
{{ end }}
{{ $style = $style | resources.Fingerprint "sha256" }}
<link rel="stylesheet" href="{{ $style.Permalink }}" integrity="{{ $style.Data.Integrity }}">
{{ partial "resource.html" (dict "context" . "type" "css" "filename" "css/main.css") }}

{{ range .Site.Params.customCSS -}}
<link rel="stylesheet" href="{{ . | absURL }}?rnd={{ now.Unix }}">
{{ partial "resource.html" (dict "context" $ "type" "css" "filename" . ) }}
{{- end }}

{{ range .Site.Params.customJS -}}
<script src="{{ . | absURL }}?rnd={{ now.Unix }}" type="text/javascript" charset="utf-8"></script>
{{ partial "resource.html" (dict "context" $ "type" "js" "filename" . ) }}
{{- end }}

{{ template "_internal/opengraph.html" . }}
Expand Down
29 changes: 29 additions & 0 deletions layouts/partials/resource.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{{ $targetFilename := "file" }}
{{ if eq .type "css" }}
{{ $targetFilename = "css/style.css"}}
{{ else if eq .type "js" }}
{{ $targetFilename = "js/script.js"}}
{{ end }}

{{ $resource := resources.Get .filename }}

{{ if $resource }}
{{ $resource := $resource | resources.ExecuteAsTemplate $targetFilename . }}
{{ if eq (getenv "HUGO_ENV") "production" | or (eq .Site.Params.env "production") }}
{{ $resource = $resource | resources.Minify }}
{{ end }}
{{ $resource = $resource | resources.Fingerprint "sha256" }}

{{ if eq .type "css" }}
<link rel="stylesheet" href="{{ $resource.Permalink }}" integrity="{{ $resource.Data.Integrity }}">
{{ else if eq .type "js" }}
<script src="{{ $resource.Permalink }}" type="text/javascript" charset="utf-8" integrity="{{ $resource.Data.Integrity }}"></script>
{{ end }}
<!-- For backward compatibility -->
{{ else }}
{{ if eq .type "css" }}
<link rel="stylesheet" href="{{ .filename | absURL }}?rnd={{ now.Unix }}">
{{ else if eq .type "js" }}
<script src="{{ .filename | absURL }}?rnd={{ now.Unix }}" type="text/javascript" charset="utf-8"></script>
{{ end }}
{{ end }}

0 comments on commit e9ee82e

Please sign in to comment.