Deployment

On this page 21

stx provides multiple deployment options and strategies to help you ship your applications to production efficiently and reliably.

Deployment Targets

Static Site Generation

Deploy stx applications as static sites:

# Build for static deployment
bun run build:static

# Deploy to various platforms
bun run deploy:netlify
bun run deploy:vercel
bun run deploy:github-pages

Server-Side Rendering

Deploy with full SSR capabilities:

# Build for SSR
bun run build:ssr

# Start production server
bun run start:prod

Edge Computing

Deploy to edge environments:

// edge-config.ts
export default {
  runtime: 'edge',
  regions: ['us-east-1', 'eu-west-1'],
  caching: {
    ttl: 3600,
    staleWhileRevalidate: true
  }
}

Platform Integrations

Vercel

{
  "functions": {
    "app/api/*.ts": {
      "runtime": "bun"
    }
  },
  "rewrites": [
    { "source": "/(.*)", "destination": "/api/app" }
  ]
}

Netlify

[build]
  command = "bun run build"
  publish = "dist"

[[redirects]]
  from = "/*"
  to = "/index.html"
  status = 200

[context.production.environment]
  BUN_VERSION = "1.0.0"

Docker

FROM oven/bun:1 as base
WORKDIR /app

COPY package.json bun.lockb ./
RUN bun install --frozen-lockfile

COPY . .
RUN bun run build

EXPOSE 3000
CMD ["bun", "run", "start"]

Build Optimization

Code Splitting

// Dynamic imports for code splitting
const LazyComponent = lazy(() => import('./LazyComponent.stx'))

// Route-based splitting
const routes = [
  {
    path: '/dashboard',
    component: () => import('./pages/Dashboard.stx')
  }
]

Bundle Analysis

# Analyze bundle size
bun run build:analyze

# Generate bundle report
bun run bundle:report

Environment Configuration

Environment Variables

// .env.production
NODE_ENV=production
API_URL=https://api.example.com
CDN_URL=https://cdn.example.com
ENABLE_ANALYTICS=true

Config Management

// config/production.ts
export default {
  server: {
    port: process.env.PORT || 3000,
    host: '0.0.0.0'
  },
  cache: {
    ttl: 86400,
    redis: process.env.REDIS_URL
  },
  logging: {
    level: 'info',
    format: 'json'
  }
}

Monitoring and Health Checks

Health Endpoints

// Health check endpoint
app.get('/health', (req, res) => {
  res.json({
    status: 'healthy',
    timestamp: new Date().toISOString(),
    uptime: process.uptime(),
    memory: process.memoryUsage()
  })
})

Performance Monitoring

// Performance metrics
import { metrics } from '@stacksjs/monitoring'

metrics.counter('requests_total').inc()
metrics.histogram('request_duration').observe(duration)

Security

HTTPS Configuration

// HTTPS setup
const options = {
  cert: fs.readFileSync('cert.pem'),
  key: fs.readFileSync('key.pem')
}

https.createServer(options, app).listen(443)

Security Headers

// Security middleware
app.use(helmet({
  contentSecurityPolicy: {
    directives: {
      defaultSrc: ["'self'"],
      styleSrc: ["'self'", "'unsafe-inline'"],
      scriptSrc: ["'self'"]
    }
  }
}))