Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditionally include tags based on features #907

Open
Madoshakalaka opened this issue Nov 8, 2024 · 3 comments
Open

Conditionally include tags based on features #907

Madoshakalaka opened this issue Nov 8, 2024 · 3 comments

Comments

@Madoshakalaka
Copy link

Is there a way to conditionally include some <script> tags based on features?

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

@ctron
Copy link
Collaborator

ctron commented Nov 8, 2024

Interesting idea. Could you come up with a PR for that?

@Madoshakalaka
Copy link
Author

I'll dig into it

@Madoshakalaka
Copy link
Author

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?

/// Config options for the cargo build command
#[derive(Clone, Debug)]
pub enum Features {
/// Use cargo's `--all-features` flag during compilation.
All,
/// An explicit list of features to use; might be empty; might include no-default-features.
Custom {
/// Space or comma separated list of cargo features to activate.
features: Option<String>,
/// Use cargo's `--no-default-features` flag during compilation.
no_default_features: bool,
},
}

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"/>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants