Skip to content

Commit f18fcef

Browse files
authored
Merge pull request #16 from ethmarks/load-asset-refactor
Refactor load-assets.html partial into css.html and js.html
2 parents 81a6471 + 2aa301c commit f18fcef

16 files changed

Lines changed: 57 additions & 40 deletions

File tree

content/posts/personalwebsite.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,14 @@ Every page layout has its own Sass file that imports the modules that page needs
5959
@import 'components/random-post-btn';
6060
```
6161

62-
These Sass files are then transpiled into normal CSS and imported into the page using my custom `load-assets` [partial](https://gohugo.io/functions/partials/include/) that allows me to just input a list of assets, and have the partial automatically generate the HTML imports. For example, here's the code that the posts page uses.
62+
These Sass files are then transpiled into normal CSS and imported into the page using some custom [partials](https://gohugo.io/functions/partials/include/) I wrote that take a file name as an input and automatically minifies and transpiles the asset, and then generates the HTML import for that file. For example, here's the code at the top of [the posts index page](/posts).
6363

6464
```go-html-template
65-
{{ define "resources" }}
66-
{{ partial "load-assets.html" (slice "css/posts.scss" "js/components/randompost.js") }}
67-
{{ end }}
65+
{{ partial "css.html" "posts.scss" }}
66+
{{ partial "js.html" "components/randompost.js" }}
6867
```
6968

70-
And here's the HTML that the partial generates.
69+
And here's the HTML that the partial generates. Note that it transpiled the SCSS into CSS, minified both assets, and added `defer` to the script.
7170

7271
```html
7372
<link rel="stylesheet" href="/css/posts.min.css" />
@@ -109,7 +108,7 @@ As of August 31, 2025, the site earns a perfect 100 on [Lighthouse](https://deve
109108

110109
My site's performance is largely due to it being lightweight and carefully optimized.
111110
- **Static Rendering**: All of my site's content is present in the HTML. Unlike some sites that use [JSX](https://react.dev/learn/writing-markup-with-jsx) and [client-side rendering](https://developer.mozilla.org/en-US/docs/Glossary/CSR) to dynamically [hydrate](https://en.wikipedia.org/wiki/Hydration_(web_development)) their pages using JavaScript, my site render all content at build time. This uses much less processing power on your device and makes the page load much faster.
112-
- **Optimized CSS and JS**: Each page on my site only loads the resources that it needs, and the resources that it does import are minified. I used my custom `load-assets` partial to automate this process slightly. I discussed `load-assets` in more detail in the [Sass](#sass) section. This per-page approach minimizes the data that needs to be downloaded and allows the page to load faster.
111+
- **Optimized CSS and JS**: Each page on my site only loads the resources that it needs, and the resources that it does import are minified. I used custom partials to automate this process slightly, as discussed in the [Sass section](#sass). This per-page approach minimizes the data that needs to be downloaded and allows the page to load faster.
113112
- **Optimized Images and Videos**: The images, videos, and animations on my website use the [WebP](https://en.wikipedia.org/wiki/WebP) and [WebM](https://en.wikipedia.org/wiki/WebM) formats, which allows them to load faster and use less data.
114113

115114
## Privacy

layouts/404.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{{ define "resources" }}
22
<link rel="preload" href="/fonts/Kenia.woff2" as="font" type="font/woff2" crossorigin />
3-
{{- partial "load-assets.html" (slice "css/404.scss" "js/pages/404.sync.js") -}}
4-
{{ end }}
3+
{{ partial "css.html" "404.scss" }}
4+
{{ partial "js.html" "404.sync.js" }}
5+
{{- end }}
56

67
{{ define "main" }}
78
<h1 class="nomint">404</h1>

layouts/_default/baseof.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
{{ partial "metadata.html" . -}}
55
{{- block "resources" . }}{{ end }}
66
{{- block "default-resources" . -}}
7-
{{- partial "load-assets.html" (slice "js/common.sync.js" "js/vendor/quicklink.async.js") -}}
7+
{{ partial "js.html" "common.sync.js" }}
8+
{{ partial "js.html" "vendor/quicklink.async.js" }}
89
{{- end -}}
910
{{ partial "font-preload.html" . }}
1011
</head>

layouts/_default/taxonomy.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{{ define "resources" }}
2-
{{- partial "load-assets.html" (slice "css/tag.scss") -}}
2+
{{ partial "css.html" "tag.scss" }}
33
{{ end }}
44

55
{{ define "main" }}

layouts/_default/terms.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{{ define "resources" }}
2-
{{- partial "load-assets.html" (slice "css/tags.scss") -}}
2+
{{ partial "css.html" "tags.scss" }}
33
{{ end }}
44

55
{{ define "main" }}

layouts/about/list.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{{ define "resources" }}
2-
{{- partial "load-assets.html" (slice "css/about.scss" "js/components/gitstats.js" "js/components/daysalive.js") -}}
2+
{{ partial "css.html" "about.scss" }}
3+
{{ partial "js.html" "components/gitstats.js" }}
4+
{{ partial "js.html" "components/daysalive.js" }}
35
{{ end }}
46

57
{{ define "main" }}

layouts/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{{ define "resources" }}
2-
{{- partial "load-assets.html" (slice "css/home.scss" "js/pages/home.js") -}}
2+
{{ partial "css.html" "home.scss" }}
3+
{{ partial "js.html" "home.js" }}
34
{{ end }}
45

56
{{ define "main" }}

0 commit comments

Comments
 (0)