Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions apps/cli/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ const resolveWebRuntime = (options: {
const resolvedApiPort = toPortOrFallback(apiEnv.PORT, 8787);
const resolvedApiHost = resolveApiHost(apiEnv.HOST);

const pwaEnv: NodeJS.ProcessEnv = { ...process.env };
let pwaEnv: NodeJS.ProcessEnv = { ...process.env };
const pwaPortFallback = mode === 'dev' ? 5173 : 4173;
if (pwaPort !== undefined) {
if (mode === 'dev') {
Expand All @@ -505,13 +505,22 @@ const resolveWebRuntime = (options: {
}
}

const configuredApiBase = pwaEnv.VITE_API_BASE;
const fallbackApiBase = `http://${resolvedApiHost}:${resolvedApiPort}/v1`;

const resolvedApiBase =
apiPort !== undefined
? (rewriteApiBasePort(pwaEnv.VITE_API_BASE, resolvedApiPort) ?? fallbackApiBase)
: (pwaEnv.VITE_API_BASE ?? fallbackApiBase);
? (rewriteApiBasePort(configuredApiBase, resolvedApiPort) ?? fallbackApiBase)
: (configuredApiBase ?? fallbackApiBase);

const injectedApiBase = apiPort !== undefined ? resolvedApiBase : configuredApiBase;

pwaEnv.VITE_API_BASE = resolvedApiBase;
if (injectedApiBase) {
pwaEnv.VITE_API_BASE = injectedApiBase;
} else {
const { VITE_API_BASE: _unusedApiBase, ...remainingPwaEnv } = pwaEnv;
pwaEnv = remainingPwaEnv;
}

const resolvedPwaPort =
mode === 'dev'
Expand Down
18 changes: 17 additions & 1 deletion apps/pwa/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,23 @@ import type {
TrendPoint,
} from './types.js';

const baseUrl = import.meta.env.VITE_API_BASE ?? 'http://localhost:8787/v1';
const normalizeBaseUrl = (value: string): string => value.replace(/\/+$/, '');

const resolveApiBaseUrl = (): string => {
const envBaseUrl = import.meta.env.VITE_API_BASE?.trim();
if (envBaseUrl) {
return normalizeBaseUrl(envBaseUrl);
}

if (typeof window !== 'undefined' && window.location.hostname) {
const protocol = window.location.protocol === 'https:' ? 'https:' : 'http:';
return `${protocol}//${window.location.hostname}:8787/v1`;
}

return 'http://localhost:8787/v1';
};

const baseUrl = resolveApiBaseUrl();
const requestTimeoutMs = 10_000;

const request = async <T>(path: string, init?: RequestInit): Promise<T> => {
Expand Down
1 change: 1 addition & 0 deletions apps/pwa/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export default defineConfig(({ mode }) => {
preview: {
host: '0.0.0.0',
port: pwaPreviewPort,
allowedHosts: ['localhost', '127.0.0.1', '.ts.net'],
},
};
});