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 API_URL_ROOT = process.env.NEXT_PUBLIC_API_URL_ROOT || "";
|
||||||
export const URL_MEDIA = process.env.NEXT_PUBLIC_URL_MEDIA || "";
|
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 = {
|
export const API = {
|
||||||
User : {
|
User : {
|
||||||
CURRENT: `${API_URL_ROOT}/users/current`,
|
CURRENT: `${API_URL_ROOT}/users/current`,
|
||||||
|
|||||||
@@ -19,6 +19,34 @@ const nextConfig: NextConfig = {
|
|||||||
],
|
],
|
||||||
},
|
},
|
||||||
output: 'standalone',
|
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) {
|
webpack(config) {
|
||||||
config.module.rules.push({
|
config.module.rules.push({
|
||||||
test: /\.svg$/,
|
test: /\.svg$/,
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "next dev",
|
"dev": "next dev",
|
||||||
|
"dev:3008": "next dev -p 3008",
|
||||||
"build": "next build",
|
"build": "next build",
|
||||||
"start": "next start",
|
"start": "next start",
|
||||||
"lint": "eslint ."
|
"lint": "eslint ."
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import axios, { AxiosError, InternalAxiosRequestConfig } from "axios";
|
import axios, { AxiosError, InternalAxiosRequestConfig } from "axios";
|
||||||
|
|
||||||
export const baseURL =
|
// Prefer same-origin requests. Next.js will proxy API paths via `next.config.ts` rewrites.
|
||||||
process.env.NEXT_PUBLIC_API_URL_ROOT || process.env.NEXT_PUBLIC_API_URL || "http://localhost:3344";
|
export const baseURL = "/";
|
||||||
|
|
||||||
export const api = axios.create({
|
export const api = axios.create({
|
||||||
baseURL,
|
baseURL,
|
||||||
@@ -68,7 +68,7 @@ api.interceptors.response.use(
|
|||||||
isRefreshing = true;
|
isRefreshing = true;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await axios.post(`${baseURL}/auth/refresh`, undefined, { withCredentials: true });
|
await axios.post("/auth/refresh", {}, { withCredentials: true });
|
||||||
|
|
||||||
processQueue(null);
|
processQueue(null);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user