@intentsolutionsio/vercel-pack 1.0.0 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +67 -44
- package/package.json +4 -4
- package/skills/vercel-advanced-troubleshooting/SKILL.md +185 -195
- package/skills/vercel-advanced-troubleshooting/references/errors.md +11 -0
- package/skills/vercel-advanced-troubleshooting/references/evidence-collection-framework.md +34 -0
- package/skills/vercel-advanced-troubleshooting/references/examples.md +11 -0
- package/skills/vercel-advanced-troubleshooting/references/systematic-isolation.md +56 -0
- package/skills/vercel-advanced-troubleshooting/references/timing-analysis.md +35 -0
- package/skills/vercel-architecture-variants/SKILL.md +227 -216
- package/skills/vercel-architecture-variants/references/errors.md +11 -0
- package/skills/vercel-architecture-variants/references/examples.md +12 -0
- package/skills/vercel-architecture-variants/references/variant-a-monolith-(simple).md +44 -0
- package/skills/vercel-architecture-variants/references/variant-b-service-layer-(moderate).md +72 -0
- package/skills/vercel-architecture-variants/references/variant-c-microservice-(complex).md +81 -0
- package/skills/vercel-ci-integration/SKILL.md +183 -73
- package/skills/vercel-ci-integration/references/errors.md +10 -0
- package/skills/vercel-ci-integration/references/examples.md +36 -0
- package/skills/vercel-ci-integration/references/implementation.md +54 -0
- package/skills/vercel-common-errors/SKILL.md +164 -60
- package/skills/vercel-common-errors/references/errors.md +53 -0
- package/skills/vercel-common-errors/references/examples.md +23 -0
- package/skills/vercel-cost-tuning/SKILL.md +158 -145
- package/skills/vercel-cost-tuning/references/cost-estimation.md +34 -0
- package/skills/vercel-cost-tuning/references/cost-reduction-strategies.md +40 -0
- package/skills/vercel-cost-tuning/references/errors.md +11 -0
- package/skills/vercel-cost-tuning/references/examples.md +15 -0
- package/skills/vercel-data-handling/SKILL.md +202 -155
- package/skills/vercel-data-handling/references/errors.md +11 -0
- package/skills/vercel-data-handling/references/examples.md +27 -0
- package/skills/vercel-data-handling/references/implementation.md +223 -0
- package/skills/vercel-debug-bundle/SKILL.md +163 -67
- package/skills/vercel-debug-bundle/references/errors.md +12 -0
- package/skills/vercel-debug-bundle/references/examples.md +24 -0
- package/skills/vercel-debug-bundle/references/implementation.md +54 -0
- package/skills/vercel-deploy-integration/SKILL.md +163 -156
- package/skills/vercel-deploy-integration/references/errors.md +11 -0
- package/skills/vercel-deploy-integration/references/examples.md +21 -0
- package/skills/vercel-deploy-integration/references/google-cloud-run.md +36 -0
- package/skills/vercel-deploy-integration/references/vercel-deployment.md +35 -0
- package/skills/vercel-deploy-preview/SKILL.md +164 -39
- package/skills/vercel-edge-functions/SKILL.md +185 -37
- package/skills/vercel-enterprise-rbac/SKILL.md +185 -170
- package/skills/vercel-enterprise-rbac/references/errors.md +11 -0
- package/skills/vercel-enterprise-rbac/references/examples.md +12 -0
- package/skills/vercel-enterprise-rbac/references/role-implementation.md +33 -0
- package/skills/vercel-enterprise-rbac/references/sso-integration.md +35 -0
- package/skills/vercel-hello-world/SKILL.md +141 -55
- package/skills/vercel-incident-runbook/SKILL.md +186 -138
- package/skills/vercel-incident-runbook/references/errors.md +11 -0
- package/skills/vercel-incident-runbook/references/examples.md +10 -0
- package/skills/vercel-incident-runbook/references/immediate-actions-by-error-type.md +41 -0
- package/skills/vercel-install-auth/SKILL.md +130 -53
- package/skills/vercel-known-pitfalls/SKILL.md +235 -233
- package/skills/vercel-known-pitfalls/references/errors.md +11 -0
- package/skills/vercel-known-pitfalls/references/examples.md +12 -0
- package/skills/vercel-load-scale/SKILL.md +197 -204
- package/skills/vercel-load-scale/references/capacity-planning.md +47 -0
- package/skills/vercel-load-scale/references/errors.md +11 -0
- package/skills/vercel-load-scale/references/examples.md +26 -0
- package/skills/vercel-load-scale/references/load-testing-with-k6.md +59 -0
- package/skills/vercel-load-scale/references/scaling-patterns.md +65 -0
- package/skills/vercel-local-dev-loop/SKILL.md +159 -71
- package/skills/vercel-local-dev-loop/references/errors.md +11 -0
- package/skills/vercel-local-dev-loop/references/examples.md +21 -0
- package/skills/vercel-local-dev-loop/references/implementation.md +60 -0
- package/skills/vercel-migration-deep-dive/SKILL.md +202 -187
- package/skills/vercel-migration-deep-dive/references/errors.md +11 -0
- package/skills/vercel-migration-deep-dive/references/examples.md +12 -0
- package/skills/vercel-migration-deep-dive/references/implementation-plan.md +80 -0
- package/skills/vercel-migration-deep-dive/references/pre-migration-assessment.md +39 -0
- package/skills/vercel-multi-env-setup/SKILL.md +167 -164
- package/skills/vercel-multi-env-setup/references/configuration-structure.md +59 -0
- package/skills/vercel-multi-env-setup/references/errors.md +11 -0
- package/skills/vercel-multi-env-setup/references/examples.md +11 -0
- package/skills/vercel-observability/SKILL.md +205 -195
- package/skills/vercel-observability/references/alert-configuration.md +40 -0
- package/skills/vercel-observability/references/errors.md +11 -0
- package/skills/vercel-observability/references/examples.md +13 -0
- package/skills/vercel-observability/references/metrics-collection.md +65 -0
- package/skills/vercel-performance-tuning/SKILL.md +212 -156
- package/skills/vercel-performance-tuning/references/caching-strategy.md +49 -0
- package/skills/vercel-performance-tuning/references/errors.md +11 -0
- package/skills/vercel-performance-tuning/references/examples.md +13 -0
- package/skills/vercel-policy-guardrails/SKILL.md +276 -193
- package/skills/vercel-policy-guardrails/references/errors.md +11 -0
- package/skills/vercel-policy-guardrails/references/eslint-rules.md +46 -0
- package/skills/vercel-policy-guardrails/references/examples.md +10 -0
- package/skills/vercel-prod-checklist/SKILL.md +219 -94
- package/skills/vercel-prod-checklist/references/errors.md +11 -0
- package/skills/vercel-prod-checklist/references/examples.md +25 -0
- package/skills/vercel-prod-checklist/references/implementation.md +60 -0
- package/skills/vercel-rate-limits/SKILL.md +187 -100
- package/skills/vercel-rate-limits/references/errors.md +11 -0
- package/skills/vercel-rate-limits/references/examples.md +46 -0
- package/skills/vercel-rate-limits/references/implementation.md +66 -0
- package/skills/vercel-reference-architecture/SKILL.md +226 -180
- package/skills/vercel-reference-architecture/references/errors.md +11 -0
- package/skills/vercel-reference-architecture/references/examples.md +13 -0
- package/skills/vercel-reference-architecture/references/key-components.md +65 -0
- package/skills/vercel-reference-architecture/references/project-structure.md +40 -0
- package/skills/vercel-reliability-patterns/SKILL.md +272 -211
- package/skills/vercel-reliability-patterns/references/circuit-breaker.md +36 -0
- package/skills/vercel-reliability-patterns/references/dead-letter-queue.md +48 -0
- package/skills/vercel-reliability-patterns/references/errors.md +11 -0
- package/skills/vercel-reliability-patterns/references/examples.md +11 -0
- package/skills/vercel-reliability-patterns/references/idempotency-keys.md +36 -0
- package/skills/vercel-sdk-patterns/SKILL.md +264 -92
- package/skills/vercel-sdk-patterns/references/errors.md +11 -0
- package/skills/vercel-sdk-patterns/references/examples.md +45 -0
- package/skills/vercel-sdk-patterns/references/implementation.md +67 -0
- package/skills/vercel-security-basics/SKILL.md +186 -96
- package/skills/vercel-security-basics/references/errors.md +10 -0
- package/skills/vercel-security-basics/references/examples.md +70 -0
- package/skills/vercel-security-basics/references/implementation.md +39 -0
- package/skills/vercel-upgrade-migration/SKILL.md +167 -67
- package/skills/vercel-upgrade-migration/references/errors.md +10 -0
- package/skills/vercel-upgrade-migration/references/examples.md +51 -0
- package/skills/vercel-upgrade-migration/references/implementation.md +29 -0
- package/skills/vercel-webhooks-events/SKILL.md +208 -132
- package/skills/vercel-webhooks-events/references/errors.md +11 -0
- package/skills/vercel-webhooks-events/references/event-handler-pattern.md +37 -0
- package/skills/vercel-webhooks-events/references/examples.md +16 -0
- package/skills/vercel-webhooks-events/references/signature-verification.md +33 -0
|
@@ -1,71 +1,196 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: vercel-deploy-preview
|
|
3
|
-
description:
|
|
4
|
-
|
|
5
|
-
Use when
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
3
|
+
description: 'Create and manage Vercel preview deployments for branches and pull requests.
|
|
4
|
+
|
|
5
|
+
Use when deploying a preview for a pull request, testing changes before production,
|
|
6
|
+
|
|
7
|
+
or sharing preview URLs with stakeholders.
|
|
8
|
+
|
|
9
|
+
Trigger with phrases like "vercel deploy preview", "vercel preview URL",
|
|
10
|
+
|
|
11
|
+
"create preview deployment", "vercel PR preview".
|
|
12
|
+
|
|
13
|
+
'
|
|
14
|
+
allowed-tools: Read, Write, Edit, Bash(vercel:*), Bash(curl:*), Bash(git:*), Grep
|
|
10
15
|
version: 1.0.0
|
|
11
16
|
license: MIT
|
|
12
17
|
author: Jeremy Longshore <jeremy@intentsolutions.io>
|
|
18
|
+
tags:
|
|
19
|
+
- saas
|
|
20
|
+
- vercel
|
|
21
|
+
- deployment
|
|
22
|
+
- preview
|
|
23
|
+
- workflow
|
|
24
|
+
compatibility: Designed for Claude Code, also compatible with Codex and OpenClaw
|
|
13
25
|
---
|
|
14
|
-
|
|
15
26
|
# Vercel Deploy Preview
|
|
16
27
|
|
|
17
28
|
## Overview
|
|
18
|
-
Deploy preview environments for pull requests and branches.
|
|
19
|
-
This is the primary workflow for Vercel - instant previews for every commit.
|
|
20
29
|
|
|
30
|
+
Deploy preview environments for branches and pull requests. Every `git push` to a non-production branch generates a unique preview URL. Covers CLI-based previews, API-based previews, deployment protection, and comment integration.
|
|
21
31
|
|
|
22
32
|
## Prerequisites
|
|
33
|
+
|
|
23
34
|
- Completed `vercel-install-auth` setup
|
|
24
|
-
-
|
|
25
|
-
-
|
|
35
|
+
- Project linked via `vercel link`
|
|
36
|
+
- Git repository connected in Vercel dashboard
|
|
26
37
|
|
|
27
38
|
## Instructions
|
|
28
39
|
|
|
29
|
-
### Step 1:
|
|
30
|
-
|
|
31
|
-
|
|
40
|
+
### Step 1: Deploy Preview via CLI
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
# Deploy current directory to a preview URL (default — not --prod)
|
|
44
|
+
vercel
|
|
45
|
+
|
|
46
|
+
# Output:
|
|
47
|
+
# 🔗 Linked to your-team/my-app
|
|
48
|
+
# 🔍 Inspect: https://vercel.com/your-team/my-app/AbCdEfG
|
|
49
|
+
# ✅ Preview: https://my-app-git-feature-branch-your-team.vercel.app
|
|
50
|
+
|
|
51
|
+
# Deploy a specific directory
|
|
52
|
+
vercel ./dist
|
|
53
|
+
|
|
54
|
+
# Deploy and wait for build to complete (useful in CI)
|
|
55
|
+
vercel --no-wait # returns immediately with deployment URL
|
|
32
56
|
```
|
|
33
57
|
|
|
34
|
-
### Step 2:
|
|
35
|
-
|
|
36
|
-
|
|
58
|
+
### Step 2: Deploy Preview via REST API
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
# Create a deployment via API — useful for custom CI pipelines
|
|
62
|
+
curl -X POST "https://api.vercel.com/v13/deployments" \
|
|
63
|
+
-H "Authorization: Bearer $VERCEL_TOKEN" \
|
|
64
|
+
-H "Content-Type: application/json" \
|
|
65
|
+
-d '{
|
|
66
|
+
"name": "my-app",
|
|
67
|
+
"target": "preview",
|
|
68
|
+
"gitSource": {
|
|
69
|
+
"type": "github",
|
|
70
|
+
"repoId": "123456789",
|
|
71
|
+
"ref": "feature/new-feature",
|
|
72
|
+
"sha": "abc123def456"
|
|
73
|
+
}
|
|
74
|
+
}'
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Step 3: Check Deployment Status
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
# Poll deployment status until READY
|
|
81
|
+
curl -s -H "Authorization: Bearer $VERCEL_TOKEN" \
|
|
82
|
+
"https://api.vercel.com/v13/deployments/dpl_xxxxxxxxxxxx" \
|
|
83
|
+
| jq '{state: .state, url: .url, readyState: .readyState}'
|
|
84
|
+
|
|
85
|
+
# States: QUEUED → BUILDING → READY (or ERROR/CANCELED)
|
|
37
86
|
```
|
|
38
87
|
|
|
39
|
-
### Step 3: Finalize
|
|
40
88
|
```typescript
|
|
41
|
-
//
|
|
89
|
+
// Programmatic polling
|
|
90
|
+
async function waitForDeployment(client: VercelClient, deploymentId: string) {
|
|
91
|
+
while (true) {
|
|
92
|
+
const d = await client.getDeployment(deploymentId);
|
|
93
|
+
if (d.state === 'READY') return d;
|
|
94
|
+
if (d.state === 'ERROR' || d.state === 'CANCELED') {
|
|
95
|
+
throw new Error(`Deployment ${d.state}: ${deploymentId}`);
|
|
96
|
+
}
|
|
97
|
+
await new Promise(r => setTimeout(r, 5000)); // poll every 5s
|
|
98
|
+
}
|
|
99
|
+
}
|
|
42
100
|
```
|
|
43
101
|
|
|
44
|
-
|
|
45
|
-
- Completed Deploy Preview execution
|
|
46
|
-
- Expected results from Vercel API
|
|
47
|
-
- Success confirmation or error details
|
|
102
|
+
### Step 4: Configure Preview Environment Variables
|
|
48
103
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
104
|
+
```bash
|
|
105
|
+
# Add env vars scoped to preview only
|
|
106
|
+
vercel env add DATABASE_URL preview
|
|
107
|
+
# Enter value when prompted
|
|
108
|
+
|
|
109
|
+
# Or via API — scope to preview environment
|
|
110
|
+
curl -X POST "https://api.vercel.com/v9/projects/my-app/env" \
|
|
111
|
+
-H "Authorization: Bearer $VERCEL_TOKEN" \
|
|
112
|
+
-H "Content-Type: application/json" \
|
|
113
|
+
-d '{
|
|
114
|
+
"key": "DATABASE_URL",
|
|
115
|
+
"value": "postgres://preview-db:5432/myapp",
|
|
116
|
+
"type": "encrypted",
|
|
117
|
+
"target": ["preview"]
|
|
118
|
+
}'
|
|
119
|
+
```
|
|
54
120
|
|
|
55
|
-
|
|
121
|
+
### Step 5: Deployment Protection
|
|
56
122
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
123
|
+
Vercel supports password-protecting preview deployments:
|
|
124
|
+
|
|
125
|
+
```json
|
|
126
|
+
// vercel.json — require authentication for previews
|
|
127
|
+
{
|
|
128
|
+
"deploymentProtection": {
|
|
129
|
+
"preview": "vercel-authentication"
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
Options:
|
|
135
|
+
|
|
136
|
+
- `"vercel-authentication"` — requires Vercel team login
|
|
137
|
+
- `"standard-protection"` — bypass for automation with `x-vercel-protection-bypass` header
|
|
138
|
+
- Disabled — previews are publicly accessible
|
|
139
|
+
|
|
140
|
+
### Step 6: GitHub Integration — PR Comments
|
|
141
|
+
|
|
142
|
+
When a GitHub repo is connected, Vercel automatically:
|
|
143
|
+
|
|
144
|
+
1. Creates a preview deployment on every push
|
|
145
|
+
2. Posts a comment on the PR with the preview URL
|
|
146
|
+
3. Updates the GitHub commit status (pending → success/failure)
|
|
147
|
+
4. Adds "Visit Preview" link in the PR checks section
|
|
148
|
+
|
|
149
|
+
To configure in the Vercel dashboard:
|
|
150
|
+
|
|
151
|
+
- **Settings > Git > Deploy Hooks** for manual triggers
|
|
152
|
+
- **Settings > Git > Ignored Build Step** to skip builds for certain paths
|
|
153
|
+
|
|
154
|
+
```bash
|
|
155
|
+
# Ignored Build Step — skip deploy when only docs changed
|
|
156
|
+
# vercel.json
|
|
157
|
+
{
|
|
158
|
+
"ignoreCommand": "git diff HEAD^ HEAD --quiet -- . ':!docs' ':!*.md'"
|
|
159
|
+
}
|
|
60
160
|
```
|
|
61
161
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
162
|
+
## Preview URL Patterns
|
|
163
|
+
|
|
164
|
+
| Branch | URL Pattern |
|
|
165
|
+
|--------|-------------|
|
|
166
|
+
| `feature/auth` | `my-app-git-feature-auth-team.vercel.app` |
|
|
167
|
+
| `fix/bug-123` | `my-app-git-fix-bug-123-team.vercel.app` |
|
|
168
|
+
| Random deploy | `my-app-abc123def.vercel.app` |
|
|
169
|
+
|
|
170
|
+
## Output
|
|
171
|
+
|
|
172
|
+
- Preview deployment URL unique to each branch/commit
|
|
173
|
+
- Build logs accessible via Vercel dashboard or API
|
|
174
|
+
- PR comment with preview link (GitHub integration)
|
|
175
|
+
- Environment variables scoped to preview only
|
|
176
|
+
|
|
177
|
+
## Error Handling
|
|
178
|
+
|
|
179
|
+
| Error | Cause | Solution |
|
|
180
|
+
|-------|-------|----------|
|
|
181
|
+
| `BUILD_FAILED` | Build command failed | Check build logs: `vercel inspect <url>` |
|
|
182
|
+
| `FUNCTION_INVOCATION_FAILED` | Runtime error in function | Review function logs: `vercel logs <url>` |
|
|
183
|
+
| `NO_BUILDS` | No output detected | Verify `outputDirectory` in vercel.json |
|
|
184
|
+
| Preview not updating | Cached old deployment | Force rebuild: `vercel --force` |
|
|
185
|
+
| `DEPLOYMENT_BLOCKED` | Deployment protection active | Use `x-vercel-protection-bypass` header |
|
|
65
186
|
|
|
66
187
|
## Resources
|
|
67
|
-
|
|
68
|
-
- [
|
|
188
|
+
|
|
189
|
+
- [Preview Deployments](https://vercel.com/docs/deployments/preview-deployments)
|
|
190
|
+
- [Deployment Protection](https://vercel.com/docs/security/deployment-protection)
|
|
191
|
+
- [CLI Deploy Command](https://vercel.com/docs/cli/deploy)
|
|
192
|
+
- [REST API: Create Deployment](https://vercel.com/docs/rest-api/deployments/create-a-new-deployment)
|
|
69
193
|
|
|
70
194
|
## Next Steps
|
|
71
|
-
|
|
195
|
+
|
|
196
|
+
For edge function development, see `vercel-edge-functions`.
|
|
@@ -1,73 +1,221 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: vercel-edge-functions
|
|
3
|
-
description:
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
3
|
+
description: 'Build and deploy Vercel Edge Functions for ultra-low latency at the
|
|
4
|
+
edge.
|
|
5
|
+
|
|
6
|
+
Use when creating API routes with minimal latency, geolocation-based routing,
|
|
7
|
+
|
|
8
|
+
A/B testing, or authentication at the edge.
|
|
9
|
+
|
|
10
|
+
Trigger with phrases like "vercel edge function", "vercel edge runtime",
|
|
11
|
+
|
|
12
|
+
"deploy edge function", "vercel middleware", "@vercel/edge".
|
|
13
|
+
|
|
14
|
+
'
|
|
15
|
+
allowed-tools: Read, Write, Edit, Bash(vercel:*), Bash(npm:*), Grep
|
|
10
16
|
version: 1.0.0
|
|
11
17
|
license: MIT
|
|
12
18
|
author: Jeremy Longshore <jeremy@intentsolutions.io>
|
|
19
|
+
tags:
|
|
20
|
+
- saas
|
|
21
|
+
- vercel
|
|
22
|
+
- edge
|
|
23
|
+
- serverless
|
|
24
|
+
- performance
|
|
25
|
+
compatibility: Designed for Claude Code, also compatible with Codex and OpenClaw
|
|
13
26
|
---
|
|
14
|
-
|
|
15
27
|
# Vercel Edge Functions
|
|
16
28
|
|
|
17
29
|
## Overview
|
|
18
|
-
Build and deploy Edge Functions for ultra-low latency at the edge.
|
|
19
|
-
Serverless functions that run close to users worldwide.
|
|
20
30
|
|
|
31
|
+
Edge Functions run on Vercel's Edge Network (V8 isolates) close to the user with no cold starts. They use Web Standard APIs (Request, Response, fetch) instead of Node.js APIs. Ideal for authentication, A/B testing, geolocation routing, and low-latency API responses.
|
|
21
32
|
|
|
22
33
|
## Prerequisites
|
|
34
|
+
|
|
23
35
|
- Completed `vercel-install-auth` setup
|
|
24
|
-
- Familiarity with
|
|
25
|
-
-
|
|
36
|
+
- Familiarity with Web APIs (Request/Response)
|
|
37
|
+
- Node.js 18+ for local development
|
|
26
38
|
|
|
27
39
|
## Instructions
|
|
28
40
|
|
|
29
|
-
### Step 1:
|
|
41
|
+
### Step 1: Create an Edge Function
|
|
42
|
+
|
|
30
43
|
```typescript
|
|
31
|
-
//
|
|
44
|
+
// api/edge-hello.ts
|
|
45
|
+
// Export `runtime = 'edge'` to run on the Edge Runtime
|
|
46
|
+
export const config = { runtime: 'edge' };
|
|
47
|
+
|
|
48
|
+
export default function handler(request: Request): Response {
|
|
49
|
+
return new Response(
|
|
50
|
+
JSON.stringify({
|
|
51
|
+
message: 'Hello from the Edge!',
|
|
52
|
+
region: request.headers.get('x-vercel-ip-city') ?? 'unknown',
|
|
53
|
+
timestamp: Date.now(),
|
|
54
|
+
}),
|
|
55
|
+
{
|
|
56
|
+
status: 200,
|
|
57
|
+
headers: { 'Content-Type': 'application/json' },
|
|
58
|
+
}
|
|
59
|
+
);
|
|
60
|
+
}
|
|
32
61
|
```
|
|
33
62
|
|
|
34
|
-
### Step 2:
|
|
63
|
+
### Step 2: Edge Function with Geolocation
|
|
64
|
+
|
|
65
|
+
Vercel injects geolocation headers into every edge request:
|
|
66
|
+
|
|
35
67
|
```typescript
|
|
36
|
-
//
|
|
68
|
+
// api/geo.ts
|
|
69
|
+
export const config = { runtime: 'edge' };
|
|
70
|
+
|
|
71
|
+
export default function handler(request: Request): Response {
|
|
72
|
+
const city = request.headers.get('x-vercel-ip-city') ?? 'unknown';
|
|
73
|
+
const country = request.headers.get('x-vercel-ip-country') ?? 'unknown';
|
|
74
|
+
const region = request.headers.get('x-vercel-ip-country-region') ?? 'unknown';
|
|
75
|
+
const latitude = request.headers.get('x-vercel-ip-latitude');
|
|
76
|
+
const longitude = request.headers.get('x-vercel-ip-longitude');
|
|
77
|
+
|
|
78
|
+
return Response.json({
|
|
79
|
+
city: decodeURIComponent(city),
|
|
80
|
+
country,
|
|
81
|
+
region,
|
|
82
|
+
coordinates: latitude && longitude ? { lat: latitude, lng: longitude } : null,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
37
85
|
```
|
|
38
86
|
|
|
39
|
-
### Step 3:
|
|
87
|
+
### Step 3: Edge Middleware (middleware.ts)
|
|
88
|
+
|
|
89
|
+
Middleware runs before every request and can rewrite, redirect, or add headers:
|
|
90
|
+
|
|
40
91
|
```typescript
|
|
41
|
-
//
|
|
42
|
-
|
|
92
|
+
// middleware.ts (must be at project root or src/)
|
|
93
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
43
94
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
95
|
+
export function middleware(request: NextRequest) {
|
|
96
|
+
// Example 1: Redirect based on country
|
|
97
|
+
const country = request.geo?.country ?? 'US';
|
|
98
|
+
if (country === 'DE') {
|
|
99
|
+
return NextResponse.redirect(new URL('/de', request.url));
|
|
100
|
+
}
|
|
48
101
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
102
|
+
// Example 2: A/B testing with cookies
|
|
103
|
+
const bucket = request.cookies.get('ab-bucket')?.value;
|
|
104
|
+
if (!bucket) {
|
|
105
|
+
const response = NextResponse.next();
|
|
106
|
+
response.cookies.set('ab-bucket', Math.random() > 0.5 ? 'a' : 'b');
|
|
107
|
+
return response;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Example 3: Add security headers
|
|
111
|
+
const response = NextResponse.next();
|
|
112
|
+
response.headers.set('X-Frame-Options', 'DENY');
|
|
113
|
+
response.headers.set('X-Content-Type-Options', 'nosniff');
|
|
114
|
+
response.headers.set('Referrer-Policy', 'strict-origin-when-cross-origin');
|
|
115
|
+
return response;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Only run on specific paths — skip static assets
|
|
119
|
+
export const config = {
|
|
120
|
+
matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'],
|
|
121
|
+
};
|
|
122
|
+
```
|
|
55
123
|
|
|
56
|
-
|
|
124
|
+
### Step 4: Edge Function with Streaming
|
|
57
125
|
|
|
58
|
-
### Complete Workflow
|
|
59
126
|
```typescript
|
|
60
|
-
//
|
|
127
|
+
// api/stream.ts
|
|
128
|
+
export const config = { runtime: 'edge' };
|
|
129
|
+
|
|
130
|
+
export default function handler(): Response {
|
|
131
|
+
const encoder = new TextEncoder();
|
|
132
|
+
const stream = new ReadableStream({
|
|
133
|
+
async start(controller) {
|
|
134
|
+
for (let i = 0; i < 5; i++) {
|
|
135
|
+
controller.enqueue(encoder.encode(`data: chunk ${i}\n\n`));
|
|
136
|
+
await new Promise(r => setTimeout(r, 500));
|
|
137
|
+
}
|
|
138
|
+
controller.enqueue(encoder.encode('data: [DONE]\n\n'));
|
|
139
|
+
controller.close();
|
|
140
|
+
},
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
return new Response(stream, {
|
|
144
|
+
headers: {
|
|
145
|
+
'Content-Type': 'text/event-stream',
|
|
146
|
+
'Cache-Control': 'no-cache',
|
|
147
|
+
Connection: 'keep-alive',
|
|
148
|
+
},
|
|
149
|
+
});
|
|
150
|
+
}
|
|
61
151
|
```
|
|
62
152
|
|
|
63
|
-
###
|
|
153
|
+
### Step 5: Edge Config (Key-Value Store)
|
|
154
|
+
|
|
64
155
|
```typescript
|
|
65
|
-
//
|
|
156
|
+
// api/feature-flags.ts
|
|
157
|
+
import { get } from '@vercel/edge-config';
|
|
158
|
+
|
|
159
|
+
export const config = { runtime: 'edge' };
|
|
160
|
+
|
|
161
|
+
export default async function handler(): Promise<Response> {
|
|
162
|
+
// Read from Edge Config — sub-millisecond reads at the edge
|
|
163
|
+
const maintenanceMode = await get<boolean>('maintenance_mode');
|
|
164
|
+
const featureFlags = await get<Record<string, boolean>>('feature_flags');
|
|
165
|
+
|
|
166
|
+
if (maintenanceMode) {
|
|
167
|
+
return Response.json({ status: 'maintenance' }, { status: 503 });
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return Response.json({ features: featureFlags });
|
|
171
|
+
}
|
|
66
172
|
```
|
|
67
173
|
|
|
174
|
+
Install: `npm install @vercel/edge-config`
|
|
175
|
+
|
|
176
|
+
## Edge vs Serverless Comparison
|
|
177
|
+
|
|
178
|
+
| Feature | Edge Runtime | Node.js (Serverless) |
|
|
179
|
+
|---------|-------------|---------------------|
|
|
180
|
+
| Cold start | None (~0ms) | 250ms–1s+ |
|
|
181
|
+
| Max duration | 30s (Hobby), 5min (Pro) | 10s (Hobby), 5min (Pro) |
|
|
182
|
+
| Max size | 1 MB (after gzip) | 250 MB (unzipped) |
|
|
183
|
+
| APIs available | Web Standard APIs | Full Node.js API |
|
|
184
|
+
| npm packages | Limited (no native modules) | All npm packages |
|
|
185
|
+
| Global deployment | Automatic | Single region default |
|
|
186
|
+
| Use case | Auth, routing, A/B, geo | Database queries, heavy compute |
|
|
187
|
+
|
|
188
|
+
## Available Web APIs in Edge Runtime
|
|
189
|
+
|
|
190
|
+
`fetch`, `Request`, `Response`, `Headers`, `URL`, `URLSearchParams`, `TextEncoder`, `TextDecoder`, `ReadableStream`, `WritableStream`, `TransformStream`, `crypto`, `atob`, `btoa`, `structuredClone`, `setTimeout`, `setInterval`, `AbortController`
|
|
191
|
+
|
|
192
|
+
**NOT available**: `fs`, `path`, `child_process`, `net`, `http`, `dns`, native Node.js modules
|
|
193
|
+
|
|
194
|
+
## Output
|
|
195
|
+
|
|
196
|
+
- Edge Function deployed globally with zero cold starts
|
|
197
|
+
- Geolocation-based routing using Vercel's injected headers
|
|
198
|
+
- Middleware running authentication and A/B tests at the edge
|
|
199
|
+
- Streaming responses for real-time data delivery
|
|
200
|
+
|
|
201
|
+
## Error Handling
|
|
202
|
+
|
|
203
|
+
| Error | Cause | Solution |
|
|
204
|
+
|-------|-------|----------|
|
|
205
|
+
| `EDGE_FUNCTION_INVOCATION_FAILED` | Runtime error in edge function | Check logs — no `try/catch` around async code |
|
|
206
|
+
| `Dynamic Code Evaluation not allowed` | Using `eval()` or `new Function()` | Refactor to avoid dynamic code evaluation |
|
|
207
|
+
| `Module not found` | npm package uses Node.js APIs | Use edge-compatible alternative or switch to Node.js runtime |
|
|
208
|
+
| `FUNCTION_PAYLOAD_TOO_LARGE` | Edge function bundle > 1 MB | Tree-shake imports, split into smaller functions |
|
|
209
|
+
| `TypeError: x is not a function` | Node.js API used in edge runtime | Replace with Web Standard API equivalent |
|
|
210
|
+
|
|
68
211
|
## Resources
|
|
69
|
-
|
|
70
|
-
- [
|
|
212
|
+
|
|
213
|
+
- [Edge Functions Documentation](https://vercel.com/docs/functions/runtimes/edge)
|
|
214
|
+
- [Edge Middleware](https://vercel.com/docs/functions/edge-middleware)
|
|
215
|
+
- [Edge Config](https://vercel.com/docs/edge-config)
|
|
216
|
+
- [Edge Runtime API](https://vercel.com/docs/functions/runtimes/edge)
|
|
217
|
+
- [Vercel Geolocation Headers](https://vercel.com/docs/headers/request-headers)
|
|
71
218
|
|
|
72
219
|
## Next Steps
|
|
73
|
-
|
|
220
|
+
|
|
221
|
+
For common errors, see `vercel-common-errors`.
|