Netlify Deployment Cheat Sheet
Deploy static sites and JAMstack apps on Netlify using the CLI, netlify.toml configuration, functions, and build settings.
Netlify CLI Basics
Install and use the Netlify CLI to deploy and manage sites.
npm i -g netlify-cli # Install CLInetlify login # Authenticatenetlify init # Link/create a sitenetlify deploy # Deploy a draft (preview) buildnetlify deploy --prod # Deploy to productionnetlify dev # Run local dev server with redirects/functionsnetlify status # Show current site/account infonetlify open # Open site dashboard in browsernetlify env:set API_KEY value # Set an environment variable
netlify.toml Config
Define build settings, redirects, and headers as code.
[build] command = "npm run build" publish = "dist" functions = "netlify/functions"[[redirects]] from = "/old-path" to = "/new-path" status = 301[[redirects]] from = "/*" to = "/index.html" status = 200[[headers]] for = "/*" [headers.values] X-Frame-Options = "DENY"
Netlify Function
Write a serverless function using the standard handler signature.
// netlify/functions/hello.jsexports.handler = async (event, context) => { const name = event.queryStringParameters?.name || 'world'; return { statusCode: 200, body: JSON.stringify({ message: `Hello, ${name}!` }), };};
Core Concepts
Key Netlify features for deploying and scaling sites.
- Deploy previews- automatic preview URL generated for every pull request on a linked Git repo
- Split testing- built-in A/B testing across branch deploys via `netlify.toml` branch config
- Netlify Identity- built-in user authentication service for JAMstack sites
- Forms- add `data-netlify="true"` to an HTML `<form>` to get serverless form handling with no backend code
- Edge Functions- Deno-based functions that run at the CDN edge, defined in `netlify/edge-functions`
- Build plugins- reusable build-step hooks configured under `[[plugins]]` in netlify.toml
- Context-based env vars- `[context.production.environment]` and similar blocks scope variables per deploy context
Scheduled Function (Cron)
Run a Netlify Function on a cron schedule using the scheduled-functions integration.
// netlify/functions/nightly-cleanup.jsimport { schedule } from '@netlify/functions';const handler = async (event) => { console.log('Running nightly cleanup at', event.next_run); // ...do cleanup work... return { statusCode: 200 };};export { handler };// netlify.toml// [functions."nightly-cleanup"]// schedule = "@daily"
Background Function
Suffix a function with -background to run it async for up to 15 minutes without blocking the caller.
// netlify/functions/send-report-background.jsexports.handler = async (event) => { const { userId } = JSON.parse(event.body); await generateAndEmailReport(userId); // long-running work, up to 15 min // Background functions return 202 immediately to the caller; // this return value is not sent back to the client. return { statusCode: 202 };};
Edge Function with Geo + Rewrite
Deno-based Edge Function that rewrites content based on the visitor's country before it hits the CDN cache.
// netlify/edge-functions/geo-redirect.jsexport default async (request, context) => { const country = context.geo.country?.code; if (country === 'FR') { return Response.redirect(new URL('/fr', request.url), 302); } const response = await context.next(); response.headers.set('x-visitor-country', country ?? 'unknown'); return response;};export const config = { path: '/*' };
Reverse Proxy Rewrite
Proxy requests to an external API through your own domain using a 200 rewrite to avoid CORS.
[[redirects]] from = "/api/*" to = "https://api.example.com/:splat" status = 200 force = true headers = { X-From = "netlify-proxy" }# Country-scoped rewrite using the built-in :country placeholder[[redirects]] from = "/pricing" to = "/pricing/eu" status = 200 conditions = { Country = ["DE", "FR", "IT"] }
Advanced Netlify Primitives
Lesser-known building blocks beyond the basic CLI/functions workflow.
- Netlify Blobs- durable key/value + binary object store usable from Functions/Edge Functions without provisioning a database
- On-demand Builders- functions that render once, cache the output at the CDN, and revalidate on a TTL (ISR-style) via `builder()` wrapper
- Atomic deploys- every deploy uploads a full immutable snapshot; instant rollback is just re-pointing production alias to a prior deploy
- Deploy contexts- `production`, `deploy-preview`, `branch-deploy` each get their own env var scoping and build hooks
- Snippet injection- inject `<script>`/`<meta>` into rendered HTML via dashboard rules or `netlify.toml`, no code changes needed
- Large Media- Git LFS-backed storage with on-the-fly image transforms for big binary assets
- Monorepo `base` + `packages`- `[build] base = "apps/web"` scopes a build to a subdirectory so multiple sites can share one repo
Use the SPA fallback redirect rule (`/* -> /index.html` with status 200, not 301) so client-side routers handle deep links correctly without breaking the browser back button or causing redirect loops.