I need to use the root domain of a page in a view that i inject in the html via javascript:
<meta property="og:url" name="twitter:url" itemprop="url" content="<%- root_domain %><%- local_path %>">
The problem is that today I kind of do it the "hard-coded" way, as I set it always equal to https://www.example.com
but in our Netlify CI the root domain change son every build, like deploy-preview-118--xyz-434545.netlify.com
on one build, then deploy-preview-119--xyz-434545.netlify.com
, and so on...
So I would like NOT to set hard-coded the root_domain to https:://www.example.com.
In a node app with webpack self-conscious of the root domain ? Is there a method that if I use always give the current root domain ? Coming from the Rails ecosystem, we have root_path
method which always equal to the root domain (here it would be example.com
, deploy-preview-118--xyz-434545.netlify.com
, deploy-preview-119--xyz-434545.netlify.com
...).
Not sure it plays a role on this question, but we use https://github.com/markdalgleish/static-site-generator-webpack-plugin plugin so we have a locals.path method but it gives us everything AFTER the root domain while what we want is the root domain.
Based on our discussion above.
After looking at Netlify's documentation, They provide several built in environmental variables which include, URL
, DEPLOY_URL
and DEPLOY_PRIME_URL
Reference: netlify-build-environment-variables
You should be able to use these in your build to set the <%- root_domain %>
variable.
Environmental variables can be accessed in node.js
using process.env
.
So in your webpack config would pass root url to the static site generator.
plugins: [
new StaticSiteGeneratorPlugin({
locals: {
root_domain: process.env.DEPLOY_PRIME_URL || 'https://www.example.com'
}
})
]
where https://www.example.com
would be used for local development or in the absence of an environmental variable.