fetch & env vars with app router & static builds #50759
-
Hey folks, I am desiring to use app router with export async function UsersView() {
const resp = await fetch(
`${process.env.API_BASE_URL}/api/users`
);
// ...
} The value for the env var is not known at build time – and the API is also not available at build time. This causes issues such as the following when running
I can kind of circumvent the issue by disabling caching for the To clarify: The definition of env vars is working fine. This question/issue is specifically about the fact that the API is not known/available at build time. How are others handling this? UpdateTurned this into a Next.js issue #50828 |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 28 replies
-
Make sure you follow the
Your
Reference |
Beta Was this translation helpful? Give feedback.
-
I have a similar case. My infrastructure is configured in a such way that my frontend and backend are building in parallel. So during the Next build phase my API server does not work - it cannot respond to requests with data. I understand that I can use requests without caching, but I want to take full advantage of static generation and caching. Is it possible to set this up somehow? |
Beta Was this translation helpful? Give feedback.
-
There is now a workaround for this that leverages Next.js' |
Beta Was this translation helpful? Give feedback.
There is now a workaround for this that leverages Next.js'
headers
andmiddleware
capability to circumvent these current limitations. For more information, see this issue comment.#50828 (comment)