trunk
trunk copied to clipboard
Conditionally include tags based on features
Is there a way to conditionally include some
I want to only include some google analytics script tag on production build, as opposed to staging or dev environments
I propose a data-trunk-feature attribute
Say this is in the index.html:
<script data-trunk-feature="analytics" async src=“https://www.googletagmanager.com/gtag/js?id=G-....” ></script>
This will only gets included with trunk build --features analytics
Interesting idea. Could you come up with a PR for that?
I'll dig into it
After looking at the conventions closer, instead of introducing a data-trunk-feature attribute, I think we should stick with the good old data-trunk attribute and introduce separate data-feature-gate="..." attribute (just like data-no-minify and data-target-path )
There are some concerns though, if a user decide to use the --no-default-features flag, I'm not sure how we determine if a feature is activated. I think we'll have to look at the user's crate's manifest to know the default features?
https://github.com/trunk-rs/trunk/blob/4c9d85f6f04a31a272c71db8df12e142c76311fb/src/config/rt/build.rs#L14-L26
Instead of coupling the conditional inclusion with rust features, maybe we should use env vars instead? In my use case for example, the google analytics tag has nothing to do with my rust code and there is actually no corresponding feature.
Moreover, current <script data-trunk src=".."/> only allow a local file as the src, should we automatically detect if it's a url or a file system path? Or should we introduce a data-... attribute to indicate?
following the env var idea, it can be for example data-external-script-env-var="SOME_THING" to indicate this script will only be included if a env var is set.
So this can be a potential design too:
<script data-trunk data-external-script-env-var="GOOGLE_ANALYTICS" src="https://google.com/tag?id=xdxdowouwuX3startstwerking"/>