Cdn

From tinh2/skills-hub-registry · View interactive version

Cdn is an AI agent skill from the GitHub repository tinh2/skills-hub-registry. Configure a CDN with optimized caching, SSL/TLS, security headers, and cache invalidation — auto-detects hosting provider and app type, generates CloudFront, Cloudflare, or Vercel config with per-content-type cache rules, edge functions, and compression. Use when deploying a static site, SPA, SSR ap

How to use this skill

Skill file

You are in AUTONOMOUS MODE. Do NOT ask questions. Do NOT pause for confirmation.

Execute every phase below in sequence, making decisions based on what you find.

============================================================

PHASE 0 -- INPUT

============================================================

$ARGUMENTS may contain:

============================================================

PHASE 1 -- INFRASTRUCTURE DETECTION

============================================================

Scan the project to determine hosting and CDN needs:

Current hosting:

Application type:

Asset analysis:

Existing CDN:

============================================================

PHASE 2 -- CACHING STRATEGY

============================================================

Define cache rules based on content type:

Immutable assets (hashed filenames -- main.a1b2c3.js):

Cache-Control: public, max-age=31536000, immutable

Static assets (non-hashed -- /favicon.ico, /robots.txt):

Cache-Control: public, max-age=86400, stale-while-revalidate=604800

HTML pages:

Cache-Control: public, max-age=300, stale-while-revalidate=3600

API responses:

Cache-Control: private, no-cache, no-store, must-revalidate

Media:

Cache-Control: public, max-age=604800

============================================================

PHASE 3 -- CDN CONFIGURATION

============================================================

Generate configuration for the detected/specified provider:

CloudFront (Terraform or console config):

resource "aws_cloudfront_distribution" "main" {
  enabled             = true
  is_ipv6_enabled     = true
  default_root_object = "index.html"
  aliases             = [var.domain]
  price_class         = "PriceClass_100"  # US, Canada, Europe
  http_version        = "http2and3"

  origin {
    domain_name = aws_s3_bucket.static.bucket_regional_domain_name
    origin_id   = "s3-static"
    origin_access_control_id = aws_cloudfront_origin_access_control.main.id
  }

  default_cache_behavior {
    allowed_methods  = ["GET", "HEAD", "OPTIONS"]
    cached_methods   = ["GET", "HEAD"]
    target_origin_id = "s3-static"

    cache_policy_id          = aws_cloudfront_cache_policy.optimized.id
    origin_request_policy_id = "88a5eaf4-2fd4-4709-b370-b4c650ea3fcf"  # CORS-S3Origin

    viewer_protocol_policy = "redirect-to-https"
    compress               = true
  }

  # API origin (if applicable)
  origin {
    domain_name = var.api_domain
    origin_id   = "api"
    custom_origin_config {
      http_port              = 80
      https_port             = 443
      origin_protocol_policy = "https-only"
      origin_ssl_protocols   = ["TLSv1.2"]
    }
  }

  ordered_cache_behavior {
    path_pattern     = "/api/*"
    target_origin_id = "api"
    allowed_methods  = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
    cached_methods   = ["GET", "HEAD"]
    cache_policy_id  = "4135ea2d-6df8-44a3-9df3-4b5a84be39ad"  # CachingDisabled
    viewer_protocol_policy = "https-only"
    compress               = true
  }

  # SPA custom error response
  custom_error_response {
    error_code         = 403
    response_code      = 200
    response_page_path = "/index.html"
  }

  viewer_certificate {
    acm_certificate_arn      = var.certificate_arn
    ssl_support_method       = "sni-only"
    minimum_protocol_version = "TLSv1.2_2021"
  }

  restrictions {
    geo_restriction {
      restriction_type = "none"
    }
  }
}

Cloudflare (wrangler.toml + page rules or _headers):

name = "{project-name}"
compatibility_date = "2024-01-01"

[site]
bucket = "./dist"

[[headers]]
for = "/_next/static/*"
[headers.values]
Cache-Control = "public, max-age=31536000, immutable"

[[headers]]
for = "/api/*"
[headers.values]
Cache-Control = "no-store"

Generate Cloudflare page rules or cache rules:

Vercel (vercel.json):

{
  "headers": [
    {
      "source": "/(.*).js",
      "headers": [
        { "key": "Cache-Control", "value": "public, max-age=31536000, immutable" }
      ]
    },
    {
      "source": "/api/(.*)",
      "headers": [
        { "key": "Cache-Control", "value": "no-store" }
      ]
    }
  ]
}

Application-level headers (if no CDN config file available):

Generate middleware to set Cache-Control headers in the application code.

============================================================

PHASE 4 -- SSL/TLS CONFIGURATION

============================================================

Ensure HTTPS is properly configured:

CloudFront + ACM:

Cloudflare:

Let's Encrypt (self-hosted):

Generate security headers in CDN config:

Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 0
Referrer-Policy: strict-origin-when-cross-origin
Permissions-Policy: camera=(), microphone=(), geolocation=()
Content-Security-Policy: default-src 'self'; script-src 'self' 'unsafe-inline'

============================================================

PHASE 5 -- EDGE FUNCTIONS (if --edge)

============================================================

Generate edge function/worker for common patterns:

A/B testing:

Redirects:

Geolocation:

Bot protection:

Image optimization:

============================================================

PHASE 6 -- CACHE INVALIDATION

============================================================

Set up cache invalidation for deployments:

CloudFront:

aws cloudfront create-invalidation \
  --distribution-id $DISTRIBUTION_ID \
  --paths "/*"

Cloudflare:

curl -X POST "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/purge_cache" \
  -H "Authorization: Bearer $CF_API_TOKEN" \
  -d '{"purge_everything": true}'

Vercel: automatic on deploy (no manual invalidation needed)

Generate CI/CD step for invalidation after deploy.

============================================================

PHASE 7 -- PERFORMANCE OPTIMIZATION

============================================================

Configure additional optimizations:

============================================================

SELF-HEALING VALIDATION (max 2 iterations)

============================================================

After completing deployment/infrastructure changes, validate:

  1. Verify all generated files are syntactically valid (YAML, JSON, HCL, Dockerfile).
  2. Run validation commands if available (terraform validate, docker build --check, kubectl dry-run).
  3. Verify no secrets, credentials, or sensitive values are hardcoded.
  4. If validation fails, diagnose and fix the specific syntax or config error.
  5. Repeat up to 2 iterations.

IF STILL FAILING after 2 iterations:

============================================================

OUTPUT

============================================================

## CDN Configuration Complete

### Provider: {provider}
### Domain: {domain}

### Files Created
{list of files}

### Caching Rules
| Content Type | Cache Duration | Revalidation |
|-------------|---------------|--------------|
| Hashed assets (JS/CSS) | 1 year | Immutable |
| Static assets | 1 day | 7 day stale-while-revalidate |
| HTML pages | 5 minutes | 1 hour stale-while-revalidate |
| API responses | No cache | -- |
| Media files | 7 days | -- |

### Security Headers
{list of security headers configured}

### Cache Invalidation
{command or CI step to invalidate cache}

### Performance
- Compression: Brotli + Gzip
- Protocol: HTTP/2 + HTTP/3
- TLS: 1.2 minimum

============================================================

NEXT STEPS

============================================================

  1. Point DNS to CDN (CNAME or alias record)
  2. Verify SSL certificate is valid and serving correctly
  3. Test cache behavior: curl -I https://yourdomain.com/asset.js -- check Cache-Control header
  4. Monitor cache hit rate in CDN dashboard (target: >90% for static assets)
  5. Set up Real User Monitoring (RUM) to track performance impact
  6. Run Lighthouse/WebPageTest before and after to measure improvement

============================================================

SELF-EVOLUTION TELEMETRY

============================================================

After producing output, record execution metadata for the /evolve pipeline.

Check if a project memory directory exists:

Entry format:

### /cdn — {{YYYY-MM-DD}}
- Outcome: {{SUCCESS | PARTIAL | FAILED}}
- Self-healed: {{yes — what was healed | no}}
- Iterations used: {{N}} / {{N max}}
- Bottleneck: {{phase that struggled or "none"}}
- Suggestion: {{one-line improvement idea for /evolve, or "none"}}

Only log if the memory directory exists. Skip silently if not found.

Keep entries concise — /evolve will parse these for skill improvement signals.

============================================================

DO NOT

============================================================

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.