Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ NEXT_PUBLIC_CONTENTSTACK_API_KEY=your_api_key_here
NEXT_PUBLIC_CONTENTSTACK_DELIVERY_TOKEN=your_delivery_token_here
NEXT_PUBLIC_CONTENTSTACK_PREVIEW_TOKEN=your_preview_token_here
NEXT_PUBLIC_CONTENTSTACK_ENVIRONMENT=preview
NEXT_PUBLIC_CONTENTSTACK_REGION=EU
NEXT_PUBLIC_CONTENTSTACK_REGION=EU # Options: NA, EU, AU, AZURE-NA, AZURE-EU, GCP-NA, GCP-EU
NEXT_PUBLIC_CONTENTSTACK_PREVIEW=true
NEXT_PUBLIC_CONTENTSTACK_SSR=true
NEXT_PUBLIC_CONTENTSTACK_EDITABLE_TAGS=true
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,49 @@ NEXT_PUBLIC_CONTENTSTACK_ENVIRONMENT=preview
NEXT_PUBLIC_CONTENTSTACK_PREVIEW=true
```


## Regions and endpoint configuration

Set `NEXT_PUBLIC_CONTENTSTACK_REGION` to the region your Contentstack account is on. The value is case-insensitive.

| Region | Value |
|---|---|
| AWS North America | `NA` or `US` |
| AWS Europe | `EU` |
| AWS Australia | `AU` |
| Azure North America | `AZURE-NA` |
| Azure Europe | `AZURE-EU` |
| GCP North America | `GCP-NA` |
| GCP Europe | `GCP-EU` |

> Not sure which region you're on? Check your Contentstack dashboard URL — `eu-app.contentstack.com` means EU, `app.contentstack.com` means NA. Free developer accounts are on EU.

All API endpoints (content delivery, live preview, Visual Builder) are automatically resolved from your region. You do not need to set them manually.

The following endpoint keys are resolved per region and available if you ever need them directly via `getContentstackEndpoint` from `@contentstack/utils`:

| Key | NA value |
|---|---|
| `contentDelivery` | `cdn.contentstack.io` |
| `preview` | `rest-preview.contentstack.com` |
| `application` | `app.contentstack.com` |
| `graphqlDelivery` | `graphql.contentstack.com` |
| `graphqlPreview` | `graphql-preview.contentstack.com` |
| `images` | `images.contentstack.io` |
| `assets` | `assets.contentstack.io` |
| `contentManagement` | `api.contentstack.io` |
| `auth` | `auth-api.contentstack.com` |

### Custom or dedicated environments

If your Contentstack account runs on a dedicated or private cloud instance, the standard region-based endpoints may not apply. In that case, override each endpoint individually using these environment variables. **Only set these if instructed by Contentstack support — standard accounts should leave them unset.**

```bash
NEXT_PUBLIC_CONTENTSTACK_CONTENT_DELIVERY=your-custom-cdn.example.com
NEXT_PUBLIC_CONTENTSTACK_PREVIEW_HOST=your-custom-preview.example.com
NEXT_PUBLIC_CONTENTSTACK_CONTENT_APPLICATION=your-custom-app.example.com
```

## Turn on Live Preview

Go to Settings > Live Preview. Click enable and select the `Preview` environment in the drop down. Hit save.
Expand Down
18 changes: 5 additions & 13 deletions app/api/middleware/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import contentstack from "@contentstack/delivery-sdk";
import { NextResponse } from "next/server"
import { getContentstackEndpoints, getRegionForString } from "@timbenniks/contentstack-endpoints";
import { getContentstackEndpoint, type ContentstackEndpoints } from "@contentstack/utils";

export async function GET(request: Request) {
const { searchParams } = new URL(request.url)
Expand All @@ -9,18 +9,10 @@ export async function GET(request: Request) {
const live_preview = searchParams.get("live_preview")
const preview_timestamp = searchParams.get("preview_timestamp")

// Use custom endpoints if provided, otherwise fall back to region-based endpoints
// For internal testing purposes at Contentstack we look for a custom region/hostnames in the env vars, you do not have to do this.
let hostname: string;
if (process.env.NEXT_PUBLIC_CONTENTSTACK_PREVIEW_HOST && process.env.NEXT_PUBLIC_CONTENTSTACK_CONTENT_DELIVERY) {
hostname = live_preview
? process.env.NEXT_PUBLIC_CONTENTSTACK_PREVIEW_HOST
: process.env.NEXT_PUBLIC_CONTENTSTACK_CONTENT_DELIVERY;
} else {
const region = getRegionForString(process.env.NEXT_PUBLIC_CONTENTSTACK_REGION as string);
const endpoints = getContentstackEndpoints(region, true);
hostname = (live_preview ? endpoints.preview : endpoints.contentDelivery) as string;
}
const endpoints = getContentstackEndpoint(process.env.NEXT_PUBLIC_CONTENTSTACK_REGION || 'NA', '', true) as ContentstackEndpoints;
const hostname = live_preview
? process.env.NEXT_PUBLIC_CONTENTSTACK_PREVIEW_HOST || endpoints.preview as string
: process.env.NEXT_PUBLIC_CONTENTSTACK_CONTENT_DELIVERY || endpoints.contentDelivery as string;

const headers = new Headers();
headers.append("Content-Type", "application/json");
Expand Down
10 changes: 5 additions & 5 deletions lib/contentstack.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import ContentstackLivePreview from "@contentstack/live-preview-utils";
import { getContentstackEndpoints, getRegionForString } from "@timbenniks/contentstack-endpoints";
import { getContentstackEndpoint, type ContentstackEndpoints } from "@contentstack/utils";

export const isPreview = process.env.NEXT_PUBLIC_CONTENTSTACK_PREVIEW === "true";

const region = getRegionForString(process.env.NEXT_PUBLIC_CONTENTSTACK_REGION as string)
const endpoints = getContentstackEndpoints(region, true)
// region resolution is now handled by getContentstackEndpoint
const endpoints = getContentstackEndpoint(process.env.NEXT_PUBLIC_CONTENTSTACK_REGION || 'NA', '', true) as ContentstackEndpoints

export function initLivePreview() {
ContentstackLivePreview.init({
Expand All @@ -17,8 +17,8 @@ export function initLivePreview() {
},
clientUrlParams: {
// Setting the client URL parameters for live preview
// for internal testing purposes at Contentstack we look for a custom host in the env vars, you do not have to do this.
host: process.env.NEXT_PUBLIC_CONTENTSTACK_CONTENT_APPLICATION || endpoints && endpoints.application
// for custom or dedicated Contentstack environments, override each endpoint individually using environment variables.
host: process.env.NEXT_PUBLIC_CONTENTSTACK_CONTENT_APPLICATION || endpoints.application as string
},
editButton: {
enable: true, // Enabling the edit button for live preview
Expand Down
Loading
Loading