{
  "slug": "cache-firebase-firestore-reads-edge",
  "title": "Cache firebase firestore reads at edge",
  "metaDescription": "Cache firebase firestore reads at edge. Shield Firebase with 180s GET edge cache; cache key uses query params on cache-firebase-firestore-reads-edge.",
  "headline": "Repeat mobile reads without origin round trips",
  "category": "caching",
  "categoryLabel": "Edge Caching & Rate Limits",
  "urgency": 8,
  "clientPlatform": "React Native",
  "upstreamServices": [
    "Firebase"
  ],
  "searchIntentQuery": "cache firebase firestore reads at edge",
  "directAnswerSummary": "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.",
  "problem": {
    "title": "Uncached Firebase spikes cost and latency",
    "explanation": "Every React Native visitor can trigger a fresh Firebase 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": "Add vault secrets for this pipeline",
      "description": "Store FIREBASE_ACCESS_TOKEN in the vault. React Native never embeds the raw secret, only a revocable rc_live_ client token."
    },
    {
      "stepNumber": 2,
      "title": "Configure upstream steps and output mapping",
      "description": "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."
    },
    {
      "stepNumber": 3,
      "title": "Call from React Native",
      "description": "Allowed origins on the token should list every React Native host that calls this URL.",
      "codeSnippet": "const res = await fetch('https://realcomposer.com/v1/workspace/cache-firebase-firestore-reads-edge', {\n  headers: { Authorization: 'Bearer rc_live_your_token' }\n});"
    }
  ],
  "pipelineConfig": {
    "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"
        }
      ]
    }
  },
  "sampleBloatedInput": {
    "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"
      }
    }
  },
  "sampleSlimmedOutput": {
    "docName": "sample",
    "fields": "sample"
  },
  "curlSnippet": "curl -X GET \"https://realcomposer.com/v1/workspace/cache-firebase-firestore-reads-edge\" \\\n  -H \"Authorization: Bearer rc_live_client_token\"",
  "faqs": [
    {
      "question": "Which Firebase credential belongs in the vault for this pipeline?",
      "answer": "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}}."
    },
    {
      "question": "What cache key does RealComposer use?",
      "answer": "cache-firebase-firestore-reads-edge POST steps bypass caches.default even if cacheTtlSeconds is set in the dashboard."
    },
    {
      "question": "How do I rotate upstream credentials?",
      "answer": "Upstream steps in cache-firebase-firestore-reads-edge honor timeoutMs and optional fallback JSON without leaking secrets."
    }
  ],
  "relatedRecipeSlugs": [
    "sub-10ms-api-for-mobile-apps",
    "cache-rest-api-without-redis"
  ],
  "updatedAt": "2026-11-20",
  "links": {
    "page": "https://realcomposer.com/recipes/cache-firebase-firestore-reads-edge",
    "markdown": "https://realcomposer.com/recipes/cache-firebase-firestore-reads-edge.md",
    "json": "https://realcomposer.com/recipes/cache-firebase-firestore-reads-edge.json",
    "install": "https://realcomposer.com/dashboard/pipelines/new?recipe=cache-firebase-firestore-reads-edge&via=agent"
  }
}