Skip to content

Commit

Permalink
Merge pull request #1213 from gethinode/develop
Browse files Browse the repository at this point in the history
Support integration of critical modules and scripts by category
  • Loading branch information
markdumay authored Sep 24, 2024
2 parents d522fb0 + e656ec9 commit 6bf687d
Show file tree
Hide file tree
Showing 18 changed files with 68 additions and 54 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion config/_default/params.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

# toml-docs-start debugging
[debugging]
showJS = true
showJS = false
showSCSS = false
purgeHTMLComments = false
includeSVGOrigin = true
Expand Down
4 changes: 2 additions & 2 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

module.exports = require('neostandard')({
ignores: [
'assets/js/functional/languageSelector.js',
'assets/js/functional/color.js',
'assets/js/critical/functional/languageSelector.js',
'assets/js/critical/functional/color.js',
'assets/js/analytics.js',
'assets/js/flexsearch.js',
'assets/js/navbar.js',
Expand Down
2 changes: 1 addition & 1 deletion exampleSite/config/_default/hugo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ home = ["HTML", "RSS", "REDIR"]
# Build and serve using local hinode clone declared in the named Hugo workspace:
workspace = "hinode.work"
[[module.imports]]
path = "github.com/gethinode/mod-cookieyes"
path = "github.com/gethinode/mod-cookieyes/v2"
[[module.imports]]
path = "github.com/gethinode/hinode"
[[module.imports]]
Expand Down
6 changes: 3 additions & 3 deletions exampleSite/config/_default/params.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
transpiler = "dartsass"

[debugging]
showJS = true
showJS = false
showSCSS = false
purgeHTMLComments = true # prevents a Goldmark warning when processing HTML comments (as used by markdownlint)
includeSVGOrigin = true
Expand Down Expand Up @@ -193,5 +193,5 @@
hinode = "https://gethinode.com"

[modules.cookieyes]
id = "a54b5553f349dd13bd225f8e"
force = false
# local = true
url = "https://cdn-cookieyes.com/client_data/a54b5553f349dd13bd225f8e/script.js"
4 changes: 3 additions & 1 deletion exampleSite/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ module github.com/gethinode/hinode/exampleSite

go 1.19

require github.com/gethinode/mod-cookieyes v1.0.2 // indirect
require (
github.com/gethinode/mod-cookieyes/v2 v2.0.3 // indirect
)
3 changes: 3 additions & 0 deletions exampleSite/go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
github.com/gethinode/mod-cookieyes v1.0.0 h1:KTkK67uugji9pzYJJalvNhdECHgG1DzPtyeaYL0Xm/0=
github.com/gethinode/mod-cookieyes v1.0.0/go.mod h1:VrBUKm5apop6KIb+R8eRqvlf3AAPnv5aMajL0RorIjY=
github.com/gethinode/mod-cookieyes v1.0.2 h1:WL9sjpYKjkV+jRKIrm6H82UpthQjFksvKGptW6HD/QU=
github.com/gethinode/mod-cookieyes v1.0.2/go.mod h1:VrBUKm5apop6KIb+R8eRqvlf3AAPnv5aMajL0RorIjY=
github.com/gethinode/mod-cookieyes/v2 v2.0.3/go.mod h1:tULb7D7CoTycGUyL7ryqHJKaX11XuL2SN+XwP7/DI0Y=
2 changes: 2 additions & 0 deletions exampleSite/hinode.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ github.com/gethinode/hinode v0.22.5 h1:zpjSDgWQVbq4BjPduxwexQB18gawFeinzPkaRRTs2
github.com/gethinode/hinode v0.22.5/go.mod h1:hsskrlBRnTbpCjrDtGbK8C1VYGML7ezHtCDGDiYL2mQ=
github.com/gethinode/mod-cookieyes v1.0.2 h1:WL9sjpYKjkV+jRKIrm6H82UpthQjFksvKGptW6HD/QU=
github.com/gethinode/mod-cookieyes v1.0.2/go.mod h1:VrBUKm5apop6KIb+R8eRqvlf3AAPnv5aMajL0RorIjY=
github.com/gethinode/mod-cookieyes/v2 v2.0.3 h1:o5PAAOjx8baRcSkhTAQuZ+hX9cK8oWjeC3sYxt1DT2c=
github.com/gethinode/mod-cookieyes/v2 v2.0.3/go.mod h1:tULb7D7CoTycGUyL7ryqHJKaX11XuL2SN+XwP7/DI0Y=
1 change: 0 additions & 1 deletion exampleSite/hugo_stats.json
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,6 @@
"collapse",
"collapse-1",
"command-prompt",
"cookieyes",
"custom-activity",
"data-tables",
"docs",
Expand Down
4 changes: 1 addition & 3 deletions exampleSite/layouts/_default/baseof.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@
<!doctype html>
<html lang="{{ .Site.Language.Lang }}" class="no-js">
<head>
{{/*- partial "footer/scripts.html" (dict "page" . "type" "critical") -*/}}
<script src="https://cdn-cookieyes.com/client_data/a54b5553f349dd13bd225f8e/script.js" id="cookieyes" type="text/javascript"></script>
{{- partial "footer/scripts.html" (dict "page" . "type" "functional") -}}
{{- partial "footer/scripts.html" (dict "page" . "type" "critical") -}}
{{ block "head" . }}{{ end -}}
</head>

Expand Down
2 changes: 1 addition & 1 deletion layouts/partials/assets/video.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<!-- Main code -->
{{ if not $error -}}
{{ if eq $host "youtube" }}
{{- $host := cond $pc.PrivacyEnhanced "www.youtube-nocookie.com" "www.youtube.com" -}}
{{- $host := cond $pc.PrivacyEnhanced "www.youtube-nocookie.com" "www.youtube.com" -}}
{{ $url := printf "https://%s/embed/%s?origin=%s" $host $id $page.Site.BaseURL }}
{{ $api := printf "https://www.youtube.com/oembed?format=json&url=%s" (printf "https://www.youtube.com/watch?v=%s" $id) }}
{{ $padding := "56.25%" }}
Expand Down
3 changes: 2 additions & 1 deletion layouts/partials/footer/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
<small>
{{- $copyright := printf "%s © %s %s %s." (T "copyright") (dateFormat "2006" now) .Site.Title (T "rights") }}
{{ cond (gt (len .Site.Copyright) 0) .Site.Copyright $copyright }}
{{ .Site.Params.footer.license | safeHTML }}
{{ .Site.Params.footer.license | safeHTML }} |
<a class="cky-banner-element link-bg-footer">{{ T "cookieSettings" }}</a> |
{{ if .Site.Params.main.endorse }}
{{ $link := partial "assets/link.html" (dict "destination" (index site.Params.links "hinode") "text" "Hinode" "class" "link-bg-footer" "page" .Page) }}
{{ T "poweredBy" $link | safeHTML }}
Expand Down
77 changes: 41 additions & 36 deletions layouts/partials/footer/scripts.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
{{ end -}}
{{ end }}


<!-- Initialize arguments -->
{{ $args := partial "utilities/InitArgs.html" (dict "structure" "scripts" "args" .) }}
{{ if $args.err }}
Expand All @@ -76,22 +75,17 @@

<!-- Initialize local arguments -->
{{ $patterns := dict
"critical" "js/critical/**.js"
"functional" "js/functional/**.js"
"other" "js/critical/*.js"
"functional" "js/critical/functional/**.js"
"analytics" "js/critical/analytics/**.js"
"performance" "js/critical/performance/**.js"
"advertisement" "js/critical/advertisement/**.js"
"core" "{js/*.js,js/vendor/**.js}"
}}

{{- $absoluteURL := site.Params.main.canonifyAssetsURLs | default false -}}
{{ $state := "immediate" }}

{{ $config := dict }}
{{ if not (in (slice "critical" "functional") $args.type) }}
{{ $config = $args.page.Scratch.Get "modules" }}
{{ if not $config }}
{{ errorf "partial [footer/scripts.html] - Cannot initialize module configuration" }}
{{ $error = true }}
{{ end }}
{{ end }}
{{ $config := $args.page.Scratch.Get "modules" }}

{{ $page_modules := slice }}
{{ if reflect.IsMap $args.page.Params.modules }}
Expand All @@ -101,57 +95,68 @@
{{ end }}
{{ with $args.page.Scratch.Get "dependencies" }}{{ $page_modules = append $page_modules . | uniq }}{{ end }}

{{- $categories := dict "other" slice -}}

{{ $modules := slice }}
{{ if eq $args.type "core" }}
{{ if eq $args.type "critical" }}
{{- $modules = $config.critical -}}
{{ $categories = merge $categories (dict "functional" slice "analytics" slice "performance" slice "advertisement" slice) }}
{{ else if eq $args.type "core" }}
{{- $modules = $config.core -}}
{{ else if eq $args.type "optional" }}
{{- $modules = $config.optional | intersect $page_modules -}}
{{ end }}

{{ range $cat, $val := $config.categories }}
{{ $categories = merge $categories (dict $cat (intersect $val $modules)) }}
{{ end }}

{{ $localize := false }}
{{ if gt (intersect (or $config.localize slice) $modules | len) 0 }}{{ $localize = true }}{{ end }}

{{ $skipTemplate := false }}
{{ if gt (intersect (or $config.skipTemplate slice) $modules | len) 0 }}{{ $skipTemplate = true }}{{ end }}

{{- $categories := dict "other" slice -}}
{{ range $cat, $val := $config.categories }}
{{ $categories = merge $categories (dict $cat (intersect $val $modules)) }}
{{ end }}

<!-- Main code -->
{{ if not $error }}
<!-- Bundle the critical and functional scripts as single file -->
{{ if in (slice "critical" "functional") $args.type }}
{{ partial "partials/bundle-script.html" (dict
"page" $args.page
"match" (index $patterns $args.type)
"destination" (printf "js/%s.bundle.js" "critical")
"cat" (cond (eq $args.type "functional") $args.type "")
"localize" $localize
"skipTemplate" $skipTemplate
"absoluteURL" $absoluteURL
)}}
<!-- Bundle the core scripts by category -->
{{ else if eq $args.type "core" }}
<!-- include external scripts first -->
{{ range $mod, $cfg := $config.modules }}
{{ if eq (index $cfg "integration") $args.type }}
{{ if or (index $cfg "local") (not hugo.IsServer) }}
{{ with index $cfg "url" }}
{{ partial "templates/script.html" (dict "link" . "category" (index $cfg "category") "state" (index $cfg "state")) }}
{{ end }}
{{ end }}
{{ end }}
{{ end }}

<!-- Bundle the critical and core scripts by category -->
{{ if ne $args.type "optional" }}
{{ range $cat, $val := $categories }}
{{ if gt ($val | len) 0 }}
{{ if or (gt ($val | len) 0) (eq $args.type "critical") }}
{{ $match := "" }}
{{ if eq $args.type "critical" }}
{{ $match = index $patterns $cat }}
{{ else if eq $cat "other" }}
{{ $match = index $patterns $args.type }}
{{ end }}

{{ partial "partials/bundle-script.html" (dict
"page" $args.page
"match" (cond (eq $cat "other") (index $patterns $args.type) "")
"match" $match
"destination" (printf "js/%s.bundle.js" $args.type)
"cat" $cat
"modules" $val
"localize" $localize
"skipTemplate" $skipTemplate
"absoluteURL" $absoluteURL
"state" "async"
"state" (cond (eq $args.type "critical") "immediate" "async")
)}}
{{ end }}
{{ end }}
{{ else }}
<!-- Bundle the optional scripts by module name and category -->
{{ range $cat, $val := $categories }}
<!-- Bundle the optional scripts by module name and category -->
{{ range $cat, $val := $categories }}
{{ range $val }}
{{ $modconfig := index $config.modules . }}
{{ partial "partials/bundle-script.html" (dict
Expand Down
6 changes: 5 additions & 1 deletion layouts/partials/utilities/InitModules.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{{ $critical := slice }}
{{ $core := slice }}
{{ $optional := slice }}
{{ $excludeSCSS := slice }}
Expand All @@ -9,7 +10,9 @@
{{ range $key, $mod := .Site.Params.modules }}
{{ if reflect.IsMap $mod }}
{{ $integration := index $mod "integration" }}
{{ if eq $integration "core" }}
{{ if eq $integration "critical" }}
{{ $critical = $critical | append $key }}
{{ else if eq $integration "core" }}
{{ $core = $core | append $key }}
{{ else if eq $integration "optional" }}
{{ $optional = $optional | append $key }}
Expand Down Expand Up @@ -43,6 +46,7 @@

{{ $modules := dict
"modules" $modules
"critical" $critical
"core" $core
"optional" $optional
"excludeSCSS" $excludeSCSS
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gethinode/hinode",
"version": "0.27.0-beta2",
"version": "0.27.0-beta",
"description": "Hinode is a clean documentation and blog theme for Hugo, an open-source static site generator",
"keywords": [
"hugo",
Expand Down

0 comments on commit 6bf687d

Please sign in to comment.