移除 next.config.js 中的 standalone 输出配置,添加 lib/api-client.ts 封装 fetch 请求, 自动为所有 API 调用添加配置的 basePath。更新所有页面和组件中的 fetch 调用以使用新的 fetchApi 函数, 确保在部署到子路径时 API 请求能正确工作。同时调整 tsconfig.json 的格式并修复项目创建时的字段名。
23 lines
640 B
TypeScript
23 lines
640 B
TypeScript
// 必须与 next.config.js 中的 basePath 保持一致
|
|
const BASE_PATH = '/ok-r';
|
|
|
|
/**
|
|
* 获取带 basePath 的完整 API URL
|
|
* @param path API 路径,例如 '/api/users'
|
|
*/
|
|
export function getApiUrl(path: string) {
|
|
if (path.startsWith('http')) return path;
|
|
const normalizedPath = path.startsWith('/') ? path : `/${path}`;
|
|
return `${BASE_PATH}${normalizedPath}`;
|
|
}
|
|
|
|
/**
|
|
* 封装 fetch 请求,自动处理 basePath
|
|
* @param path API 路径,例如 '/api/users'
|
|
* @param init fetch 配置项
|
|
*/
|
|
export async function fetchApi(path: string, init?: RequestInit) {
|
|
const url = getApiUrl(path);
|
|
return fetch(url, init);
|
|
}
|