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.toml and npm scripts, ensuring that the compiled dist/ 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:

  1. Run npm run build on each deploy.
  2. 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/ into dist/.
    • Copies assets (src/assets/) into dist/assets/.
    • Copies any files under public/ into dist/.
    • Suppresses errors if any source directory is empty .
  • npm run dev

    • Executes the build, then serves dist/ via the serve static file server on localhost for previewing.

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 .

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]
  1. A commit to main triggers Netlify via GitHub webhook.
  2. Netlify provisions a build container running Node.js 20.
  3. It executes npm run build, producing dist/.
  4. The dist/ folder is deployed to Netlify’s global CDN.
  5. 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.