Skip to content

Instantly share code, notes, and snippets.

@potato4d
Created November 6, 2020 03:17
Show Gist options
  • Save potato4d/6962fb3fc2c1c3370556897ff77f9e38 to your computer and use it in GitHub Desktop.
Save potato4d/6962fb3fc2c1c3370556897ff77f9e38 to your computer and use it in GitHub Desktop.
Nuxt.js use enviroment variables in app.html file
<!DOCTYPE html>
<html {{ HTML_ATTRS }}>
<script async src="https://www.googletagmanager.com/gtag/js?id={{ ENV.GA_ID }}"></script>
<script>
console.log(`GA_ID: {{ ENV.GA_ID }}`)
</script>
<head>
{{ HEAD }}
</head>
<body {{ BODY_ATTRS }}>
{{ APP }}
</body>
</html>
export default {
mode: 'universal',
target: 'server',
env: {
GA_ID: 12345
},
}
@aldy-san
Copy link

Not working in production, any solution?

@tomardern
Copy link

A few years late here, but I've managed to get it to work via this PR - https://github.com/nuxt/nuxt/pull/6505/files

see https://github.com/nuxt/nuxt/tree/bb9427ee743a556761105dd0c4e7e474e922aee6/examples/custom-template for an example.

html:

 <h1>Hello {{ ENV.HELLO }}</h1>

nuxt.config.js

export default {
// ...
 env: {
    HELLO: 'world',
  },
// ... 
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment