Skip to content

Commit 3fdd2be

Browse files
committed
refactor: head.html, style.html: Implement CSS preloading
The main CSS is now preloaded in `<head>` for faster rendering. The style.html partial now uses the preloaded CSS from `.Scratch`.
1 parent b5c34a6 commit 3fdd2be

File tree

2 files changed

+54
-26
lines changed

2 files changed

+54
-26
lines changed

layouts/partials/essentials/head.html

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,34 @@
7171
{{ end }}
7272
{{ end }}
7373

74+
{{- /* --- Asset Preloading to optimize Critical Rendering Path --- */ -}}
75+
{{- /* 1. Generate main CSS resource and store it in Scratch */ -}}
76+
{{- $styles := slice -}}
77+
{{- range site.Params.plugins.css -}}
78+
{{- if not (findRE `^http` .link) -}}
79+
{{- $styles = $styles | append (resources.Get .link) -}}
80+
{{- end -}}
81+
{{- end -}}
82+
{{- $styles = $styles | append (resources.Get "scss/main.scss" | toCSS) -}}
83+
{{- $styles = $styles | resources.Concat "css/style.css" -}}
84+
{{- $styles = $styles | css.PostCSS -}}
85+
{{- if hugo.IsProduction -}}
86+
{{- $styles = $styles | resources.ExecuteAsTemplate "css/style.css" . | minify | fingerprint | resources.PostProcess -}}
87+
{{- else -}}
88+
{{- $styles = $styles | resources.ExecuteAsTemplate "css/style.css" . -}}
89+
{{- end -}}
90+
{{- .Scratch.Set "main_css" $styles -}}
91+
92+
{{- /* 2. Preload the main CSS file */ -}}
93+
<link rel="preload" href="{{ $styles.RelPermalink }}" as="style" />
94+
95+
{{/* 3. Preload critical fonts to break the request chain */}}
96+
{{ range site.Data.fonts.files }}
97+
{{ with resources.Get . }}
98+
<link rel="preload" href="{{ .RelPermalink }}" as="font" type="font/woff2" crossorigin>
99+
{{ end }}
100+
{{ end }}
101+
74102
<!-- opengraph and twitter card -->
75103
{{ partial "components/base-seo.html" . }}
76104

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
{{- /* layouts/partials/essentials/style.html */ -}}
2-
<!-- plugins + stylesheet -->
3-
{{ $styles := slice }}
4-
{{ range site.Params.plugins.css }}
5-
{{ if findRE "^http" .link }}
6-
<link
7-
crossorigin="anonymous"
8-
media="all"
9-
rel="stylesheet"
10-
href="{{ .link | relURL }}"
11-
{{ .attributes | safeHTMLAttr }} />
12-
{{ else }}
13-
{{ $styles = $styles | append (resources.Get .link) }}
14-
{{ end }}
15-
{{ end }}
16-
{{ $styles := $styles | append (resources.Get "scss/main.scss" | toCSS) }}
17-
{{ $styles := $styles | resources.Concat "css/style.css" }}
18-
{{ $styles = $styles | css.PostCSS }}
19-
{{ if hugo.IsProduction }}
20-
{{ $styles = $styles | resources.ExecuteAsTemplate "css/style.css" . | minify | fingerprint | resources.PostProcess }}
21-
{{ else }}
22-
{{ $styles = $styles | resources.ExecuteAsTemplate "css/style.css" . }}
23-
{{ end }}
24-
<link
25-
href="{{ $styles.RelPermalink }}"
26-
integrity="{{ $styles.Data.Integrity }}"
27-
rel="stylesheet" />
2+
{{- /*
3+
This partial now links to the CSS that was pre-processed and preloaded in head.html.
4+
It also handles any external CSS files.
5+
*/ -}}
6+
7+
{{- /* Link to external stylesheets first */ -}}
8+
{{- range site.Params.plugins.css -}}
9+
{{- if findRE `^http` .link -}}
10+
<link
11+
crossorigin="anonymous"
12+
media="all"
13+
rel="stylesheet"
14+
href="{{ .link | relURL }}"
15+
{{ .attributes | safeHTMLAttr }} />
16+
{{- end -}}
17+
{{- end -}}
18+
19+
{{- /* Link to the main stylesheet generated in head.html */ -}}
20+
{{- with .Scratch.Get "main_css" -}}
21+
<link
22+
href="{{ .RelPermalink }}"
23+
integrity="{{ .Data.Integrity }}"
24+
rel="stylesheet" />
25+
{{- else -}}
26+
{{- warnf "The 'main_css' resource was not found in .Scratch. The stylesheet might be missing. This can happen if head.html is not called before style.html in baseof.html." -}}
27+
{{- end -}}

0 commit comments

Comments
 (0)