Hide contentful preview token in nextjs app
Preview mode without public preview secrets
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.
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
How RealComposer Solves This at the Edge
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.
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).
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' }
});
{
"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"
}
}
}
{
"entryId": "sample",
"title": "sample"
}
Recipe Specs
- HTTP method
- GET
- Upstream calls
- 1
- Edge cache TTL
- No caching
- Client auth
- Restricted client token
- Mapped output fields
- 2
- Payload
- 255 B to 37 B (-85%)
curl -X GET "https://realcomposer.com/v1/workspace/hide-contentful-preview-token-nextjs" \ -H "Authorization: Bearer rc_live_client_token"
Developer Questions & Troubleshooting
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.
Sources & Definitions Used in This Answer
Deploy this Next.js / React + Contentful Recipe
Clone this pre-configured pipeline into your RealComposer workspace in one click. We pre-populate upstream URLs, headers, and mapping rules.