A Next.js application that demonstrates edge-level redirects and rewrites powered by Contentstack CMS, designed to be hosted on Contentstack Launch. This solution allows content editors to manage URL redirects and rewrites directly from the CMS without any code changes.
This project implements a CMS-driven redirect/rewrite system where:
- No code changes required - Redirects and rewrites are managed entirely in Contentstack
- Edge-level performance - Rules execute at the edge before reaching the origin
- Content editor friendly - Marketing teams can manage redirects without developer help
- Build-time injection - Rules are fetched from CMS and injected into edge functions during build
This solution uses Contentstack CMS to store redirect/rewrite rules that are fetched at build time and injected into edge functions:
- ✅ Edge performance - Redirects execute at CDN edge, not at origin
- ✅ No database needed - Rules stored in Contentstack CMS
- ✅ Version controlled - Rules follow Contentstack's publish workflow
- ✅ Automatic updates - New deployments fetch latest rules from CMS
- Content editors create redirect/rewrite entries in Contentstack
- Build script fetches all rules from Contentstack via REST API
- Edge function is auto-generated with rules embedded
- Contentstack Launch deploys and executes edge function on all requests
┌─────────────────────┐
│ Add/Update entry │
│ in Contentstack │
└──────────┬──────────┘
│ (deploy)
▼
┌─────────────────────┐
│ Build fetches │
│ rules from CMS │
└──────────┬──────────┘
│
▼
┌─────────────────────┐
│ Edge function │
│ auto-generated │
└──────────┬──────────┘
│
▼
┌─────────────────────┐
│ Redirects/Rewrites │
│ execute at edge │
└─────────────────────┘
npm installCreate a .env.local file in the root directory:
CONTENTSTACK_API_KEY=your_api_key
CONTENTSTACK_DELIVERY_TOKEN=your_delivery_token
CONTENTSTACK_ENVIRONMENT=your_environment
CONTENTSTACK_APP_HOST=cdn.contentstack.io
# Content type UIDs (optional - defaults shown)
REDIRECT_CT=redirect
REWRITE_CT=rewrite
# Optional: For Live Preview
NEXT_PUBLIC_CONTENTSTACK_API_KEY=your_api_key
NEXT_PUBLIC_CONTENTSTACK_DELIVERY_TOKEN=your_delivery_token
NEXT_PUBLIC_CONTENTSTACK_ENVIRONMENT=your_environment
NEXT_PUBLIC_CONTENTSTACK_MANAGEMENT_TOKEN=your_management_token
NEXT_PUBLIC_CONTENTSTACK_PREVIEW_HOST=your_preview_host
NEXT_PUBLIC_CONTENTSTACK_LIVE_PREVIEW_HOST=your_live_preview_host
NEXT_PUBLIC_CONTENTSTACK_APP_HOST=your_app_host# Fetch rules and generate edge function
npm run build:rules
# Or run dev (automatically runs prebuild)
npm run devCreate a content type named "redirect" with the following fields:
| Field UID | Display Name | Type | Required | Description |
|---|---|---|---|---|
title |
Title | Text | Yes | Name of the redirect rule |
source |
Source | Text | Yes | Source path (e.g., /old-page) |
destination |
Destination | Text | Yes | Destination path (e.g., /new-page) |
statuscode |
Status Code | Number/Dropdown | No | HTTP status: 301, 302, 307, 308 |
Status Code Options:
301- Permanent Redirect (default)302- Temporary Redirect307- Temporary Redirect (preserves method)308- Permanent Redirect (preserves method)
Create a content type named "rewrite" with the following fields:
| Field UID | Display Name | Type | Required | Description |
|---|---|---|---|---|
title |
Title | Text | Yes | Name of the rewrite rule |
source |
Source | Text | Yes | Source path (e.g., /api/proxy) |
destination |
Destination | Text | Yes | Destination URL to proxy to |
- Go to Contentstack → Your Stack → Entries
- Create new entry in the "redirect" content type
- Fill in the fields:
source:/about(the old URL)destination:/contact(the new URL)statuscode:301
- Publish the entry
- Trigger a new deployment on Contentstack Launch
The redirect will be active after deployment!
| Source | Destination | Status Code | Description |
|---|---|---|---|
/old-blog |
/blog |
301 | Permanent redirect |
/temp-promo |
/sale |
302 | Temporary redirect |
/legacy/* |
/new/* |
301 | Wildcard redirect |
/external |
https://x.com |
302 | External redirect |
Set up Contentstack Automation to automatically redeploy your Launch project when redirect entries are published or updated:
- Go to Contentstack → Your Stack → Automation Hub
- Create new automation with:
- Trigger: Entry Published / Entry Updated
- Content Type: Select
redirect(andrewriteif using)
- Add Action: Trigger Contentstack Launch Deployment
- Select your Launch project
- Save and Enable the automation
Now when you publish or update any redirect entry, the project automatically redeploys with the latest rules!
- User visits
https://yourdomain.com/about - Edge function executes before reaching Next.js
- Matches redirect rule:
source: /about→destination: /contact - Returns 301 response with
Location: /contactheader - Browser redirects to
/contact - Next.js serves the
/contactpage
- Go to Contentstack Launch dashboard
- Connect your Git repository
- Configure build settings:
- Build Command:
npm run build - Output Directory:
.next - Node Version: 18.x or higher
- Build Command:
Add your Contentstack environment variables in Launch:
CONTENTSTACK_API_KEYCONTENTSTACK_DELIVERY_TOKENCONTENTSTACK_ENVIRONMENTCONTENTSTACK_APP_HOST
The edge function is automatically generated at functions/[proxy].edge.js during the build process. Contentstack Launch detects and deploys this edge function.
To add or update redirects:
- Create/Edit redirect entry in Contentstack
- Publish the entry
- Trigger deployment on Contentstack Launch
- New redirects are active after deployment completes
├── app/
│ ├── page.tsx # Home page
│ ├── about/page.tsx # About page
│ ├── contact/page.tsx # Contact page
│ └── services/page.tsx # Services page
├── functions/
│ └── [proxy].edge.js # Auto-generated edge function
├── scripts/
│ ├── fetch-rules.js # Fetches rules from Contentstack
│ └── update-edge-file.js # Generates edge function
├── lib/
│ └── contentstack.ts # Contentstack SDK initialization
└── .env.local # Environment variables
| Script | Description |
|---|---|
npm run dev |
Start dev server (runs prebuild automatically) |
npm run build |
Build for production |
npm run prebuild |
Fetch rules and generate edge function |
npm run build:rules |
Manually regenerate edge function |
- Check entry is published: Redirect entries must be published in Contentstack
- Verify deployment: Ensure a new deployment was triggered after publishing
- Check build logs: Look for "✅ Fetched X redirects" in build output
- Check destination exists: Ensure the destination page/route exists
- Verify path format: Destination should start with
/for internal paths
- Check environment variables: Ensure all Contentstack credentials are set in Launch
- Verify content type UID: Default is
redirect- checkREDIRECT_CTenv var if different