# Deliver public assets through CDN for Next.js

Deliver public assets for a Next.js app through the Ample CDN. Publish a bucket with `ample bucket publish`, upload versioned assets with long-lived Cache-Control headers, and reference the CDN URL (https://cdn.//) from your pages. The CDN serves bucket objects; the app's own responses are still served by the app.

## Resources Required
- **Compute**: Apps run in isolated x86_64 Firecracker microVMs that auto-pause when idle and wake on request; sizes are the priced VM sizes.
- **CDN**: The CDN host serves objects from published buckets. It does not front app compute and is not a cache or key-value store.

## Prerequisites
- A bucket created with `ample bucket create` and published with `ample bucket publish` (its public URL is the CDN base)
- An S3 client in the app to upload assets under versioned keys
- An Ample account token with servers:write and buckets:write

## Workflow Steps
1. **Publish the bucket**: Publishing makes objects readable at the CDN URL; keep private data in a separate, unpublished bucket.
   - Command: `ample bucket publish`
2. **Upload versioned assets**: Use keys such as assets//file and set Cache-Control: public, max-age=31536000, immutable; change the version to invalidate.
3. **Reference the CDN URL**: Pass S3_PUBLIC_URL to the app and build asset URLs from it.
   - Command: `ample deploy . --name [APP_NAME] --public --env S3_PUBLIC_URL=... --env S3_ENDPOINT=... --env S3_REGION=... --env S3_BUCKET=... --env S3_ACCESS_KEY_ID=... --env S3_SECRET_ACCESS_KEY=...`
4. **Verify delivery**: Fetch /api/asset-url for the asset URL, then fetch that URL: the CDN must return the asset.
5. **Verify**: Fetch the live URL and run the success checks below. On failure read the build log, then the runtime log, fix the cause and deploy again; do not blind-retry.
   - Command: `ample logs [YOUR_LOG_OPTIONS]`

## Limitations
- Verified on the node-22 template at s-1vcpu-1gb; other templates and sizes are not verified by this recipe.
- The CDN serves objects from a published bucket only; it does not front the app's own responses.

## Cost Estimate
- **Currency**: USD
- **Monthly Amount**: 5.0
- **Components**:
  - App server (size: s-1vcpu-1gb, quantity: 1.0, monthly amount: 5.0)  
  Note: Compute only. Buckets are allocation-priced per quota and capped by the plan; see `ample usage`.

## Examples
- **Next.js CDN assets canary**: Versioned asset uploaded to a published bucket and served from the CDN URL.

## Success Checks
- Asset URL resolves through the CDN.

### Next Actions
- **Browse the catalog index**: Browse published recipes by intent, stack, and constraints.
- **Prepare a side-effect-free deployment plan** for an authorized project.
