# Contentful and algolia search cache edge

> 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.

- Canonical page: https://realcomposer.com/recipes/contentful-algolia-search-cache-edge
- Client platform: Next.js / React
- Upstream services: Contentful, Algolia
- Category: Edge Caching & Rate Limits
- JSON version: https://realcomposer.com/recipes/contentful-algolia-search-cache-edge.json
- Install in RealComposer (free plan, pre-fills this pipeline): https://realcomposer.com/dashboard/pipelines/new?recipe=contentful-algolia-search-cache-edge&via=agent

## Problem: Uncached Contentful and Algolia spikes cost and latency

Every Next.js / React visitor can trigger a fresh Contentful and Algolia call. Bursts exhaust per-minute quotas even when data changes slowly.

- Thundering herds after cache expiry on the origin
- Analytics skewed by client-side retry storms
- Support tickets blaming "API downtime" during spikes

## Solution steps

1. **Bind vault keys to the Worker**: 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.

2. **Target the real upstream URL**: 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.

3. **Call from Next.js / React**: 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.

```
// app/api route or client component
const res = await fetch('https://realcomposer.com/v1/workspace/contentful-algolia-search-cache-edge', {
  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": "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"
      }
    ]
  }
}
```

## Call the pipeline

```bash
curl -X POST "https://realcomposer.com/v1/workspace/contentful-algolia-search-cache-edge" \
  -H "Authorization: Bearer rc_live_client_token" \
  -H "Content-Type: application/json" \
  -d '{"model":"example"}'
```

## Payload before and after

Raw upstream response:

```json
{
  "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"
    }
  }
}
```

Slimmed RealComposer response:

```json
{
  "contentful_entryId": "sample",
  "contentful_title": "sample",
  "algolia_hitCount": "sample",
  "algolia_firstHit": "sample"
}
```

## FAQ

### Can Next.js / React call Contentful without holding the production key?

Store CONTENTFUL_ACCESS_TOKEN, ALGOLIA_API_KEY for contentful-algolia-search-cache-edge. Next.js / React never sees decrypted values.

### Does POST traffic get cached?

contentful-algolia-search-cache-edge forwards client JSON to POST upstreams unchanged; secrets interpolate only in URLs and header templates.

### How do I rotate upstream credentials?

Rotate vault secrets for contentful-algolia-search-cache-edge anytime; clients keep the same rc_live_ token.
