Makes every client-side fetch('/api/...') call respect the mount prefix
when Bulwark is served behind a reverse proxy at a sub-path (e.g.
`/webmail`).
### Problem
`getPathPrefix()` (added in 1.4.13 by #XXX / d762b94) already fixes
router navigation and redirect URIs for reverse-proxy deployments.
Client-side `fetch()` calls, though, still target the browser origin:
await fetch('/api/foo')
// Browser at /webmail/en/inbox → hits /api/foo (not proxied → 404)
That means the login flow, session establishment, settings save, plugin
loader, calendar import, etc. all break the moment you front Bulwark
with nginx (or any proxy) at a sub-path.
### Fix
Add `apiFetch(input, init)` next to `getPathPrefix()` in
`lib/browser-navigation.ts`. It prepends the mount prefix to any
absolute path at call time:
await apiFetch('/api/foo')
// /webmail/en/inbox → /webmail/api/foo
// /en/inbox → /api/foo
Same runtime-detection model as `getPathPrefix()` — the built bundle
works at any mount point without rebuilding or env-var config.
Protocol-relative (`//cdn...`) and absolute (`https://...`) URLs pass
through unchanged. Server-side route handlers are untouched (the mount
prefix is a browser-only concept).
### Migration
Mechanical rewrite of every client-side `fetch('/api/...')` call in
hooks/, lib/, stores/, components/, app/ — 99 call sites across
26 files. `route.ts` handlers and other server-only files are skipped.
### Compat
- No behaviour change when mounted at `/` (the common case): an empty
prefix + raw path is identical to raw path.
- No new config knobs, env vars, or build flags.
- Supersedes PR #181 (which required a build-time `NEXT_PUBLIC_BASE_PATH`)
— will close #181 after this lands.
### Testing
Should run the existing suite; smoke-tested by Jabali Panel which
reverse-proxies Bulwark at `/webmail/` (https://github.com/shukiv/jabali-panel).
188 lines
6.3 KiB
TypeScript
188 lines
6.3 KiB
TypeScript
"use client";
|
|
|
|
import { useState, useEffect } from 'react';
|
|
import { usePolicyStore } from '@/stores/policy-store';
|
|
import { apiFetch } from '@/lib/browser-navigation';
|
|
|
|
interface ConfigData {
|
|
appName: string;
|
|
jmapServerUrl: string;
|
|
oauthEnabled: boolean;
|
|
oauthOnly: boolean;
|
|
oauthClientId: string;
|
|
oauthIssuerUrl: string;
|
|
rememberMeEnabled: boolean;
|
|
settingsSyncEnabled: boolean;
|
|
stalwartFeaturesEnabled: boolean;
|
|
devMode: boolean;
|
|
faviconUrl: string;
|
|
appLogoLightUrl: string;
|
|
appLogoDarkUrl: string;
|
|
loginLogoLightUrl: string;
|
|
loginLogoDarkUrl: string;
|
|
loginCompanyName: string;
|
|
loginImprintUrl: string;
|
|
loginPrivacyPolicyUrl: string;
|
|
loginWebsiteUrl: string;
|
|
demoMode: boolean;
|
|
autoSsoEnabled: boolean;
|
|
allowCustomJmapEndpoint: boolean;
|
|
embeddedMode: boolean;
|
|
parentOrigin: string;
|
|
}
|
|
|
|
interface AppConfig extends ConfigData {
|
|
isLoading: boolean;
|
|
error: string | null;
|
|
}
|
|
|
|
let configCache: ConfigData | null = null;
|
|
let configPromise: Promise<ConfigData> | null = null;
|
|
|
|
export async function fetchConfig(): Promise<ConfigData> {
|
|
// Return cached config if available
|
|
if (configCache) {
|
|
return configCache;
|
|
}
|
|
|
|
// If a fetch is already in progress, wait for it
|
|
if (configPromise) {
|
|
return configPromise;
|
|
}
|
|
|
|
// Start a new fetch
|
|
configPromise = apiFetch('/api/config')
|
|
.then((res) => {
|
|
if (!res.ok) {
|
|
throw new Error('Failed to fetch config');
|
|
}
|
|
return res.json();
|
|
})
|
|
.then((data) => {
|
|
configCache = data;
|
|
// Fetch admin policy alongside config (non-blocking)
|
|
usePolicyStore.getState().fetchPolicy();
|
|
return data;
|
|
})
|
|
.finally(() => {
|
|
configPromise = null;
|
|
});
|
|
|
|
return configPromise;
|
|
}
|
|
|
|
/**
|
|
* Hook to fetch runtime configuration
|
|
*
|
|
* Fetches app configuration from /api/config endpoint, which reads
|
|
* environment variables at runtime (not build time).
|
|
*
|
|
* The config is cached after first fetch to avoid unnecessary requests.
|
|
*/
|
|
export function useConfig(): AppConfig {
|
|
const [config, setConfig] = useState<AppConfig>({
|
|
appName: configCache?.appName || 'Webmail',
|
|
jmapServerUrl: configCache?.jmapServerUrl || '',
|
|
oauthEnabled: configCache?.oauthEnabled || false,
|
|
oauthOnly: configCache?.oauthOnly || false,
|
|
oauthClientId: configCache?.oauthClientId || '',
|
|
oauthIssuerUrl: configCache?.oauthIssuerUrl || '',
|
|
rememberMeEnabled: configCache?.rememberMeEnabled || false,
|
|
settingsSyncEnabled: configCache?.settingsSyncEnabled || false,
|
|
stalwartFeaturesEnabled: configCache?.stalwartFeaturesEnabled ?? true,
|
|
devMode: configCache?.devMode || false,
|
|
faviconUrl: configCache?.faviconUrl || '/branding/Bulwark_Favicon.svg',
|
|
appLogoLightUrl: configCache?.appLogoLightUrl || '',
|
|
appLogoDarkUrl: configCache?.appLogoDarkUrl || '',
|
|
loginLogoLightUrl: configCache?.loginLogoLightUrl || '/branding/Bulwark_Logo_Color.svg',
|
|
loginLogoDarkUrl: configCache?.loginLogoDarkUrl || '/branding/Bulwark_Logo_White.svg',
|
|
loginCompanyName: configCache?.loginCompanyName || '',
|
|
loginImprintUrl: configCache?.loginImprintUrl || '',
|
|
loginPrivacyPolicyUrl: configCache?.loginPrivacyPolicyUrl || '',
|
|
loginWebsiteUrl: configCache?.loginWebsiteUrl || '',
|
|
demoMode: configCache?.demoMode || false,
|
|
autoSsoEnabled: configCache?.autoSsoEnabled || false,
|
|
allowCustomJmapEndpoint: configCache?.allowCustomJmapEndpoint || false,
|
|
embeddedMode: configCache?.embeddedMode || false,
|
|
parentOrigin: configCache?.parentOrigin || '',
|
|
isLoading: !configCache,
|
|
error: null,
|
|
});
|
|
|
|
useEffect(() => {
|
|
// If already cached, no need to fetch
|
|
if (configCache) {
|
|
setConfig({
|
|
appName: configCache.appName,
|
|
jmapServerUrl: configCache.jmapServerUrl,
|
|
oauthEnabled: configCache.oauthEnabled,
|
|
oauthOnly: configCache.oauthOnly,
|
|
oauthClientId: configCache.oauthClientId,
|
|
oauthIssuerUrl: configCache.oauthIssuerUrl,
|
|
rememberMeEnabled: configCache.rememberMeEnabled,
|
|
settingsSyncEnabled: configCache.settingsSyncEnabled,
|
|
stalwartFeaturesEnabled: configCache.stalwartFeaturesEnabled,
|
|
devMode: configCache.devMode,
|
|
faviconUrl: configCache.faviconUrl,
|
|
appLogoLightUrl: configCache.appLogoLightUrl,
|
|
appLogoDarkUrl: configCache.appLogoDarkUrl,
|
|
loginLogoLightUrl: configCache.loginLogoLightUrl,
|
|
loginLogoDarkUrl: configCache.loginLogoDarkUrl,
|
|
loginCompanyName: configCache.loginCompanyName,
|
|
loginImprintUrl: configCache.loginImprintUrl,
|
|
loginPrivacyPolicyUrl: configCache.loginPrivacyPolicyUrl,
|
|
loginWebsiteUrl: configCache.loginWebsiteUrl,
|
|
demoMode: configCache.demoMode,
|
|
autoSsoEnabled: configCache.autoSsoEnabled,
|
|
allowCustomJmapEndpoint: configCache.allowCustomJmapEndpoint,
|
|
embeddedMode: configCache.embeddedMode,
|
|
parentOrigin: configCache.parentOrigin,
|
|
isLoading: false,
|
|
error: null,
|
|
});
|
|
return;
|
|
}
|
|
|
|
fetchConfig()
|
|
.then((data) => {
|
|
setConfig({
|
|
appName: data.appName,
|
|
jmapServerUrl: data.jmapServerUrl,
|
|
oauthEnabled: data.oauthEnabled,
|
|
oauthOnly: data.oauthOnly,
|
|
oauthClientId: data.oauthClientId,
|
|
oauthIssuerUrl: data.oauthIssuerUrl,
|
|
rememberMeEnabled: data.rememberMeEnabled,
|
|
settingsSyncEnabled: data.settingsSyncEnabled,
|
|
stalwartFeaturesEnabled: data.stalwartFeaturesEnabled,
|
|
devMode: data.devMode,
|
|
faviconUrl: data.faviconUrl,
|
|
appLogoLightUrl: data.appLogoLightUrl,
|
|
appLogoDarkUrl: data.appLogoDarkUrl,
|
|
loginLogoLightUrl: data.loginLogoLightUrl,
|
|
loginLogoDarkUrl: data.loginLogoDarkUrl,
|
|
loginCompanyName: data.loginCompanyName,
|
|
loginImprintUrl: data.loginImprintUrl,
|
|
loginPrivacyPolicyUrl: data.loginPrivacyPolicyUrl,
|
|
loginWebsiteUrl: data.loginWebsiteUrl,
|
|
demoMode: data.demoMode,
|
|
autoSsoEnabled: data.autoSsoEnabled,
|
|
allowCustomJmapEndpoint: data.allowCustomJmapEndpoint,
|
|
embeddedMode: data.embeddedMode,
|
|
parentOrigin: data.parentOrigin,
|
|
isLoading: false,
|
|
error: null,
|
|
});
|
|
})
|
|
.catch((err) => {
|
|
setConfig((prev) => ({
|
|
...prev,
|
|
isLoading: false,
|
|
error: err.message,
|
|
}));
|
|
});
|
|
}, []);
|
|
|
|
return config;
|
|
}
|