Skip to content

Instantly share code, notes, and snippets.

@mgaebler
Last active April 13, 2021 07:14
Show Gist options
  • Save mgaebler/18d5067a15d6f669eb16bea06f6f13dd to your computer and use it in GitHub Desktop.
Save mgaebler/18d5067a15d6f669eb16bea06f6f13dd to your computer and use it in GitHub Desktop.
next-js

tests

context: generated static files like NEXT_PUBLIC_APP_VERSION

✅ - local system uses .env ✅ - local system uses system variables ✅ - eb local run, with docker container uses .env ✅ - eb local run, with docker container uses .env.local

❌ - eb local run, with docker container uses environment variables passed through

eb local run --envvars NEXT_PUBLIC_APP_VERSION=docker_test
# tested with
docker exec -it 93555a461718 /bin/sh env
# local variable is available, but not used while building

✅ - eb local run, with docker container uses environment variables in dockerfile

# this has to explicitly run in the build block 🙃
ENV NEXT_PUBLIC_APP_VERSION test_local_docker
RUN yarn build

Here is some discussion how to fix this problem. But this only helps if you're able to set build params. We're not able to use this in elastic beanstalk.

vercel/next.js#13427

At least I would discard this scenario because it's to complicated for the outcome.

  • You have to understand the difference between 'static site generation' and 'dynamic site generation' in nextjs context and especially why information is only available in a certain point of time.

  • You have also to understand how docker builds work and how to parameterize them. (build-args)

I think it's a better approach to use dynamic rest-api endpoints to get the information you need.

export default (req: NextApiRequest, res: NextApiResponse): void => {
  res.status(200).json({
    name: 'John Doe',
    version: process.env.NEXT_PUBLIC_APP_VERSION,
    env: process.env.NODE_ENV,
  });
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment