Fix apiFetch basePath: use publicRuntimeConfig instead of NEXT_PUBLIC_ env var

NEXT_PUBLIC_* vars are inlined at build time — not available in Docker build context.
publicRuntimeConfig is resolved at runtime from next.config.mjs, which works correctly.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Vadym Samoilenko 2026-03-19 22:32:49 +00:00
parent 4e9ce79a3c
commit a99e18c744
2 changed files with 8 additions and 1 deletions

View file

@ -2,9 +2,13 @@
* Wrapper around fetch that prepends the Next.js basePath so API calls
* reach the correct backend when deployed under a sub-path (e.g. /ppt-tool).
*
* basePath is read from next.config.mjs at build time via next/config.
* Usage: apiFetch('/api/v1/...', options) identical to fetch(), just works.
*/
const BASE_PATH = process.env.NEXT_PUBLIC_BASE_PATH ?? '';
import getConfig from 'next/config';
const { publicRuntimeConfig } = getConfig() ?? {};
const BASE_PATH: string = publicRuntimeConfig?.basePath ?? '';
export function apiFetch(path: string, init?: RequestInit): Promise<Response> {
const url = path.startsWith('/api/') ? `${BASE_PATH}${path}` : path;

View file

@ -4,6 +4,9 @@ const API_URL = process.env.API_INTERNAL_URL || 'http://localhost:8000';
const nextConfig = {
basePath: "/ppt-tool",
assetPrefix: "/ppt-tool",
publicRuntimeConfig: {
basePath: "/ppt-tool",
},
reactStrictMode: false,
distDir: ".next-build",