# Cache firebase firestore reads at edge

> Edge cache Firestore REST document GETs keyed by document path. For "cache firebase firestore reads at edge", store FIREBASE_ACCESS_TOKEN in Secret Vault, call https://firestore.googleapis.com/v1/projects/{{query.projectId}}/databases/(default)/documents/{{query.collection}}/{{query.docId}} from a Worker, and let React Native hit only https://realcomposer.com/v1/workspace/cache-firebase-firestore-reads-edge with an rc_live_ client token. Enable 180s GET cache only when query params identify the resource.

- Canonical page: https://realcomposer.com/recipes/cache-firebase-firestore-reads-edge
- Client platform: React Native
- Upstream services: Firebase
- Category: Edge Caching & Rate Limits
- JSON version: https://realcomposer.com/recipes/cache-firebase-firestore-reads-edge.json
- Install in RealComposer (free plan, pre-fills this pipeline): https://realcomposer.com/dashboard/pipelines/new?recipe=cache-firebase-firestore-reads-edge&via=agent

## Problem: Uncached Firebase spikes cost and latency

Every React Native visitor can trigger a fresh Firebase 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. **Add vault secrets for this pipeline**: Store FIREBASE_ACCESS_TOKEN in the vault. React Native never embeds the raw secret, only a revocable rc_live_ client token.

2. **Configure upstream steps and output mapping**: Configure headersTemplate to inject vault secrets, target https://firestore.googleapis.com/v1/projects/{{query.projectId}}/databases/(default)/documents/{{query.collection}}/{{query.docId}}, and Add output mapping fields that mirror the JSON contract your screen already expects.

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

```
const res = await fetch('https://realcomposer.com/v1/workspace/cache-firebase-firestore-reads-edge', {
  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": "Cache firebase firestore reads at edge",
  "method": "GET",
  "cacheTtlSeconds": 180,
  "authMode": "client_token",
  "steps": [
    {
      "stepKey": "firebase",
      "upstreamUrlTemplate": "https://firestore.googleapis.com/v1/projects/{{query.projectId}}/databases/(default)/documents/{{query.collection}}/{{query.docId}}",
      "httpMethod": "GET",
      "headersTemplate": {
        "Authorization": "Bearer {{secrets.FIREBASE_ACCESS_TOKEN}}"
      },
      "timeoutMs": 3000
    }
  ],
  "outputMapping": {
    "version": 1,
    "fields": [
      {
        "targetPath": "docName",
        "sourcePath": "name",
        "type": "string",
        "sourceStep": "firebase"
      },
      {
        "targetPath": "fields",
        "sourcePath": "fields",
        "type": "object",
        "sourceStep": "firebase"
      }
    ]
  }
}
```

## Call the pipeline

```bash
curl -X GET "https://realcomposer.com/v1/workspace/cache-firebase-firestore-reads-edge" \
  -H "Authorization: Bearer rc_live_client_token"
```

## Payload before and after

Raw upstream response:

```json
{
  "requestId": "req_cache-fi",
  "upstreamLatencyMs": 142,
  "audit": {
    "region": "edge",
    "trace": "cache-firebase-firestore-reads-edge"
  },
  "payload": {
    "id": "sample_id",
    "status": "active",
    "nested": {
      "tags": [
        "demo"
      ],
      "internal": true
    },
    "unused_blob": {
      "foo": "bar",
      "bar": "baz"
    }
  }
}
```

Slimmed RealComposer response:

```json
{
  "docName": "sample",
  "fields": "sample"
}
```

## FAQ

### Which Firebase credential belongs in the vault for this pipeline?

Yes for cache-firebase-firestore-reads-edge. Ship only an rc_live_ client token in React Native; RealComposer injects FIREBASE_ACCESS_TOKEN on https://firestore.googleapis.com/v1/projects/{{query.projectId}}/databases/(default)/documents/{{query.collection}}/{{query.docId}}.

### What cache key does RealComposer use?

cache-firebase-firestore-reads-edge POST steps bypass caches.default even if cacheTtlSeconds is set in the dashboard.

### How do I rotate upstream credentials?

Upstream steps in cache-firebase-firestore-reads-edge honor timeoutMs and optional fallback JSON without leaking secrets.
