Skip to content

Commit ba4eb60

Browse files
author
Ben Zörb
authored
Merge pull request #12 from jungvonmatt/feature/resource-hints
perf(resource-hints): Adds preload tag for stage images
2 parents ff5c17a + 18489b3 commit ba4eb60

File tree

15 files changed

+222
-54
lines changed

15 files changed

+222
-54
lines changed

hugo-modules/core/utils/asset/image.html

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
)
1515
) -}}
1616
*/}}
17+
{{- $globals := .globals -}}
1718
{{- $context := .context | default dict -}}
1819
{{- $options := .options | default dict -}}
1920
{{- $params := (merge $context $options) -}}
@@ -29,6 +30,7 @@
2930
{{- $loading := cond (eq $params.lazy true) "lazy" ($params.loading | default false) -}}
3031
{{- $ratio := $params.ratio -}}
3132
{{- $sizes := $params.sizes -}}
33+
{{- $preload := $params.preload | default false -}}
3234

3335
{{- $image_attributes := partialCached "utils/html/attribute" (dict
3436
"loading" $loading
@@ -49,13 +51,30 @@
4951
{{- $height := .height -}}
5052
{{- range $srcsets -}}
5153
<source sizes="{{- $local_sizes -}}" srcset="{{- .srcset -}}" type="{{- .type -}}">
54+
{{- $preload_attr := (dict
55+
"as" "image"
56+
"imagesizes" $local_sizes
57+
"imagesrcset" .srcset
58+
"type" .type
59+
) -}}
60+
{{- if and $preload (eq .type "image/avif") (not (in ($globals.Scratch.Get "preload") $preload_attr)) -}}
61+
{{- $globals.Scratch.Add "preload" (slice $preload_attr) -}}
62+
{{- end -}}
5263
{{- end -}}
53-
<img src="{{- $src -}}" width="{{- $width -}}" height="{{- $height -}}" {{ $image_attributes }} alt="{{- $alt -}}">
64+
<img src="{{- $src -}}" width="{{- $width -}}" height="{{- $height -}}" {{ $image_attributes }} alt="{{- $alt -}}"{{ with $params.preload }} fetchpriority="high"{{ end }}>
5465
{{- end -}}
5566
{{- else -}}
5667
{{- with $url -}}
5768
{{- $src := $params.src | default (cond (hasPrefix . "//") (print "https:" .) .) -}}
58-
<img src="{{- $src -}}" width="{{- $width -}}" height="{{- $height -}}" {{ $image_attributes }} alt="{{- $alt -}}">
69+
<img src="{{- $src -}}" width="{{- $width -}}" height="{{- $height -}}" {{ $image_attributes }} alt="{{- $alt -}}"{{ with $params.preload }} fetchpriority="high"{{ end }}>
70+
{{- $preload_attr := (dict
71+
"as" "image"
72+
"href" $src
73+
"type" $mime_type
74+
) -}}
75+
{{- if and $preload (eq .type "image/avif") (not (in ($globals.Scratch.Get "preload") $preload_attr)) -}}
76+
{{- $globals.Scratch.Add "preload" (slice $preload_attr) -}}
77+
{{- end -}}
5978
{{- end -}}
6079
{{- end -}}
6180
</picture>

hugo-modules/core/utils/asset/video.html

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,23 @@
1313
)
1414
) -}}
1515
*/}}
16+
{{- $globals := .globals -}}
1617
{{- $context := .context | default dict -}}
1718
{{- $options := .options | default dict -}}
1819
{{- $params := (merge $context $options) -}}
1920
{{- $mime_type := $params.mime_type -}}
2021
{{- $class_name := $params.class_name -}}
2122
{{- $url := $params.src | default $params.url -}}
23+
{{- $width := $params.width -}}
24+
{{- $height := $params.height -}}
2225
{{- $loop := $params.loop | default true -}}
2326
{{- $autoplay := $params.autoplay | default true -}}
27+
{{- $playsinline := $params.playsinline | default true -}}
2428
{{- $controls := $params.controls | default false -}}
2529
{{- $poster := $params.poster | default false -}}
2630
{{- $muted := (or $autoplay $params.muted) -}}
31+
{{- $loading := cond (eq $params.lazy true) "lazy" ($params.loading | default false) -}}
32+
{{- $preload := $params.preload | default false -}}
2733

2834
{{- $attributes := partialCached "utils/html/attribute" (dict
2935
"loop" $loop
@@ -35,7 +41,18 @@
3541
) $params -}}
3642

3743
{{- if in $mime_type "video" -}}
44+
{{- $preload_attr := (dict
45+
"as" "image"
46+
"href" $poster
47+
) -}}
48+
{{- if and $preload $poster (not (in ($globals.Scratch.Get "preload") $preload)) -}}
49+
{{- $globals.Scratch.Add "preload" (slice $preload) -}}
50+
{{- end -}}
3851
<video {{$attributes}}>
39-
<source src="{{- $url -}}" type="{{- $mime_type -}}">
52+
{{- if eq $loading "lazy" -}}
53+
<source data-src="{{- $url -}}" type="{{- $mime_type -}}" />
54+
{{- else -}}
55+
<source src="{{- $url -}}" type="{{- $mime_type -}}" />
56+
{{- end -}}
4057
</video>
4158
{{- end -}}

package-lock.json

Lines changed: 85 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/create-wekit-app/create-app.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,10 @@ export async function createApp({ appPath }: { appPath: string }): Promise<void>
239239
/**
240240
* Write it to disk.
241241
*/
242-
fs.writeFileSync(path.join(targetDir, 'package.json'), JSON.stringify(packageJson, null, 2) + os.EOL);
242+
fs.writeFileSync(
243+
path.join(targetDir, 'package.json'),
244+
JSON.stringify(packageJson, null, 2) + os.EOL
245+
);
243246

244247
/**
245248
* These flags will be passed to `install()`.
@@ -251,6 +254,7 @@ export async function createApp({ appPath }: { appPath: string }): Promise<void>
251254
const dependencies = [
252255
'@fullhuman/postcss-purgecss@4',
253256
'autoprefixer@10',
257+
'quicklink',
254258
'container-query-polyfill',
255259
'postcss@8',
256260
'postcss-cli@9',

templates/app/config/_default/config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ ignoreFiles:
77
disableHugoGeneratorInject: true
88
enableRobotsTXT: true
99
buildDrafts: true
10-
languageCode: en-us
11-
defaultContentLanguage: en-us
10+
languageCode: en
11+
defaultContentLanguage: en
1212
defaultContentLanguageInSubdir: true
1313
sitemap:
1414
changefreq: daily
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
theme_color: "#fff"
22
project_prefix: jvm
3+
4+
preconnect:
5+
- https://videos.ctfassets.net
6+
- https://images.ctfassets.net

templates/app/layouts/_default/baseof.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
{{- $settings := partial "utils/get-settings" . -}}
2+
3+
{{- $params := merge site.Params (dict
4+
"environment" (or (getenv "HUGO_ENVIRONMENT") hugo.Environment)
5+
) -}}
6+
7+
{{- with .Params.content -}}
8+
{{/* Prerender the page so we have the Scratch object available in "structure/head" to apply preload hints */}}
9+
{{/* Stored in a variable so we don't render the content to screen */}}
10+
{{ $tmp := partialCached "utils/get-partial.html" (dict
11+
"context" .
12+
"globals" $
13+
) . }}
14+
{{- end -}}
15+
116
<!doctype html>
217
<html lang="{{ (index (split site.LanguageCode "-") 0) | default "en" }}">
318
<head>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{{ define "main" }}
22
{{- $pages := .Pages -}}
33
{{ with .Params.content }}
4-
{{ partial "utils/get-partial" (dict
4+
{{ partialCached "utils/get-partial" (dict
55
"context" .
66
"globals" $
77
"options" (dict
88
"pages" $pages
99
)
10-
) }}
10+
) . }}
1111
{{ end }}
1212
{{ end }}

0 commit comments

Comments
 (0)