Overview
Astro supports multiple deployment strategies depending on your project needs. You can deploy as a static site or enable server-side rendering (SSR) with adapters for various hosting platforms.Build Your Site
Run the build command to create a production-ready version of your site:By default, this creates a
dist/ directory with your built site.Choose Your Deployment Strategy
Astro supports two main deployment modes:
- Static (SSG): Pre-renders all pages at build time
- Server (SSR): Renders pages on-demand with an adapter
Static Site Deployment
For static sites, Astro builds all pages to HTML at build time. This is the default mode and works with any static hosting provider.Configuration
astro.config.mjs
Popular Static Hosting Providers
Netlify
Drop-in support with zero configuration. Just connect your Git repository.
Vercel
Automatic deployments from Git with preview URLs for every commit.
Cloudflare Pages
Fast global CDN with unlimited bandwidth on the free tier.
GitHub Pages
Free hosting for public repositories with custom domain support.
Server-Side Rendering (SSR)
For dynamic features like API routes, user authentication, or database queries, use SSR with an adapter.Node.js Adapter
The Node adapter allows you to deploy to any platform that supports Node.js:astro.config.mjs
Vercel Adapter
Deploy serverless functions to Vercel’s edge network:astro.config.mjs
Netlify Adapter
Deploy to Netlify Functions with automatic configuration:astro.config.mjs
Cloudflare Adapter
Deploy to Cloudflare Workers for edge computing:astro.config.mjs
Hybrid Rendering
Use hybrid mode to pre-render most pages while keeping specific routes dynamic:astro.config.mjs
src/pages/api/user.astro
Environment Variables
Managing Secrets
Managing Secrets
Create a Access them in your code:Important: Never commit
.env file for local development:.env
.env files. Add them to .gitignore.Public vs Private Variables
Public vs Private Variables
Variables prefixed with
PUBLIC_ are exposed to the browser:.env
Build Output
Thedist/ directory structure varies by output mode:
Static Output:
Deployment Checklist
Set your site URL
Configure the
site option in astro.config.mjs for proper canonical URLs and sitemap generation.Configure environment variables
Set production environment variables in your hosting provider’s dashboard.
Test the build locally
Run
astro build && astro preview to test the production build before deploying.Troubleshooting
Build fails with module errors
Build fails with module errors
Ensure all dependencies are listed in
package.json and not just in devDependencies. Server-side code needs production dependencies.404 errors on deployed site
404 errors on deployed site
Check that your hosting provider is configured to handle client-side routing. For SPAs, you may need a redirect rule to send all requests to
index.html.Environment variables not working
Environment variables not working
Verify that variables are set in your hosting provider’s dashboard. Remember that
PUBLIC_ prefix is required for client-side access.