Hiding secrets from the frontend / client side #19996
-
I have the following page and secret server function page with getServerSidePropsmodule.exports.env = {
SECRET: '..."
} page with getServerSidePropsimport secretMethod from 'server/secretMethod'
export default function MyPage(props) {
...
}
export function getServerSideProps() {
const props = await secretMethod()
return { props }
} secretMethod.js
The question is: How to avoid that process.env.SECRET being exposed to the client side? According to this answer on Stack Overflow the way to achieve that is to NOT USE next.config.js and instead of a .env file on the repository. Of course you don't want to push the .env files to |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
I'd strongly recommend using Next's newer built in environment variable support. This will ensure that server secrets stay on the server and client secrets stay on the client. With that said, if you are using the next.config.js environment variable support then the variables you use will only be exposed if you expose them in client code. If you are only using |
Beta Was this translation helpful? Give feedback.
I'd strongly recommend using Next's newer built in environment variable support. This will ensure that server secrets stay on the server and client secrets stay on the client.
With that said, if you are using the next.config.js environment variable support then the variables you use will only be exposed if you expose them in client code. If you are only using
process.env.SECRET
in yoursecretMethod
and that method is only used ingetServerSideProps
then it will not be exposed to the client.