| name | netlify |
| description | [Applies to: **/*] This guide provides opinionated, actionable best practices for building, deploying, and maintaining high-performance, secure, and scalable Jamstack applications on Netlify. It focuses on modern workflows, code quality, and leveraging Netlify's edge-first architecture. |
| source | cursor_mdc |
netlify Best Practices
Netlify is the definitive platform for the Jamstack. Our workflow prioritizes pre-built, edge-first deployments, ensuring maximum performance, security, and developer experience. These guidelines enforce a consistent, high-quality approach to building for the modern web on Netlify.
1. Code Organization and Build Configuration
Always centralize your build logic and deployment settings in netlify.toml. This ensures consistency and reproducibility across environments and team members.
✅ GOOD: Standardized netlify.toml
[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"
X-XSS-Protection = "1; mode=block"
X-Content-Type-Options = "nosniff"
Referrer-Policy = "no-referrer-when-downgrade"
Content-Security-Policy = "default-src 'self'; script-src 'self' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self';"
Strict-Transport-Security = "max-age=63072000; includeSubDomains; preload"
❌ BAD: Relying on UI settings or missing critical configurations
[build]
command = "npm run build"
publish = "build"
2. Environment Variables and Secrets Management
Never hardcode sensitive information. Use Netlify's encrypted environment variables for all secrets. Differentiate between build-time and runtime variables.
✅ GOOD: Secure Environment Variables
import fetch from 'node-fetch';
export async function handler(event, context) {
const API_KEY = process.env.MY_EXTERNAL_API_KEY;
if (!API_KEY) {
return { statusCode: 500, body: 'API Key not configured.' };
}
}
[build.environment]
GATSBY_API_URL = "https://api.example.com/public"
❌ BAD: Hardcoding secrets or exposing them in client-side bundles
const API_KEY = "sk_YOUR_SECRET_KEY_HERE";
3. Performance Optimization
Leverage Netlify's edge network and build process for maximum speed.
✅ GOOD: Image Optimization with Build Plugins
Use Netlify Build Plugins for automated image optimization.
[[plugins]]
package = "@netlify/plugin-nextjs"
❌ BAD: Serving unoptimized, large images
<img src="/images/hero-lg.jpg" alt="Hero" />
4. Edge Functions for Dynamic Logic
Move lightweight server-side logic to the edge with Netlify Edge Functions for faster response times and reduced latency.
✅ GOOD: Edge Function for A/B Testing or Geo-targeting
import type { Context } from "https://edge.netlify.com/";
export default async (request: Request, context: Context) => {
const country = context.geo?.country?.name || "Unknown";
if (country === "Germany") {
return Response.redirect(new URL("/de", request.url));
}
return context.next();
};
❌ BAD: Relying on traditional serverless functions for every request
exports.handler = async (event, context) => {
const ip = event.headers['x-forwarded-for'];
return { statusCode: 200, body: `Hello from ${country}` };
};
5. Testing and CI/CD Hygiene
Integrate automated testing and quality checks into your Netlify build pipeline.
✅ GOOD: Automated Linting, Type Checking, and E2E Tests
Ensure your package.json scripts support these, and Netlify's build command triggers them.
{
"scripts": {
"build": "npm run lint && npm run typecheck && next build",
"lint": "eslint . --ext .js,.jsx,.ts,.tsx",
"typecheck": "tsc --noEmit",
"test:e2e": "cypress run"
}
}
[build]
command = "npm run build"
publish = "out"
[context.deploy-preview]
command = "npm run build && npm run test:e2e"
❌ BAD: Skipping quality checks in CI
[build]
command = "next build"
publish = "out"
6. Accessibility
Accessibility is a non-negotiable part of modern web development. Integrate tools to enforce it.
✅ GOOD: Automated Accessibility Checks
Use tools like Lighthouse CI or Pa11y in your build process.
{
"scripts": {
"build": "next build",
"audit:a11y": "lighthouse ci --config=./.lighthouseci.json"
}
}
[build]
command = "npm run build"
publish = "out"
[context.production]
command = "npm run build && npm run audit:a11y"
❌ BAD: Ignoring accessibility until manual review
<img src="image.jpg" />
7. Decoupled Architecture and API-First Development
Embrace the Jamstack philosophy: frontend decoupled from backend. Interact with services via APIs.
✅ GOOD: Consuming external APIs
export async function fetchProducts() {
const response = await fetch('/.netlify/functions/getProducts');
if (!response.ok) {
throw new Error('Failed to fetch products');
}
return response.json();
}
❌ BAD: Tightly coupled frontend and backend
const express = require('express');
const app = express();