config.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { settings } from 'app/customization/settings';
  2. export const MULTITENANT = appSetting('config', 'multitenant');
  3. export const MULTITENANT_IMAGES_PROXY = appSetting('config', 'multitenant_images_proxy');
  4. export const APP_URL = appSetting('config', 'app_url') ;
  5. // Strip trailing slashes — UNA nginx 308s `//path` and relative Location breaks img/fetch.
  6. export const UNA_URL = String(appSetting('config', 'una_url') || '').replace(/\/+$/, '');
  7. export const UNA_API_KEY = appSetting('config', 'una_api_key');
  8. export const PREVIEW_DOMAIN = String(appSetting('config', 'preview_domain') || '');
  9. export const PREVIEW_API_KEY = appSetting('config', 'preview_api_key');
  10. export const APP_ORIGIN = appSetting('config', 'app_origin');
  11. /**
  12. * Join UNA origin + path without creating a double slash.
  13. * Pass `base` for multi-tenant overrides (defaults to configured UNA_URL).
  14. */
  15. export function joinUnaUrl(path = '', base = UNA_URL) {
  16. const resolvedBase = String(base || '').replace(/\/+$/, '');
  17. const suffix = String(path || '');
  18. if (!resolvedBase) return suffix;
  19. if (!suffix) return resolvedBase;
  20. if (/^https?:\/\//i.test(suffix)) return suffix;
  21. return `${resolvedBase}/${suffix.replace(/^\/+/, '')}`;
  22. }
  23. export function appSetting(section, name, path) {
  24. appSetting.cacheSettings = appSetting.cacheSettings || {};
  25. const cacheKey = path ? `${section}_${name}_${path}` : `${section}_${name}`;
  26. if (appSetting.cacheSettings.hasOwnProperty(cacheKey)) {
  27. return appSetting.cacheSettings[cacheKey];
  28. }
  29. const sectionData = settings?.[section];
  30. const result = path ? sectionData?.[name]?.[path] : sectionData?.[name];
  31. appSetting.cacheSettings[cacheKey] = result ?? '';
  32. return appSetting.cacheSettings[cacheKey];
  33. }
  34. export function getBaseUrl() {
  35. // Client-side
  36. if (typeof window !== "undefined") {
  37. return `${window.location.protocol}//${window.location.host}`;
  38. }
  39. // Server-side / SSR (shared bundle has no next/headers — prefer configured app URL)
  40. const appUrl = typeof APP_URL === 'string' ? APP_URL.replace(/\/$/, '') : '';
  41. if (appUrl) {
  42. return appUrl;
  43. }
  44. // No request context in the shared bundle (next/headers is app-only).
  45. return 'http://localhost:3000';
  46. }