Vercel Adapter
Guidelines for deploying Astro projects to Vercel using the @astrojs/vercel adapter.
astro
## Astro Vercel Adapter Guidelines
1. Purpose: Enables deployment of Astro projects to Vercel, specifically required for Server-Side Rendering (SSR / on-demand routes). Also enables Vercel-specific features like Image Optimization and Web Analytics, even for static sites.
2. Installation: Use the Astro CLI for automatic setup:
```bash
npx astro add vercel
# or pnpm add @astrojs/vercel / yarn add @astrojs/vercel
```
This installs the adapter and updates `astro.config.mjs`.
3. Usage:
- SSR/Hybrid: Required when using `output: 'server'` or `output: 'hybrid'` in `astro.config.mjs` to deploy server-rendered pages or API endpoints to Vercel Functions (Serverless or Edge).
- Static: Only needed if you want to use Vercel's Web Analytics or Image Optimization features with a statically generated Astro site. Otherwise, no adapter is needed for static deployments.
4. Configuration Options (Passed to `vercel()` in `astro.config.mjs`):
- `webAnalytics: { enabled: boolean }`: Enable Vercel Web Analytics. Injects the necessary tracking script. Defaults to `false`.
- `imageService: boolean`: Enable Vercel's Image Optimization service for images processed by `astro:assets` (`<Image />`, `<Picture />`, `getImage()`). Defaults to `false`. Requires configuration in Vercel project settings.
- `imagesConfig`: (`VercelImagesConfig`): Passthrough configuration for Vercel's native Image Optimization (domains, sizes, etc.). Use this if _not_ using `imageService: true`.
- `devImageService: 'sharp' | 'squoosh' | string`: Specify the image service (`sharp` or `squoosh`) to use locally during `astro dev` _only_ when `imageService: true` is enabled. Defaults to `sharp`. Useful if Sharp has installation issues locally.
- `isr: boolean | { expiration: number, exclude?: (string | RegExp)[], bypassToken?: string }`: Enable Incremental Static Regeneration (ISR) for on-demand pages. Pages are cached after the first request.
- `expiration`: Cache duration in seconds.
- `exclude`: Array of paths (strings or RegExps) to _always_ render on-demand (never cache).
- `bypassToken`: Secret token to trigger on-demand regeneration (used with `exclude`).
- `includeFiles: string[]`: Array of file paths to force-include in the function bundle.
- `excludeFiles: string[]`: Array of file paths to explicitly exclude from the function bundle.
- `maxDuration: number`: Maximum execution time in seconds for Serverless Functions. Subject to Vercel plan limits.
- `skewProtection: boolean`: Enable Vercel's Skew Protection (requires Pro/Enterprise plan). Defaults to `false`.
- `edgeMiddleware: boolean`: Deploy Astro middleware (`src/middleware.js/ts`) as a Vercel Edge Function instead of part of the Serverless Function. Allows middleware to run on all requests (including static assets). `context.locals` is passed via headers. Defaults to `false`.
5. Middleware & Edge: When `edgeMiddleware: true`, middleware runs at the edge. Context data is serialized via JSON and passed in headers to serverless functions handling page rendering. Vercel Edge request context is available via `context.locals.vercel.edge`. Update `src/env.d.ts` with `EdgeLocals` for type safety.
6. Node.js Version: The adapter relies on specific Node.js versions supported by Vercel. Check Vercel project settings for available versions.
Reference: [@astrojs/vercel Adapter Docs](https://docs.astro.build/en/guides/integrations-guide/vercel/)