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:
parent
e511ec529f
commit
247f02aaf2
6 changed files with 17 additions and 5 deletions
7
.dockerignore
Normal file
7
.dockerignore
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
node_modules
|
||||
.next
|
||||
.env
|
||||
.env.local
|
||||
.env.development
|
||||
.env.production
|
||||
.git
|
||||
|
|
@ -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*`,
|
||||
},
|
||||
];
|
||||
},
|
||||
|
|
|
|||
|
|
@ -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' },
|
||||
|
|
|
|||
|
|
@ -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',
|
||||
},
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue