Skip to content

Commit 2eec13c

Browse files
fix chat messages
1 parent c52ef53 commit 2eec13c

7 files changed

Lines changed: 202 additions & 5 deletions

File tree

assets/index-BZyTf0Y9.js

Lines changed: 86 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/index-CmA9LlJR.js

Lines changed: 86 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,15 @@ const FRONTEND_URL = process.env.FRONTEND_URL || "*";
2929
========================= */
3030

3131
// CORS fix cho production
32+
// Note: credentials=true cannot be used with Access-Control-Allow-Origin: '*'
33+
const allowAnyOrigin = String(FRONTEND_URL).trim() === '*';
3234
app.use(cors({
33-
origin: FRONTEND_URL,
34-
credentials: true
35+
origin: allowAnyOrigin ? true : FRONTEND_URL,
36+
credentials: !allowAnyOrigin
37+
}));
38+
app.options('*', cors({
39+
origin: allowAnyOrigin ? true : FRONTEND_URL,
40+
credentials: !allowAnyOrigin
3541
}));
3642

3743
app.use(express.json());

frontend/dist/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
}(window.location));
1717
</script>
1818
<script src="./runtime-config.js"></script>
19-
<script type="module" crossorigin src="./assets/index-DBWXNU1C.js"></script>
19+
<script type="module" crossorigin src="./assets/index-BZyTf0Y9.js"></script>
2020
<link rel="stylesheet" crossorigin href="./assets/index-DmnBBc78.css">
2121
</head>
2222
<body>

frontend/src/api.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,24 @@ function buildUrl(path) {
77
return `${API_BASE}${normalizedPath}`;
88
}
99

10+
function shouldBlockRelativeApiCall(path) {
11+
const normalizedPath = String(path || '').startsWith('/') ? path : `/${path}`;
12+
if (!normalizedPath.startsWith('/api/')) return false;
13+
if (API_BASE) return false;
14+
if (typeof window === 'undefined') return false;
15+
const hostname = String(window.location?.hostname || '');
16+
const isLocalhost = /^(localhost|127\.0\.0\.1)$/i.test(hostname);
17+
return !isLocalhost;
18+
}
19+
1020
async function request(path, options = {}) {
21+
if (shouldBlockRelativeApiCall(path)) {
22+
return {
23+
success: false,
24+
error: 'Backend API chưa được cấu hình. Cập nhật window.__API_BASE__ trong runtime-config.js.'
25+
};
26+
}
27+
1128
try {
1229
const res = await fetch(buildUrl(path), options);
1330
const text = await res.text();

frontend/src/config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ const hostname = typeof window !== 'undefined' ? window.location.hostname : '';
1010
const isLocalhost = /^(localhost|127\.0\.0\.1)$/i.test(hostname);
1111
const isGithubPages = /(^|\.)github\.io$/i.test(hostname);
1212

13+
const isInvalidGithubApiBase = isGithubPages && /(^|\.)github\.io$/i.test(String(runtimeApiBase || envApiBase));
14+
1315
const fallbackApiBase = isLocalhost ? 'http://localhost:3001' : '';
14-
const configuredApiBase = runtimeApiBase || envApiBase || fallbackApiBase;
16+
const configuredApiBase = isInvalidGithubApiBase ? '' : (runtimeApiBase || envApiBase || fallbackApiBase);
1517

1618
const runtimeOffline = typeof window !== 'undefined' ? window.__IS_OFFLINE__ : undefined;
1719
const envOffline = import.meta.env.VITE_OFFLINE_MODE === 'true';

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
}(window.location));
1717
</script>
1818
<script src="./runtime-config.js"></script>
19-
<script type="module" crossorigin src="./assets/index-DBWXNU1C.js"></script>
19+
<script type="module" crossorigin src="./assets/index-BZyTf0Y9.js"></script>
2020
<link rel="stylesheet" crossorigin href="./assets/index-DmnBBc78.css">
2121
</head>
2222
<body>

0 commit comments

Comments
 (0)