HUGO

Is staticDir2 available as a variable?

I am using a 2nd static dir to store resources like bootstrap/jquery css and js files which can be used conditionally. The purpose is to have 2 different config.toml files, one for local development which uses this local version of the files (staticDir2 is defined with the path), and another config file for the live site which pulls the static resources from official CDNs (the staticDir2 variable is empty or omitted).

Testing on the existence/value of .Site.staticDir2 in the partials does not work:
executing “theme/partials/header.html” at <.Site.staticDir2>: can’t evaluate field StaticDir in type *hugolib.SiteInfo

Is there a way to check on this variable ? For the sake of simplicity of the config file, I’d like to avoid having to define another variable under ‘params’ to identify that staticDir2 is in use, if that is possible.

No. But even if it was, it would have no value on its own. The static dirs configuration becomes a big union file system.

I think the following link on stackoverflow will give you a hint how to do this with javascript (it’s the other way around, using CDN and fall back local, but you get the drift). this will slow the page down if the fallback needs to run.

Other than that a simple extra parameter nocdn in the config file will not kill simplicity and everyone downloading external libraries and putting them in the secondary static dir will have no problem to add this or uncomment this parameter. it’s probably best to assume people want to use the CDN unless they add a nocdn param in this constellation.

Thanks for confirming that this variable cannot be checked on. Agreed that the value in itself is of no real use. I’ll fall back to using a custom variable under ‘params’ for my needs.

Thanks pkollitsch for the link and suggestion. Your “fallback” scenario could be an option which I had not thought of. In the end, I’ll stick to the "extra parameter " way of doing it. CDNs are pretty reliable nowadays, at least those used by the usual suspects (bootstrap, jquery, font-awesome), so I am not sure I want to add a javascript-based fall back option in the code for the live site.

All I need really is for the development site for me to have the ability to work on it and test it locally even without being connected to the Internet in some cases as happens from time to time (CDN unreachable). Latency is also improved by having the files locally. For that, the extra parameter associated with an {{ if .noCDN }} check in footer.html and header.tml will be sufficient, and 2 config.toml files, one for dev with noCDN set to ‘true’ and staticDir2 defined, and one for the live site without them.

Have a look at @bep’s answer in this thread:

That should work without config variable if you need it only for local development.

Thanks for the link to the other post. Very useful.

There is also:

.Site.IsServer

coming soon I believe - see https://github.com/gohugoio/hugo/pull/4541

For example:

{{if .Site.IsServer}}
Code here
{{end}}