Files
FixIt/layouts/_partials/plugin/style.html
T
Cell 17164f42d0 fix(assets): rewrite service worker with two-strategy caching (#774)
- Replace legacy service-worker.js (imported from DoIt) with a clean
  rewrite using Hugo's ExecuteAsTemplate for fingerprinted URL injection
- Cache-first for static assets (immutable/fingerprinted), network-first
  for HTML pages with LRU eviction (max 100 entries)
- Fix LRU eviction to protect precached URLs using absolute URL comparison
- Add Fingerprint param to js-build.html partial to reduce boilerplate
- Extract to-css.html partial for reusable SCSS-to-CSS pipeline
- Support pre-built Resource param in script.html and style.html
- Store mainCSS and mainJS in Site.Store to avoid duplicate builds
- Remove explicit scope from serviceWorker.register() (defaults to /)

Closes #298
2026-05-31 17:54:02 +08:00

77 lines
3.0 KiB
HTML

{{- /*
Stylesheet tag renderer.
- Accepts direct <link> HTML, a pre-built resource, or builds from source path.
- Supports template execution, optional toCSS, minify/fingerprint, and preload mode.
@param {String} [.Source] stylesheet URL/path; if starts with "<link" it's treated as raw HTML
@param {resource.Resource} [.Resource] pre-built resource (skips build pipeline)
@param {String} [.Content] inline style content to write as generated resource
@param {String} [.Path] target path for resource created from Content
@param {Object} [.Template] target path for resource created from template execution
@param {Object} [.Context] template context for ExecuteAsTemplate
@param {Object|Boolean} [.ToCSS] true for defaults, dict for custom toCSS options, false/omit to skip toCSS
@param {Boolean} [.Minify] whether to minify resource
@param {String} [.Fingerprint] fingerprint algorithm
@param {Boolean} [.Crossorigin] whether to add crossorigin=anonymous
@param {Boolean} [.Preload] whether to output preload + noscript fallback
@param {String} [.Integrity] SRI hash value
@param {String} [.Attr] additional attributes
*/ -}}
{{- if strings.HasPrefix (.Source | default "") "<link" -}}
{{- safeHTML .Source -}}
{{- else -}}
{{- $href := "" -}}
{{- $integrity := .Integrity -}}
{{- /* Pre-built resource */ -}}
{{- with .Resource -}}
{{- $href = .RelPermalink -}}
{{- with .Data.Integrity -}}
{{- $integrity = . -}}
{{- end -}}
{{- end -}}
{{- /* Source build */ -}}
{{- if not $href -}}
{{- $href = .Source -}}
{{- end -}}
{{- if $href | and (not (urls.Parse $href).Host) | and (not .Resource) -}}
{{- $resource := resources.Get $href -}}
{{- with .Template -}}
{{- $resource = resources.ExecuteAsTemplate . $.Context $resource -}}
{{- end -}}
{{- $resource = dict "Resource" $resource "ToCSS" .ToCSS "Minify" .Minify "Fingerprint" .Fingerprint | partial "function/to-css.html" -}}
{{- with .Fingerprint -}}
{{- $integrity = $resource.Data.Integrity -}}
{{- end -}}
{{- $href = $resource.RelPermalink -}}
{{- end -}}
{{- /* Style from string */ -}}
{{- if .Content | and .Path -}}
{{- $contentResource := resources.FromString .Path .Content -}}
{{- if .Minify -}}
{{- $contentResource = $contentResource | minify -}}
{{- end -}}
{{- $href = $contentResource.RelPermalink -}}
{{- end -}}
{{- /* Style attributes */ -}}
{{- $attrs := printf `href="%v"` $href -}}
{{- if .Crossorigin -}}
{{- $attrs = ` crossorigin="anonymous"` | add $attrs -}}
{{- end -}}
{{- with $integrity -}}
{{- $attrs = printf ` integrity="%v"` . | add $attrs -}}
{{- end -}}
{{- with .Attr -}}
{{- $attrs = add " " . | add $attrs -}}
{{- end -}}
{{- if .Preload -}}
<link rel="preload" {{ $attrs | safeHTMLAttr }} as="style" onload="this.removeAttribute('onload');this.rel='stylesheet'">
<noscript><link rel="stylesheet" {{ $attrs | safeHTMLAttr }}></noscript>
{{- else -}}
<link rel="stylesheet" {{ $attrs | safeHTMLAttr }}>
{{- end -}}
{{- end -}}