chore: proxy backend routes via Next rewrites
This commit is contained in:
@@ -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`,
|
||||
|
||||
@@ -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$/,
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"dev": "next dev",
|
||||
"dev:3008": "next dev -p 3008",
|
||||
"build": "next build",
|
||||
"start": "next start",
|
||||
"lint": "eslint ."
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user