Capacitor production builds retaining development server.url configuration
0 reputation · 18 Apr 2021, 19:19 UTC
0 reputation · 18 Apr 2021, 19:19 UTC
Capacitor's server.url setting in capacitor.config.json directs the WebView to a local development server for live reload. The documentation marks this as a development-only feature, yet the CLI does not automatically remove or override the value when running capacitor build ios or capacitor build android. If the key remains, the packaged app attempts to load http://localhost:8100 on the device, resulting in a white screen or connection error with no actionable log output.
--prod) strips server.url or server.cleartext during the sync or copy phases.capacitor.config.prod.json) or migrate to capacitor.config.ts with process.env.NODE_ENV checks, which adds a TypeScript compilation step.Establish a reliable, low-maintenance strategy that guarantees server.url and server.cleartext are absent from production native projects without relying on manual steps or custom scripts that diverge across repositories.
server.cleartext flag be validated as disabled in production builds as part of an automated check?34775 reputation · 19 Apr 2021, 06:56 UTC
To guarantee that server.url and server.cleartext are absent from production builds, migrate from capacitor.config.json to capacitor.config.ts. This allows you to use conditional logic based on environment variables to omit the server configuration entirely during production builds.
Convert your static JSON file to a TypeScript configuration. This approach eliminates the need for duplicate config files or fragile shell scripts in CI/CD pipelines.
import { CapacitorConfig } from '@capacitor/cli';
const isProduction = process.env.NODE_ENV === 'production';
const config: CapacitorConfig = {
appId: 'com.example.app',
appName: 'my-app',
webDir: 'dist',
server: isProduction ? undefined : {
url: 'http://localhost:8100',
cleartext: true
}
};
export default config;
The Capacitor CLI treats the configuration file as a source of truth for the current state of the project. It does not perform "tree-shaking" or property stripping on capacitor.config.json during the copy or sync phases because it assumes the developer has provided the configuration intended for the target build. Since JSON is static, it cannot react to build flags like --prod.
To ensure the production build is correctly configured and server.cleartext is disabled, use the following verification steps:
capacitor.config.json generated in the android/ or ios/ folders) to confirm the server key is missing.localhost, the configuration is correct.server.url will typically throw a net::ERR_CONNECTION_REFUSED or a WebView load error immediately upon startup.This solution assumes you are using Capacitor 3.x through 6.x, as capacitor.config.ts support was introduced to solve this exact limitation. It also assumes your CI/CD pipeline sets NODE_ENV=production during the build phase.
Diagnostic Detail Needed: Are you using a custom build wrapper or a specific CI provider (e.g., GitHub Actions, Bitrise) that overrides process.env during the npx cap sync step?
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.