Spectrum is now read-only. Learn more about the decision in our official announcement.


Fast in every way that matters. Gatsby is a free and open source framework based on React that helps developers build blazing fast websites and apps.


Is gatsby-ssr.js working with netlify?

May 11, 2020 at 6:13am

Is gatsby-ssr.js working with netlify?

May 11, 2020 at 6:13am
Hi folks! I have several websites from this same code, in one of them I would like to add script to bottom <body>.
In gatsby-ssr.js I have someting like that:
const React = require("react");
require("dotenv").config({ path: .env.${process.env.NODE_ENV} });
exports.onRenderBody = ({ setPreBodyComponents }) => { setPreBodyComponents([
process.env.WEBSITE_WITH_SCRIPT && (
alert( 'Hello, world!' );
</script> ) ]); };
but' it doesn't work.. Do you have any idea how to fix it?

May 11, 2020 at 6:27am
Try putting the dotenv call in gatsby-config, and make the variable GATSBY_WEBSITE_WITH_SCRIPT (still needs to be referenced like process.env.GATSBY_WEBSITE_WITH_SCRIPT).
gatsby-ssr is processed with Webpack, so it probably isn’t picking up the env variable in the way you would expect. Putting dotenv in gatsby-config causes the variables to get picked up by Node, and the GATSBY_ prefix will tell Gatsby to include its value in the Webpack configuration

June 16, 2021 at 8:40pm
(js-brecht) I don't think this works. I have all this in my gatsby-config and still don't see env vars in my ssr file.
(tbaustin) I just generated a new repo using gatsby new test-site && cd test-site.
I added this to gatsby-config:
process.env.GATSBY_TEST_VAR = "I AM HERE";
I added this to gatsby-ssr:
export const onRenderBody = () => {
I am attaching an image of the output. Notice the I AM HERE log output