chore: proxy backend routes via Next rewrites

This commit is contained in:
taDuc
2026-05-10 02:55:45 +07:00
parent b3e765b6f1
commit 0d6599015b
4 changed files with 37 additions and 6 deletions
+4 -2
View File
@@ -1,6 +1,8 @@
export const API_URL_ROOT = process.env.NEXT_PUBLIC_API_URL_ROOT || "";
export const URL_MEDIA = process.env.NEXT_PUBLIC_URL_MEDIA || "";
export const HOME_URL = process.env.NEXT_PUBLIC_HOME_URL || "http://localhost:3000";
export const HOME_URL =
process.env.NEXT_PUBLIC_HOME_URL ||
(typeof window !== "undefined" ? window.location.origin : "");
export const API = {
User : {
CURRENT: `${API_URL_ROOT}/users/current`,
@@ -70,4 +72,4 @@ export const API = {
GET_ALL: `${API_URL_ROOT}/statistics`,
GET_BY_DATE: (date: string) => `${API_URL_ROOT}/statistics/${date}`,
}
}
}
+29 -1
View File
@@ -19,6 +19,34 @@ const nextConfig: NextConfig = {
],
},
output: 'standalone',
async rewrites() {
// Proxy backend API calls through Next.js to avoid browser CORS issues.
//
// Configure the target via:
// - API_PROXY_TARGET (server-side, recommended) e.g. http://localhost:8080
// - NEXT_PUBLIC_API_URL_ROOT (fallback)
const target =
process.env.API_PROXY_TARGET ||
process.env.NEXT_PUBLIC_API_URL_ROOT ||
"https://history-api.kain.id.vn";
const prefixes = [
"auth",
"users",
"media",
"projects",
"submissions",
"statistics",
"roles",
"historian",
];
return [
...prefixes.map((p) => ({
source: `/${p}/:path*`,
destination: `${target}/${p}/:path*`,
})),
];
},
webpack(config) {
config.module.rules.push({
test: /\.svg$/,
@@ -37,4 +65,4 @@ const nextConfig: NextConfig = {
},
};
export default nextConfig;
export default nextConfig;
+1
View File
@@ -4,6 +4,7 @@
"private": true,
"scripts": {
"dev": "next dev",
"dev:3008": "next dev -p 3008",
"build": "next build",
"start": "next start",
"lint": "eslint ."
+3 -3
View File
@@ -1,7 +1,7 @@
import axios, { AxiosError, InternalAxiosRequestConfig } from "axios";
export const baseURL =
process.env.NEXT_PUBLIC_API_URL_ROOT || process.env.NEXT_PUBLIC_API_URL || "http://localhost:3344";
// Prefer same-origin requests. Next.js will proxy API paths via `next.config.ts` rewrites.
export const baseURL = "/";
export const api = axios.create({
baseURL,
@@ -68,7 +68,7 @@ api.interceptors.response.use(
isRefreshing = true;
try {
await axios.post(`${baseURL}/auth/refresh`, undefined, { withCredentials: true });
await axios.post("/auth/refresh", {}, { withCredentials: true });
processQueue(null);