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>
16 lines
648 B
TypeScript
16 lines
648 B
TypeScript
/**
|
|
* 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.
|
|
*/
|
|
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;
|
|
return fetch(url, init);
|
|
}
|