Add configurable internal API URL

This commit is contained in:
Mike Cao committed 2026-05-09 01:06:52 -07:00
1 parent fa182d0947
commit 2fd319f910
6 files changed
+124 -4

No files matched your search

+38 -1
View File
@@ -8,6 +8,7 @@ const TRACKER_SCRIPT = '/script.js';
const isProd = process.env.NODE_ENV === 'production';
const apiUrl = process.env.API_URL || '';
const basePath = process.env.BASE_PATH || '';
const cloudMode = process.env.CLOUD_MODE || '';
const cloudUrl = process.env.CLOUD_URL || '';
@@ -22,12 +23,31 @@ const trackerScriptURL = process.env.TRACKER_SCRIPT_URL || '';
const selfTrack = process.env.UMAMI_SELF_TRACK || '';
const selfRecord = process.env.UMAMI_SELF_RECORD || '';
function getUrlOrigin(url: string) {
try {
return new URL(url).origin;
} catch {
return '';
}
}
function isRelativeUrl(url: string) {
return Boolean(url && !/^https?:\/\//i.test(url));
}
function normalizePath(url: string) {
return `/${url.replace(/^\/+|\/+$/g, '')}`;
}
const apiUrlOrigin = getUrlOrigin(apiUrl);
const connectSrc = ["'self'", 'https:', apiUrlOrigin].filter(Boolean).join(' ');
const contentSecurityPolicy = `
default-src 'self';
img-src 'self' https: data:;
script-src 'self' 'unsafe-eval' 'unsafe-inline';
style-src 'self' 'unsafe-inline';
connect-src 'self' https:;
connect-src ${connectSrc};
frame-src 'self' http: https:;
frame-ancestors 'self' ${frameAncestors};
`;
@@ -123,6 +143,22 @@ if (collectApiEndpoint) {
});
}
if (isRelativeUrl(apiUrl)) {
const normalizedApiUrl = normalizePath(apiUrl);
if (normalizedApiUrl !== '/' && normalizedApiUrl !== '/api') {
headers.push({
source: `${normalizedApiUrl}/:path*`,
headers: apiHeaders,
});
rewrites.push({
source: `${normalizedApiUrl}/:path*`,
destination: '/api/:path*',
});
}
}
const redirects = [
{
source: '/teams/:id/dashboard/edit',
@@ -188,6 +224,7 @@ if (isProd && cloudMode) {
export default withNextIntl({
reactStrictMode: false,
env: {
apiUrl,
basePath,
cloudMode,
cloudUrl,