aspect-build / aspect-build/rules_js

How to set env for nextjs ?

Open
#2,228 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Starlark
Stars
378
Forks
183
Avg merge
1d 9h
Merged PRs (30d)
32

Description

Hi,

I have a nextJS app and using the nextjs macros (thank you so much for adding these btw! )

```
nextjs(
name = "next",
srcs = [
"//src/app",
],
config = "next.config.mjs",
data = [
"//:node_modules/next",
"//:node_modules/react",
"//:node_modules/react-dom",
"//:node_modules/styled-jsx",
"//:node_modules/typescript",
"//:package_json",
],
next_js_binary = "//:next_js_binary",
)
```

This works perfectly fine when I do `bazel run next.dev`. However, if I do
`bazel run next.start`, I have issues when nextJS tries to pre-render the page (e.g. `/`). Specifically, some required environment variables are not set.

How do I make sure that the env variables are set (i.e. available in `process.env`) when nextJS is pre-rendering some routes during `next.start`?

____

I tried playing around with passing `env = {'MY_VAR': 'xxx'}` in the `nextjs` rule, and that works, but 'xxx' must be hardcoded, so that I cannot really use it (`MY_VAR` can be some API token, for example, so I cannot hardcode it into the `BUILD` file).

I tried also using `bazel run --action_env=MY_VAR next.start` but here it's also not able to find the `MY_VAR` definition.

What is the recommended approach here?

Thanks a lot for your help!

Contributor guide

Open the contributing guide

Research direction

Start with the nextjs rule invocation and the next.start execution path described in the issue. Reproduce the environment-variable behavior, comparing the env attribute with bazel run --action_env=MY_VAR. Done means identifying and documenting or implementing a supported way to make runtime variables available during Next.js prerendering without hardcoding secrets in BUILD.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nextjs
Domain
build-system, web-dev
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.