fix(docker): runtime API_URL for SSR in Docker containers

NEXT_PUBLIC_* vars are baked at build time and can't change at runtime.
Server-side rendering (getInitialProps, getServerSideProps, rewrites)
now reads process.env.API_URL at runtime, falling back to the baked
NEXT_PUBLIC_API_URL for client-side code.

This allows the Docker image to connect to trade-api via Docker DNS
while the browser still uses the public URL.

Co-Authored-By: Charon <charon@lethean.io>
This commit is contained in:
Claude 2026-04-03 12:50:39 +01:00
parent e511ec529f
commit 247f02aaf2
No known key found for this signature in database
GPG key ID: AF404715446AEB41
6 changed files with 17 additions and 5 deletions

7
.dockerignore Normal file
View file

@ -0,0 +1,7 @@
node_modules
.next
.env
.env.local
.env.development
.env.production
.git

View file

@ -42,10 +42,12 @@ const nextConfig = {
return config;
},
async rewrites() {
const apiUrl =
process.env.API_URL || process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3336';
return [
{
source: '/api/:path*',
destination: `${process.env.NEXT_PUBLIC_API_URL}/api/:path*`,
destination: `${apiUrl}/api/:path*`,
},
];
},

View file

@ -1,7 +1,7 @@
import PeriodState from '@/interfaces/states/pages/dex/trading/InputPanelItem/PeriodState';
import SelectValue from '@/interfaces/states/pages/dex/trading/InputPanelItem/SelectValue';
export const API_URL = process.env.NEXT_PUBLIC_API_URL;
export const API_URL = process.env.NEXT_PUBLIC_API_URL || process.env.API_URL;
export const periods: PeriodState[] = [
// { name: '1s', code: '1sec' },

View file

@ -97,7 +97,9 @@ App.getInitialProps = async (context: AppContext) => {
try {
const pageProps = await NextApp.getInitialProps(context);
const configRes = await axios.get(`${API_URL}/api/config`, {
const serverApiUrl =
typeof window === 'undefined' ? process.env.API_URL || API_URL : API_URL;
const configRes = await axios.get(`${serverApiUrl}/api/config`, {
headers: {
'Content-Type': 'application/json',
},

View file

@ -14,7 +14,8 @@ export const getServerSideProps: GetServerSideProps = async (context) => {
}
try {
const idFound = await findPairID(first as string, second as string, API_URL);
const serverApiUrl = process.env.API_URL || API_URL;
const idFound = await findPairID(first as string, second as string, serverApiUrl);
console.log('ID found:', idFound);

View file

@ -31,7 +31,7 @@ import { CancelAllData } from '@/interfaces/fetch-data/cancel-all-orders/CancelA
import CancelAllRes from '@/interfaces/responses/orders/CancelAllRes';
const isServer = typeof window === 'undefined';
const baseUrl = isServer ? API_URL : '';
const baseUrl = isServer ? process.env.API_URL || API_URL || '' : '';
export async function getUser(): Promise<ErrorRes | GetUserRes> {
return axios