{
  "slug": "contentful-algolia-search-cache-edge",
  "title": "Contentful and algolia search cache edge",
  "metaDescription": "Contentful and algolia search cache edge. Shield Contentful and Algolia with no POST edge cache; cache key uses query params on.",
  "headline": "Site search with dual upstream cache",
  "category": "caching",
  "categoryLabel": "Edge Caching & Rate Limits",
  "urgency": 8,
  "clientPlatform": "Next.js / React",
  "upstreamServices": [
    "Contentful",
    "Algolia"
  ],
  "searchIntentQuery": "contentful and algolia search cache edge",
  "directAnswerSummary": "Cache composed search hits for popular queries. For \"contentful and algolia search cache edge\", store CONTENTFUL_ACCESS_TOKEN, ALGOLIA_API_KEY in Secret Vault, call https://cdn.contentful.com/spaces/{{query.spaceId}}/entries from a Worker, and let Next.js / React hit only https://realcomposer.com/v1/workspace/contentful-algolia-search-cache-edge with an rc_live_ client token. POST upstream calls are not stored in caches.default; clients must send JSON bodies and headers forward unchanged.",
  "problem": {
    "title": "Uncached Contentful and Algolia spikes cost and latency",
    "explanation": "Every Next.js / React visitor can trigger a fresh Contentful and Algolia call. Bursts exhaust per-minute quotas even when data changes slowly.",
    "risks": [
      "Thundering herds after cache expiry on the origin",
      "Analytics skewed by client-side retry storms",
      "Support tickets blaming \"API downtime\" during spikes"
    ]
  },
  "solutionSteps": [
    {
      "stepNumber": 1,
      "title": "Bind vault keys to the Worker",
      "description": "Store CONTENTFUL_ACCESS_TOKEN, ALGOLIA_API_KEY in the vault. Next.js / React never embeds the raw secret, only a revocable rc_live_ client token."
    },
    {
      "stepNumber": 2,
      "title": "Target the real upstream URL",
      "description": "Configure headersTemplate to inject vault secrets, target https://cdn.contentful.com/spaces/{{query.spaceId}}/entries, and Add output mapping fields that mirror the JSON contract your screen already expects."
    },
    {
      "stepNumber": 3,
      "title": "Call from Next.js / React",
      "description": "Call https://realcomposer.com/v1/workspace/contentful-algolia-search-cache-edge with a restricted rc_live_ client token. Next.js / React never sends upstream secrets.",
      "codeSnippet": "// app/api route or client component\nconst res = await fetch('https://realcomposer.com/v1/workspace/contentful-algolia-search-cache-edge', {\n  cache: 'no-store',\n  headers: { Authorization: 'Bearer rc_live_your_token' }\n});"
    }
  ],
  "pipelineConfig": {
    "name": "Contentful and algolia search cache edge",
    "method": "POST",
    "cacheTtlSeconds": 0,
    "authMode": "client_token",
    "steps": [
      {
        "stepKey": "contentful",
        "upstreamUrlTemplate": "https://cdn.contentful.com/spaces/{{query.spaceId}}/entries",
        "httpMethod": "GET",
        "headersTemplate": {
          "Authorization": "Bearer {{secrets.CONTENTFUL_ACCESS_TOKEN}}"
        },
        "timeoutMs": 3000
      },
      {
        "stepKey": "algolia",
        "upstreamUrlTemplate": "https://{{secrets.ALGOLIA_APP_ID}}-dsn.algolia.net/1/indexes/{{query.index}}/query",
        "httpMethod": "POST",
        "headersTemplate": {
          "X-Algolia-API-Key": "{{secrets.ALGOLIA_API_KEY}}",
          "X-Algolia-Application-Id": "{{secrets.ALGOLIA_APP_ID}}",
          "Content-Type": "application/json"
        },
        "timeoutMs": 10000
      }
    ],
    "outputMapping": {
      "version": 1,
      "fields": [
        {
          "targetPath": "contentful.entryId",
          "sourcePath": "items.0.sys.id",
          "type": "string",
          "sourceStep": "contentful"
        },
        {
          "targetPath": "contentful.title",
          "sourcePath": "items.0.fields.title",
          "type": "string",
          "sourceStep": "contentful"
        },
        {
          "targetPath": "algolia.hitCount",
          "sourcePath": "nbHits",
          "type": "number",
          "sourceStep": "algolia"
        },
        {
          "targetPath": "algolia.firstHit",
          "sourcePath": "hits.0.objectID",
          "type": "string",
          "sourceStep": "algolia"
        }
      ]
    }
  },
  "sampleBloatedInput": {
    "requestId": "req_contentf",
    "upstreamLatencyMs": 142,
    "audit": {
      "region": "edge",
      "trace": "contentful-algolia-search-cache-edge"
    },
    "payload": {
      "id": "sample_id",
      "status": "active",
      "nested": {
        "tags": [
          "demo"
        ],
        "internal": true
      },
      "unused_blob": {
        "foo": "bar",
        "bar": "baz"
      }
    }
  },
  "sampleSlimmedOutput": {
    "contentful_entryId": "sample",
    "contentful_title": "sample",
    "algolia_hitCount": "sample",
    "algolia_firstHit": "sample"
  },
  "curlSnippet": "curl -X POST \"https://realcomposer.com/v1/workspace/contentful-algolia-search-cache-edge\" \\\n  -H \"Authorization: Bearer rc_live_client_token\" \\\n  -H \"Content-Type: application/json\" \\\n  -d '{\"model\":\"example\"}'",
  "faqs": [
    {
      "question": "Can Next.js / React call Contentful without holding the production key?",
      "answer": "Store CONTENTFUL_ACCESS_TOKEN, ALGOLIA_API_KEY for contentful-algolia-search-cache-edge. Next.js / React never sees decrypted values."
    },
    {
      "question": "Does POST traffic get cached?",
      "answer": "contentful-algolia-search-cache-edge forwards client JSON to POST upstreams unchanged; secrets interpolate only in URLs and header templates."
    },
    {
      "question": "How do I rotate upstream credentials?",
      "answer": "Rotate vault secrets for contentful-algolia-search-cache-edge anytime; clients keep the same rc_live_ token."
    }
  ],
  "relatedRecipeSlugs": [
    "aggregate-contentful-with-algolia-search",
    "cache-shopify-storefront-api"
  ],
  "updatedAt": "2026-11-20",
  "links": {
    "page": "https://realcomposer.com/recipes/contentful-algolia-search-cache-edge",
    "markdown": "https://realcomposer.com/recipes/contentful-algolia-search-cache-edge.md",
    "json": "https://realcomposer.com/recipes/contentful-algolia-search-cache-edge.json",
    "install": "https://realcomposer.com/dashboard/pipelines/new?recipe=contentful-algolia-search-cache-edge&via=agent"
  }
}