Overview
The Inmobiliaria Web frontend is a static single-page application (SPA) built with Vite and React. It’s deployed using Caddy as a web server and can be hosted on platforms like Railway that support nixpacks.Build Process
Development Build
Run the development server locally:http://localhost:5173.
Production Build
Run TypeScript Check
Verify type safety before building:This runs
tsc -b && vite build which:- Compiles TypeScript to check for type errors
- Builds the production bundle
Build Configuration
The build is configured inpackage.json:
package.json
The
build script runs TypeScript compiler (tsc -b) before Vite build to ensure type safety. If TypeScript errors are found, the build will fail.Environment Variables
Required Variables
Set these environment variables for production:.env.production
API URL Configuration
The API URL is configured insrc/lib/api.ts:
src/lib/api.ts
Caddy Web Server
Caddyfile Configuration
The application uses Caddy for serving static files and handling routing:Caddyfile
Key Directives
| Directive | Purpose |
|---|---|
admin off | Disables Caddy admin API |
auto_https off | Disables automatic HTTPS (handled by proxy) |
trusted_proxies | Configures proxy trust for Railway/cloud platforms |
redir | Redirects www to non-www domain |
root * /app/dist | Serves files from the dist directory |
encode gzip | Enables gzip compression |
try_files {path} /index.html | SPA routing fallback |
The
try_files {path} /index.html directive is crucial for SPA routing. It ensures all routes are handled by React Router instead of returning 404 errors.Health Check Endpoint
/health requests to serve the index page, useful for platform health checks.
Nixpacks Configuration
nixpacks.toml
For platforms like Railway that use nixpacks:nixpacks.toml
Configuration Breakdown
- phases.setup: Installs Caddy from nixpkgs
- phases.start: Runs Caddy with the Caddyfile configuration
Nixpacks automatically detects the Node.js environment and runs
npm install && npm run build before the start phase.Deployment Platforms
Railway Deployment
Connect Repository
- Go to Railway
- Create new project
- Connect your GitHub repository
Deploy
Railway automatically:
- Detects nixpacks.toml
- Installs dependencies
- Runs build command
- Starts Caddy server
Configure Domain
Add custom domain in Railway settings:
- ramirezinmuebles.com → main service
- www.ramirezinmuebles.com → redirects to non-www
Manual Deployment
For traditional hosting:Docker Deployment
Create aDockerfile:
Dockerfile
Nginx Configuration
Alternative to Caddy using Nginx:nginx.conf
Performance Optimization
Vite Build Optimization
Configure invite.config.ts:
vite.config.ts
Compression
Caddy automatically handles gzip compression. For additional optimization:Cache Headers
Add cache headers in Caddyfile:Monitoring and Logging
Structured Logging
Caddy outputs JSON logs for easy parsing:Health Checks
Implement health check monitoring:Error Tracking
Integrate error tracking services:Troubleshooting
Build Failures
TypeScript Errors:Runtime Issues
API Connection Errors:- Verify
VITE_API_URLis set correctly - Check CORS configuration on backend
- Ensure backend is accessible from frontend
- Verify
try_filesdirective in Caddyfile - Ensure SPA fallback is configured
- Ensure variables have
VITE_prefix - Rebuild after changing environment variables
- Check variables are set in deployment platform
Security Checklist
Environment Variables
- Never commit
.envfiles - Use platform secrets for sensitive data
- Rotate API keys regularly
CORS Configuration
- Configure backend CORS to allow only your domain
- Don’t use
*wildcards in production
Best Practices
- Always run builds with environment variables set
- Test production builds locally with
npm run preview - Use CI/CD pipelines for automated deployments
- Monitor build sizes and performance metrics
- Implement proper error tracking and logging
- Set up automated backups
- Use staging environments for testing
- Document deployment procedures
CI/CD Pipeline Example
.github/workflows/deploy.yml