Hi again friends! The NextJS integration was recen...
# help
a
Hi again friends! The NextJS integration was recently added and since the project I'm working on uses both SST and NextJS I figured I should combine them, but I'm having a slight issue with the environment variables, when trying to deploy it seems that the environment variables that I have configured in the construct are not actually getting injected at all (logging them out during build in NextJS just logs
NEXT_PUBLIC_API_URI: '{{ NEXT_PUBLIC_API_URI }}'
) as such any fetch's that I have in next are throwing a
Only absolute URLs are supported
error. Any help would be greatly appreciated 🙂
t
The issue is at build time we inject placeholders which are then replaced once the site is fully deployed to to s3/cloudfront
We don't have the variable values available for replacement until after stack deployment is finished
So logging at build time will show the wrong values. Can you log when the application is actually running to see the values?
a
Hmm gotcha, so is there any way to have them injected at build time, in any way, since I use fetch in
getStaticProps
and without it there during build time it doesn't have them
Sadly I can't really log them with it running since it fails to deploy because of this
t
Yeah you might need to rethink your architecture because technically the API values aren't available during build time
a
Copy code
environment: {
  NEXT_PUBLIC_API_URI: api.customDomainUrl!,
  NEXT_PUBLIC_CMS_URI: process.env.CMS_URI!,
}
For context this is the only env I have in there
As such they are both static env variables, since they are both declared prior
Normally I would just have these in vercel or whatever for build time, or if building locally I'd have it in the
.env.production
in the Nextjs dir but
I guess I can hard code them in using node_env but I'd rather not do that if I can help it
Is there no easy way to get around this without having to go through every one of my files and hardcode it? 😄
t
might want to wait to see if @Frank has any ideas, I only have a high level understanding of the nextjs construct
a
For now I got away with manually setting them in my next config
t
What I'd suggest is to implement a function that returns the url
If you're seeing a placeholder in that value, return a hardcoded api string
Otherwise just return the value
f
@thdxr thanks for jumping on this
Hey @ACPixel, can you show me where you are referencing
NEXT_PUBLIC_API_URI
in ur Next.js app?
a
@Frank it's primarily being used in getStaticProps
Copy code
export const getStaticProps: GetStaticProps = async (ctx) => {
  if (process.env.NODE_ENV === "development") {
    //@ts-ignore
    process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;
  }

  let _locale = ctx.locale;
  let topThree = await (
    await fetch(
      `${process.env.NEXT_PUBLIC_CMS_URI}/Reviews?_limit=3&_locale=${_locale}`
    )
  ).json();

  let pageInfo = await (
    await fetch(
      `${process.env.NEXT_PUBLIC_CMS_URI}/home-page?_locale=${_locale}`
    )
  ).json();

  return {
    revalidate: 120,
    props: {
      topThree,
      pageInfo,
    },
  };
};
those are both using the CMS variable and not the API, but close enough
f
I see. Unlike
getServerSideProps
,
getStaticProps
needs the values at build time. And reading deployed values at build time is tricky.
The value for
NEXT_PUBLIC_CMS_URI
wouldn’t exist on the first deploy b/c the API hasn’t been deployed yet.
On subsequent deploys, the value for
NEXT_PUBLIC_CMS_URI
could change at deploy time, so you could be reading a wrong value at build time.
You could either hardcode the URI, or you can save the URI to an SSM parameter and call AWS SDK in
getStaticProps
to read from SSM.
Let me know if that makes sense.
a
Thank you @Frank that makes sense I will look into that 😄
It would be really cool in the future if we could specify build time env vars for at least the nextjs construct 🙂