Can you use an environment variable in the preview function?

default discord avatar
mauz.
2 months ago
5

I mean, from console logging, I can see that the function is actually executed on the client, not the server, so

process.env

is not really available. Is there any other option to pass that variable?

  • default discord avatar
    notchr
    2 months ago

    @mauz. I could be wrong, but I believe you can prefix env vars with PUBLIC, or some keyword that allows it to be available for the client



    I would check the official doc on this, I forget it off the top of my head



    Payload will automatically supply any present env variables that are prefixed with PAYLOAD_PUBLIC_ directly to the Admin panel.
    
    For example, if you've got the following environment variable:
    
    PAYLOAD_PUBLIC_STRIPE_PUBLISHABLE_KEY=pk_test_XXXXXXXXXXXXXXXXXX
    
    This key will automatically be made available to the Payload bundle and can be referenced in your Admin component code as process.env.PAYLOAD_PUBLIC_STRIPE_PUBLISHABLE_KEY.


    https://payloadcms.com/docs/admin/webpack#admin-environment-vars
  • default discord avatar
    mauz.
    2 months ago

    Thanks a lot!

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.