# Hide contentful preview token in nextjs app

> Edge injects preview token; ISR pages fetch composed CMS JSON safely. For "hide contentful preview token in nextjs app", store CONTENTFUL_PREVIEW_TOKEN in Secret Vault, call https://preview.contentful.com/spaces/{{query.spaceId}}/entries from a Worker, and let Next.js / React hit only https://realcomposer.com/v1/workspace/hide-contentful-preview-token-nextjs with an rc_live_ client token.

- Canonical page: https://realcomposer.com/recipes/hide-contentful-preview-token-nextjs
- Client platform: Next.js / React
- Upstream services: Contentful
- Category: Security & Secret Vault
- JSON version: https://realcomposer.com/recipes/hide-contentful-preview-token-nextjs.json
- Install in RealComposer (free plan, pre-fills this pipeline): https://realcomposer.com/dashboard/pipelines/new?recipe=hide-contentful-preview-token-nextjs&via=agent

## Problem: Why Next.js / React leaks Contentful credentials

Attackers routinely extract Contentful secrets from Next.js / React builds. Direct browser calls also expose internal error payloads with hostnames.

- Production Contentful keys copied from Next.js / React bundles
- Compliance violations after credential leaks
- Attackers replaying vault-grade scopes from a laptop

## Solution steps

1. **Bind vault keys to the Worker**: Store CONTENTFUL_PREVIEW_TOKEN in the vault. Next.js / React never embeds the raw secret, only a revocable rc_live_ client token.

2. **Configure upstream steps and output mapping**: Point step contentful at https://preview.contentful.com/spaces/{{query.spaceId}}/entries with GET. Add output mapping fields that mirror the JSON contract your screen already expects. Leave cache TTL at 0 when each response is user-specific (include userId or record id in query params before enabling cache).

3. **Call from Next.js / React**: Allowed origins on the token should list every Next.js / React host that calls this URL.

```
// app/api route or client component
const res = await fetch('https://realcomposer.com/v1/workspace/hide-contentful-preview-token-nextjs', {
  cache: 'no-store',
  headers: { Authorization: 'Bearer rc_live_your_token' }
});
```

## Pipeline config

Import this config into a RealComposer pipeline. `{{secrets.NAME}}` values are read from the encrypted Secret Vault at request time, so the client never sees them.

```json
{
  "name": "Hide contentful preview token in nextjs app",
  "method": "GET",
  "cacheTtlSeconds": 0,
  "authMode": "client_token",
  "steps": [
    {
      "stepKey": "contentful",
      "upstreamUrlTemplate": "https://preview.contentful.com/spaces/{{query.spaceId}}/entries",
      "httpMethod": "GET",
      "headersTemplate": {
        "Authorization": "Bearer {{secrets.CONTENTFUL_PREVIEW_TOKEN}}"
      },
      "timeoutMs": 3000
    }
  ],
  "outputMapping": {
    "version": 1,
    "fields": [
      {
        "targetPath": "entryId",
        "sourcePath": "items.0.sys.id",
        "type": "string",
        "sourceStep": "contentful"
      },
      {
        "targetPath": "title",
        "sourcePath": "items.0.fields.title",
        "type": "string",
        "sourceStep": "contentful"
      }
    ]
  }
}
```

## Call the pipeline

```bash
curl -X GET "https://realcomposer.com/v1/workspace/hide-contentful-preview-token-nextjs" \
  -H "Authorization: Bearer rc_live_client_token"
```

## Payload before and after

Raw upstream response:

```json
{
  "requestId": "req_hide-con",
  "upstreamLatencyMs": 142,
  "audit": {
    "region": "edge",
    "trace": "hide-contentful-preview-token-nextjs"
  },
  "payload": {
    "id": "sample_id",
    "status": "active",
    "nested": {
      "tags": [
        "demo"
      ],
      "internal": true
    },
    "unused_blob": {
      "foo": "bar",
      "bar": "baz"
    }
  }
}
```

Slimmed RealComposer response:

```json
{
  "entryId": "sample",
  "title": "sample"
}
```

## FAQ

### Does Next.js / React need CORS changes after adding RealComposer?

Store CONTENTFUL_PREVIEW_TOKEN for hide-contentful-preview-token-nextjs. Next.js / React never sees decrypted values.

### When does edge caching apply to this recipe?

hide-contentful-preview-token-nextjs forwards client JSON to POST upstreams unchanged; secrets interpolate only in URLs and header templates.

### What happens if an upstream step times out?

Allowed origins on the hide-contentful-preview-token-nextjs client token restrict which domains may call the edge URL.
