{
  "slug": "hide-contentful-preview-token-nextjs",
  "title": "Hide contentful preview token in nextjs app",
  "metaDescription": "Hide contentful preview token in nextjs app. Keep Contentful keys in RealComposer Vault; Next.js / React uses rc_live_ tokens only.",
  "headline": "Preview mode without public preview secrets",
  "category": "security",
  "categoryLabel": "Security & Secret Vault",
  "urgency": 9,
  "clientPlatform": "Next.js / React",
  "upstreamServices": [
    "Contentful"
  ],
  "searchIntentQuery": "hide contentful preview token in nextjs app",
  "directAnswerSummary": "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.",
  "problem": {
    "title": "Why Next.js / React leaks Contentful credentials",
    "explanation": "Attackers routinely extract Contentful secrets from Next.js / React builds. Direct browser calls also expose internal error payloads with hostnames.",
    "risks": [
      "Production Contentful keys copied from Next.js / React bundles",
      "Compliance violations after credential leaks",
      "Attackers replaying vault-grade scopes from a laptop"
    ]
  },
  "solutionSteps": [
    {
      "stepNumber": 1,
      "title": "Bind vault keys to the Worker",
      "description": "Store CONTENTFUL_PREVIEW_TOKEN in the vault. Next.js / React never embeds the raw secret, only a revocable rc_live_ client token."
    },
    {
      "stepNumber": 2,
      "title": "Configure upstream steps and output mapping",
      "description": "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)."
    },
    {
      "stepNumber": 3,
      "title": "Call from Next.js / React",
      "description": "Allowed origins on the token should list every Next.js / React host that calls this URL.",
      "codeSnippet": "// app/api route or client component\nconst res = await fetch('https://realcomposer.com/v1/workspace/hide-contentful-preview-token-nextjs', {\n  cache: 'no-store',\n  headers: { Authorization: 'Bearer rc_live_your_token' }\n});"
    }
  ],
  "pipelineConfig": {
    "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"
        }
      ]
    }
  },
  "sampleBloatedInput": {
    "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"
      }
    }
  },
  "sampleSlimmedOutput": {
    "entryId": "sample",
    "title": "sample"
  },
  "curlSnippet": "curl -X GET \"https://realcomposer.com/v1/workspace/hide-contentful-preview-token-nextjs\" \\\n  -H \"Authorization: Bearer rc_live_client_token\"",
  "faqs": [
    {
      "question": "Does Next.js / React need CORS changes after adding RealComposer?",
      "answer": "Store CONTENTFUL_PREVIEW_TOKEN for hide-contentful-preview-token-nextjs. Next.js / React never sees decrypted values."
    },
    {
      "question": "When does edge caching apply to this recipe?",
      "answer": "hide-contentful-preview-token-nextjs forwards client JSON to POST upstreams unchanged; secrets interpolate only in URLs and header templates."
    },
    {
      "question": "What happens if an upstream step times out?",
      "answer": "Allowed origins on the hide-contentful-preview-token-nextjs client token restrict which domains may call the edge URL."
    }
  ],
  "relatedRecipeSlugs": [
    "protect-supabase-service-role-key-nextjs",
    "hide-api-key-framer"
  ],
  "updatedAt": "2026-11-20",
  "links": {
    "page": "https://realcomposer.com/recipes/hide-contentful-preview-token-nextjs",
    "markdown": "https://realcomposer.com/recipes/hide-contentful-preview-token-nextjs.md",
    "json": "https://realcomposer.com/recipes/hide-contentful-preview-token-nextjs.json",
    "install": "https://realcomposer.com/dashboard/pipelines/new?recipe=hide-contentful-preview-token-nextjs&via=agent"
  }
}