Deployment on Netlify (Build, Redirects, and Headers)
Overview
This section describes how the Nexora Web Agency static marketing site is built and deployed on Netlify. It covers:
- Build and publish configuration via
netlify.tomland npm scripts, ensuring that the compileddist/directory contains all routes, assets, and public files ready for deployment. - Environment configuration for production builds, including the Node.js runtime version.
- Edge‐level routing rules for clean URLs, 404 handling, and SPA fallbacks.
- HTTP headers for security hardening and asset caching, applied at the CDN edge.
Together, these settings enable a zero-server, high-performance deployment with automatic continuous deployment from the main Git branch, robust SEO, and hardened security.
Build & Publish Configuration
Netlify Build Settings
The core build settings live in netlify.toml:
[build]
# Directory to publish (output of the npm build script)
publish = "dist"
# Build command invoked by Netlify
command = "npm run build"
[context.production.environment]
# Enforce Node.js 20 for Netlify’s build container
NODE_VERSION = "20"
These lines tell Netlify to:
- Run
npm run buildon each deploy. - Publish the contents of the newly generated
dist/folder to the CDN at the end of the build .
Local Build & Development Scripts
In package.json, two npm scripts orchestrate the local build and serve workflow:
"scripts": {
"build": "rm -rf dist && mkdir -p dist && cp -R src/pages/* dist/ 2>/dev/null || true && cp -R src/assets dist/assets 2>/dev/null || true && cp -R public/* dist/ 2>/dev/null || true",
"dev": "npm run build && npx serve dist"
}
-
npm run build- Deletes any existing
dist/folder. - Creates
dist/. - Copies all HTML pages from
src/pages/intodist/. - Copies assets (
src/assets/) intodist/assets/. - Copies any files under
public/intodist/. - Suppresses errors if any source directory is empty .
- Deletes any existing
-
npm run dev- Executes the build, then serves
dist/via theservestatic file server on localhost for previewing.
- Executes the build, then serves
Distribution Artifact Structure
After building, the dist/ directory has this layout:
dist/
├── index.html
├── about.html
├── services/… # All nested .html pages from src/pages/services/
├── assets/
│ ├── css/ # global.css, utilities.css, main.css
│ └── js/ # forms.js, template.js, etc.
├── icons/ # Copied from public/icons/
├── images/ # Copied from public/images/
└── 404.html
Redirects Configuration
Netlify’s edge routing rules enforce clean URLs and fallback behavior:
[[redirects]]
# SPA fallback (not recommended for multi-page static sites)
from = "/*"
to = "/index.html"
status = 200
[[redirects]]
# 404 fallback: serve custom 404.html for any unmatched path
from = "/*"
to = "/404.html"
status = 404
- The first rule rewrites all requests to
index.html(useful for SPAs). - The second rule ensures any URL not matching a file in
dist/returns the custom 404 page .
Headers Configuration
Note: For a purely multi-page static site, you may remove the SPA fallback rule to avoid masking legitimate 404s, relying only on the second rule.
Security and caching headers are injected at the CDN edge via Netlify’s headers API:
# Global security headers for every route
[[headers]]
for = "/*"
[headers.values]
X-Frame-Options = "DENY"
X-Content-Type-Options = "nosniff"
X-XSS-Protection = "1; mode=block"
Referrer-Policy = "strict-origin-when-cross-origin"
# Aggressive caching for immutable image assets
[[headers]]
for = "/images/*"
[headers.values]
Cache-Control = "public, max-age=31536000, immutable"
# Aggressive caching for unhashed CSS/JS (treated as immutable)
[[headers]]
for = "/assets/*"
[headers.values]
Cache-Control = "public, max-age=31536000, immutable"
- Global Security: Prevents clickjacking, MIME sniffing, XSS injection, and restricts referrer metadata.
- Caching:
- All images under
/images/and all files under/assets/are served with a one-year immutable cache directive, enabling maximum edge performance .
- All images under
Deployment Flow
flowchart TB
Developer[Developer]
GitHub[GitHub Main Branch]
Netlify[Netlify CI/CD]
Build[Build Container<br/>Node.js 20]
NPMBuild[npm run build]
Dist[dist/ Artifact]
CDN[Netlify CDN Edge]
Developer -->|Push to Main| GitHub
GitHub -->|Webhook| Netlify
Netlify --> Build
Build -->|Runs "npm run build"| NPMBuild
NPMBuild --> Dist
Dist -->|Publish| CDN
CDN -->|Serve| EndUser[End User]
- A commit to
maintriggers Netlify via GitHub webhook. - Netlify provisions a build container running Node.js 20.
- It executes
npm run build, producingdist/. - The
dist/folder is deployed to Netlify’s global CDN. - End users receive assets with the configured redirects and headers in place.
Key Configuration Reference
| File | Purpose |
|---|---|
| netlify.toml | Build, publish, environment, redirects, and headers |
| package.json | Local build & dev scripts for artifact generation |
| public/ | Unprocessed static assets copied into dist/ |
| src/pages/ | Source HTML pages compiled into dist/ |
| src/assets/ | CSS/JS assets copied into dist/assets/ |
These settings ensure a seamless, performant, and secure deployment pipeline on Netlify, aligning with the project’s goals of high SEO, low latency, and serverless form handling.