Should I expect the environment variables defined ...
# help
r
Should I expect the environment variables defined within the NextjsSite construct to be available to my site during getServerSideProps within process.env? It doesn't seem like they are coming through (getting a
Only absolute URLs...
error in cloudwatch logs for the main site edge function) unless I also define a .env.local file within the frontend directory
t
The variables passed through SST are only available after build time. I'm not sure when
getServerSideProps
runs but if it's during build time it can't be set through the
environment
field
Need to rely on an env file like you have there
We maybe need better naming but the
environment
property values only get replaced after a full deploy so we can get the values of potentially newly created resources
r
In my case they are used when the visitor accesses the site (e.g. after deploy), during server side rendering.. If I look at the deployment bundle for the main edge function, I still see references to process.env.{variable} which I guess is why they are not resolving without having the .env file
t
can I see how you're defining the NextJsSite?
r
all the env variables are just passed in strings
t
Yeah that should work
is this for sst start or sst deploy?
r
sst deploy
t
hm that should be working
What we do is when building nextjs, we pass in placeholders in the env, like
tenant={{ TENANT }}
. Then the build should get generated with the literal
{{ TENANT }}
and once everything is done deploying we replace those with real values in s3
At least that's what we do for staticsite, nextjs maybe different and @Frank has more insight on how it works
r
Seems like the assets uploaded to S3 have the placeholders replaced, but the lambda code that generates the server-side rendered content maybe does not
f
@Roger Chi, can u check in ur package.json which version of SST r u using?
r
Copy code
"dependencies": {
    "@serverless-stack/cli": "0.43.3",
    "@serverless-stack/resources": "0.43.3",
    "@aws-cdk/core": "1.114.0"
  }
f
that’s on the latest.
Let’s try this: 1. go into ur Lambda console; 2. look for a Lambda function that starts with
MainFunction
in its name; 3. open up the
Code
tab 4. in the file explorer on the left, select
pages
and open up the file for the page that has
process.env
in
getServerSideProps()
5. search for ie.
{{ BASE_URL }}
Let me know if it’s getting replaced.
r
I import a helper module so don't directly call process.env from within the pages -- if I look in the chunks, I can see references to process.env.BASE_URL but not {{ BASE_URL }}
I'll add a quick line of code to my page that uses the env variable to see what the behavior is
Doesn't look like the process.envs get converted to {{ variable }} for the lambda functions
that's from the deployment bundle for MainFunction
f
Oh that’s weird…
process.env
should get resolved by Next.js on build 🤔
So what SST does is that on build, it runs
Copy code
BASE_URL={{ BASE_URL }} next build
And the code above should look like:
Copy code
console.log("{{ BASE_URL }}");
And on deploy, SST will then replace “{{ BASE_URL }}” with the real value.
It seems weird that Next.js is leaving
process.env.BASE_URL
there in ur code.
Just to clarify, the screenshot you shared above with
console.log(process.env.BASE_URL)
, you are seeing that in ur Lambda console? A page within the
pages
directory in the file explorer?
r
My code is too large for the lambda console, but it's from the exported deployment package
f
I see. If you go into ur SST app’s
.build
folder, can you show me what’s in there?
Can you open up
default-lambda
?
Then in pages > dashboard > index.js, do you see
console.log(process.env.BASE_URL)
in there?
r
Yes
f
Can you try running
npm run build
inside your Next.js app?
Give me a min. Let me dig into this a bit.
r
Thank you for your help, it’s very much appreciated
f
Ah I managed to reproduce the issue on my end.
I will cut a release with the fix tonight.
Will keep you posted!
r
👍
f
Hey @Roger Chi, I just released v0.43.4 with the fix.
You can update by running:
Copy code
npm install --save --save-exact @serverless-stack/cli@0.43.4 @serverless-stack/resources@0.43.4
Give it a try and let me know if it works for you!
w
I had a similar problem which I resolved by exposing the environment variables to the nextjs javascript bundle via next.config.js as described here and here
f
Hey @Warwick Grigg with the new v0.43.4 update, you shouldn't need to define in
next.config.js
any more!
w
@Frank OK, I'll give that a try. Thanks! I wasn't sure whether that fix was just for server side rendering, or for all the nextjs lambdas
r
Thanks @Frank, looks to be working perfectly now
f
AWESOME! 👍