Hello everyone!
We are having issues using a secret environment variable in our next.js getStaticProps functions.
We created a set of public and private environment variables in each context of our application through the netlify.toml
file as stated here
Public next.js variables are working as expected server and client side, but our secret variables are undefined
by the time the on demand functions access them. Here is a sample code of our implementation:
[build]
command = "npm run build"
publish = "out"
[[plugins]]
package = "@netlify/plugin-nextjs"
[functions]
directory = "functions/"
[context.production.environment]
SECRET = "prod-dummy-secret"
NEXT_PUBLIC_VAR = "prod-example"
[context.staging.environment]
SECRET = "staging-dummy-secret"
NEXT_PUBLIC_VAR = "staging-example"
[context.deploy-preview.environment]
SECRET = "preview-dummy-secret"
NEXT_PUBLIC_VAR = "preview-example"
And then our page componente uses incremental static regeneration as follows:
export const getStaticPaths: GetStaticPaths<MyProps> = () => {
return {
paths: [],
fallback: 'blocking',
};
};
export const getStaticProps: GetStaticProps<Partial<MyProps>, PathParams> = async ({ params }) => {
const foo = myServiceMethod();
return {
props: { foo },
revalidate: 300,
};
};
Finally here is the service trying to access the secret var
export function myServiceMethod(): string {
try {
console.log(process.env.SECRET) // undefined
} catch (err) {
throw err;
}
}