Project environment variables in build #70
timkelty
started this conversation in
Feature Requests & Enhancements
Replies: 1 comment 2 replies
-
|
Does this limitation mean it is currently not possible to use something like the Sentry JS SDK with environment variables import * as Sentry from '@sentry/browser';
if (import.meta.env.PROD) {
Sentry.init({
dsn: import.meta.env.VITE_SENTRY_DSN,
environment: import.meta.env.VITE_CRAFT_ENVIRONMENT
});
}I know Vite cannot use standard .env vars directly, but no Or as a workaround, are we looking at injecting these at runtime e.g. via Twig in the main core layout and accessing via something like: In main app layout in Twig: <script>
window.SENTRY_CONFIG = {
dsn: '{{ getenv("SENTRY_DSN")|e("js") }}',
environment: '{{ getenv('CRAFT_ENVIRONMENT')|e("js") }}',
release: '{{ getenv('GIT_SHA') }}'
};
</script>or {% js at head %}
window.SENTRY_CONFIG = {
dsn: '{{ getenv("SENTRY_DSN")|e('js') }}',
environment: '{{ getenv('CRAFT_ENVIRONMENT')|e('js') }}',
release: '{{ getenv('GIT_SHA') }}'
};
{% endjs %}Then in main js app file: import * as Sentry from '@sentry/browser';
if (window.SENTRY_CONFIG?.dsn) {
Sentry.init(window.SENTRY_CONFIG);
}The DSN is technically not a secret, but you still couldn't do sourcemaps as that needs an auth token which very much is and can't be exposed. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Build steps (composer, node, etc) currently do not run with the project's environment variables.
Use-cases:
VITE_*configurationSENTRY_AUTH_TOKENto send sourcemaps to Sentry on buildBeta Was this translation helpful? Give feedback.
All reactions