divinerites / plausible-hugo

Dead simple integration between plausible.io and gohugo.io
GNU General Public License v3.0
108 stars 10 forks source link

Error using module installation with “congo” theme #17

Closed jmb closed 1 year ago

jmb commented 1 year ago

I’m trying to use the module approach with the congo theme, but I don’t suppose this is limited solely to the this theme.

I’ve added {{ partial “plausible_head.html” . }} to the layouts/partials/analytics.html file which the congo theme uses to store all the analytics stuff that will be put into the header.

However I suspect the variable scope/context is causing an issue (error: <.Page.Kind>: can't evaluate field Page in type *hugolib.SiteInfo), so I’m wondering if there is a more generic way to make this work using $. to get the global context?

Full error message below.

$ hugo
Start building sites … 
hugo v0.110.0-e32a493b7826d02763c3b79623952e625402b168+extended linux/amd64 BuildDate=2023-01-17T12:16:09Z VendorInfo=gohugoio
WARN 2023/02/23 11:40:46 *** Please, consider leaving a star on Github if you like this plausible-hugo module.
 Remove this warning by adding 'gitstar = false' in your plausible config
ERROR 2023/02/23 11:40:46 render of "home" failed: "/tmp/hugo_cache/modules/filecache/modules/pkg/mod/github.com/jpanther/congo/v2@v2.5.2/layouts/_default/baseof.html:17:6": execute of template failed: template: index.html:17:6: executing "index.html" at <partial "head.html" .>: error calling partial: "/tmp/hugo_cache/modules/filecache/modules/pkg/mod/github.com/jpanther/congo/v2@v2.5.2/layouts/partials/head.html:109:5": execute of template failed: template: partials/head.html:109:5: executing "partials/head.html" at <partialCached "analytics.html" .Site>: error calling partialCached: "[..]/layouts/partials/analytics.html:1:3": execute of template failed: template: partials/analytics.html:1:3: executing "partials/analytics.html" at <partial "plausible_head.html" .>: error calling partial: "/tmp/hugo_cache/modules/filecache/modules/pkg/mod/github.com/divinerites/plausible-hugo@v1.17.2/layouts/partials/plausible_head.html:76:19": execute of template failed: template: partials/plausible_head.html:76:19: executing "partials/plausible_head.html" at <.Page.Kind>: can't evaluate field Page in type *hugolib.SiteInfo
ERROR 2023/02/23 11:40:46 render of "section" failed: "/tmp/hugo_cache/modules/filecache/modules/pkg/mod/github.com/jpanther/congo/v2@v2.5.2/layouts/_default/baseof.html:17:6": execute of template failed: template: _default/list.html:17:6: executing "_default/list.html" at <partial "head.html" .>: error calling partial: "/tmp/hugo_cache/modules/filecache/modules/pkg/mod/github.com/jpanther/congo/v2@v2.5.2/layouts/partials/head.html:109:5": execute of template failed: template: partials/head.html:109:5: executing "partials/head.html" at <partialCached "analytics.html" .Site>: error calling partialCached: "[..]/layouts/partials/analytics.html:1:3": execute of template failed: template: partials/analytics.html:1:3: executing "partials/analytics.html" at <partial "plausible_head.html" .>: error calling partial: "/tmp/hugo_cache/modules/filecache/modules/pkg/mod/github.com/divinerites/plausible-hugo@v1.17.2/layouts/partials/plausible_head.html:76:19": execute of template failed: template: partials/plausible_head.html:76:19: executing "partials/plausible_head.html" at <.Page.Kind>: can't evaluate field Page in type *hugolib.SiteInfo
ERROR 2023/02/23 11:40:46 render of "taxonomy" failed: "/tmp/hugo_cache/modules/filecache/modules/pkg/mod/github.com/jpanther/congo/v2@v2.5.2/layouts/_default/baseof.html:17:6": execute of template failed: template: _default/taxonomy.html:17:6: executing "_default/taxonomy.html" at <partial "head.html" .>: error calling partial: "/tmp/hugo_cache/modules/filecache/modules/pkg/mod/github.com/jpanther/congo/v2@v2.5.2/layouts/partials/head.html:109:5": execute of template failed: template: partials/head.html:109:5: executing "partials/head.html" at <partialCached "analytics.html" .Site>: error calling partialCached: "[..]/layouts/partials/analytics.html:1:3": execute of template failed: template: partials/analytics.html:1:3: executing "partials/analytics.html" at <partial "plausible_head.html" .>: error calling partial: "/tmp/hugo_cache/modules/filecache/modules/pkg/mod/github.com/divinerites/plausible-hugo@v1.17.2/layouts/partials/plausible_head.html:76:19": execute of template failed: template: partials/plausible_head.html:76:19: executing "partials/plausible_head.html" at <.Page.Kind>: can't evaluate field Page in type *hugolib.SiteInfo
ERROR 2023/02/23 11:40:46 render of "taxonomy" failed: "/tmp/hugo_cache/modules/filecache/modules/pkg/mod/github.com/jpanther/congo/v2@v2.5.2/layouts/_default/baseof.html:17:6": execute of template failed: template: _default/taxonomy.html:17:6: executing "_default/taxonomy.html" at <partial "head.html" .>: error calling partial: "/tmp/hugo_cache/modules/filecache/modules/pkg/mod/github.com/jpanther/congo/v2@v2.5.2/layouts/partials/head.html:109:5": execute of template failed: template: partials/head.html:109:5: executing "partials/head.html" at <partialCached "analytics.html" .Site>: error calling partialCached: "[..]/layouts/partials/analytics.html:1:3": execute of template failed: template: partials/analytics.html:1:3: executing "partials/analytics.html" at <partial "plausible_head.html" .>: error calling partial: "/tmp/hugo_cache/modules/filecache/modules/pkg/mod/github.com/divinerites/plausible-hugo@v1.17.2/layouts/partials/plausible_head.html:76:19": execute of template failed: template: partials/plausible_head.html:76:19: executing "partials/plausible_head.html" at <.Page.Kind>: can't evaluate field Page in type *hugolib.SiteInfo
Error: Error building site: failed to render pages: render of "page" failed: "/tmp/hugo_cache/modules/filecache/modules/pkg/mod/github.com/jpanther/congo/v2@v2.5.2/layouts/_default/baseof.html:17:6": execute of template failed: template: _default/single.html:17:6: executing "_default/single.html" at <partial "head.html" .>: error calling partial: "/tmp/hugo_cache/modules/filecache/modules/pkg/mod/github.com/jpanther/congo/v2@v2.5.2/layouts/partials/head.html:109:5": execute of template failed: template: partials/head.html:109:5: executing "partials/head.html" at <partialCached "analytics.html" .Site>: error calling partialCached: "[..]/layouts/partials/analytics.html:1:3": execute of template failed: template: partials/analytics.html:1:3: executing "partials/analytics.html" at <partial "plausible_head.html" .>: error calling partial: "/tmp/hugo_cache/modules/filecache/modules/pkg/mod/github.com/divinerites/plausible-hugo@v1.17.2/layouts/partials/plausible_head.html:76:19": execute of template failed: template: partials/plausible_head.html:76:19: executing "partials/plausible_head.html" at <.Page.Kind>: can't evaluate field Page in type *hugolib.SiteInfo
Total in 170 ms
divinerites commented 1 year ago

I don't know how and where you modified your code. So I'm just guessing here ...

I guess I would create a new layout/partials/analytics.html to surcharge the original in your theme with 1 line calling the plausible partial inside this analytics partial (while also removing GA and Fathom calls).

Plausible already take care of site.IsServer :

layout/partials/analytics.html

<!-- Just one line calling plausible head partial -->
{{ partial “plausible_head.html” . }}
divinerites commented 1 year ago

Yes you encounter a context problem. You need to pass the right context because the error origin is line 76 when testing if it is a 404 page. So the simplest for you is my previous advice I guess.

{{- if eq .Page.Kind "404" }}
    <script>plausible("404",{ props: { path: document.location.pathname } });</script>
{{- end }}
jmb commented 1 year ago

I have created the one-line analytics.html as suggested.

The congo theme has a chain of partials/templates I think. From the errors: baseof.html calls <partial "head.html" . > which calls <partialCached "analytics.html" .Site > where I have entered the call to <partial "plausible_head.html" . >

So am I right in thinking that plausible_head only gets .Site but needs more context? If I change the call to the analytics.html partial to ., then maybe that will fix it.

jmb commented 1 year ago

If I change the call to the analytics.html partial to ., then maybe that will fix it.

This seems to have fixed the problem - I copied the head.html from the congo source to my layouts/partials and changed the .Site to .. I will go back to the congo repo and see if it can be changed there.

divinerites commented 1 year ago

Ah yes if the context passed to analytics.html is .Site it will surely fails because of the contexte not being . for plausible partial.

I think you've done the right thing. Closing this one then.

jmb commented 1 year ago

If anyone else is following, here is the issue over at the congo repo: https://github.com/jpanther/congo/issues/480

jmb commented 1 year ago

This has now been fixed in Congo. ☺️