This project demonstrates how to dynamically generate a launch.json file during the build process in Contentstack Launch. This approach allows to manage Launch configuration centrally through Contentstack content types, eliminating the need to hardcode redirects, rewrites, and cache configurations directly in your repository.
.
├── lib/
│ └── contentstack-server.ts # Centralized Contentstack Stack setup
├── scripts/
│ └── generateLaunchJson.ts # Script to generate launch.json
├── package.json
├── tsconfig.scripts.json
└── launch.json # Generated at build time
Create the required content types in the stack by following the guide of Create Content Model to manage your Launch configuration:
- **Redirects** - for URL redirects
- **Rewrites** - for URL rewrites
- **Cache** - for cache priming URLsSet up your Contentstack Stack connection following the Connect stack Documentation
Clone the repository:
git clone <your-repository-url>
cd Dynamic_launchInstall dependencies:
npm installCreate a .env.local file at the root with your stack credentials:
CONTENTSTACK_API_KEY=your_stack_api_key
CONTENTSTACK_DELIVERY_TOKEN=your_delivery_token
CONTENTSTACK_ENVIRONMENT=environment nameAdd a prebuild hook to generate launch.json before build.
{
"scripts": {
"prebuild": "npm run generate:launch",
"generate:launch": "ts-node --project tsconfig.scripts.json scripts/generateLaunchJson.ts",
"build": "next build"
}
}prebuild→ runs automatically beforenpm run buildgenerate:launch→ generates launch.jsonbuild→ builds the Next.js app
Deploy your project to Launch. The build process automatically generates launch.json with your current configuration.
{
"redirects": [
{
"source": "/source",
"destination": "/destination",
"statusCode": 308,
"response": {
"headers": {
"x-powered-by": "launch"
}
}
}
],
"rewrites": [
{
"source": "/source",
"destination": "/destination",
"request": {
"headers": {
"x-api-key": "api-key"
}
},
"response": {
"headers": {
"x-powered-by": "launch"
}
}
}
],
"cache": {
"cachePriming": {
"urls": ["/delay", "/blog.html", "/new-page.html"]
}
}
}Script fails to run:
- Verify your environment variables are correctly set
- Ensure your Contentstack credentials have the necessary permissions
Generated launch.json is empty:
- Verify that you have published entries in your content types
- Check that the environment field matches your
CONTENTSTACK_ENVIRONMENTvariable
Build process fails:
- Check the build logs for specific error messages
- Verify that all dependencies are installed
Reference Documentation