[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-05。"],[],[],null,["\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nFirebase Hosting uses a powerful global CDN to make your site as fast as\npossible.\n\nAny requested ***static content* is automatically cached on the CDN** . If you\nredeploy your site's content, Firebase Hosting automatically clears all your\ncached content across the CDN until the next request.\n\nHowever, because Cloud Functions and Cloud Run services generate\ncontent dynamically, the content for a given URL can vary based on such things\nas user input or the user's identity. To account for this, requests that are\nhandled by backend code do *not* cache on the CDN by default.\n\nYou can, though, **configure caching behavior for dynamic content**. For\nexample, if a function generates new content only periodically, you can speed up\nyour app by caching the generated content for at least a short period of time.\n\nYou can similarly configure caching behavior to potentially reduce function\nexecution costs because the content is served from the CDN rather than from a\ntriggered function. Read more about optimizing function execution and services\nin the [Cloud Functions](/docs/functions/tips) and\n[Cloud Run](//cloud.google.com/run/docs/tips)\ndocumentation.\n\nThe exception is requests that return 404 errors. The CDN caches your\nservice's 404 response to a nonexistent URL for 10 minutes, so that subsequent\nrequests for that URL are served out of the CDN. If you change your service\nso that content now exists at this URL, the CDN continues serving any cached\n404s for 10 minutes (at most), and then serves content from that URL normally.\n\nIf a 404 response already contains caching headers set by your\nCloud Functions or Cloud Run service, they override the\ndefault of 10 minutes and fully determine the caching behavior of\nthe CDN.\n\nLearn more about caching behavior in Google's\n[web developer documentation](//developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching).\n\nSet Cache-Control\n\nThe main tool that you use to manage cache for dynamic content is the\n`Cache-Control` header. By configuring this header, you can communicate both to\nthe browser and the CDN how long your content can be cached. In your function,\nyou set `Cache-Control` like so: \n\n res.set('Cache-Control', 'public, max-age=300, s-maxage=600');\n\nIn this example header, the directives do three things:\n\n- `public` --- Marks the cache as `public`. This means that both the browser *and*\n the intermediate servers (meaning the CDN for Firebase Hosting) can cache\n the content.\n\n | **Note:** By default, `Cache-Control` is set to `private`. This means that if you don't explicitly set `Cache-Control` to `public`, only the browser of the requesting user is allowed to cache the content.\n- `max-age` --- Tells the browser and the CDN how many seconds that they can cache\n the content. When the set time expires, the browser and the CDN must\n revalidate the content with the origin server. In the example header, we're\n allowing the browser and the CDN to cache the content for five minutes (see\n `s-maxage` below for specific controls for CDN caching).\n\n- `s-maxage` --- Overrides the `max-age` directive for the CDN-caching only; tells\n the CDN how many seconds that it can cache the content. When the set time\n expires, the CDN must revalidate the content with the origin server. In the\n example header, we're overriding the setting for `max-age` *for the CDN only*\n and allowing the CDN to cache the content for ten minutes.\n\nFor `max-age` and `s-maxage`, set their values to the longest amount of time\nthat you're comfortable with users receiving stale content. If a page changes\nevery few seconds, use a small time value. However, other types of content can\nbe safely cached for hours, days, or even months.\n\nIf you want to prevent caching entirely (for example, to always serve the\nfreshest version of static content), you can configure this in\n`firebase.json` using the [`headers`](//firebase.google.com/docs/hosting/full-config#headers)\nsetting: \n\n \"hosting\": {\n // ...\n\n // Disables caching for the /posts route\n \"headers\": [ {\n // Change source to match your dynamically-rendered routes\n \"source\": \"/posts/**\",\n \"headers\": [ {\n \"key\": \"Cache-Control\",\n \"value\": \"no-cache, no-store\"\n } ]\n } ]\n }\n\nYou can learn more about the `Cache-Control` header on the\n[Mozilla Developer Network](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control)\nand in Google's\n[web developer documentation](https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching#cache-control).\n\nWhen is cached content served?\n\nThe browser and the CDN cache your content based on:\n\n- The hostname\n- The path\n- The query string\n- The content of the request headers specified in the [`Vary` header](#vary_headers)\n\nVary headers\n\nThe\n[`Vary` header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Vary)\ndetermines which request headers should be used to provide an appropriate\nresponse (whether the cached content is valid or if the content should be\nrevalidated with the origin server).\n\nFirebase Hosting automatically sets an appropriate `Vary` header on your\nresponse for common situations. **Most of the time, you don't need to worry\nabout the `Vary` header.** However, in some advanced use cases, you might have\nother headers that you need to affect the cache. When that's the case,\nyou can set the `Vary` header on your response. For example: \n\n res.set('Vary', 'Accept-Encoding, X-My-Custom-Header');\n\nIn this case, the value of the `Vary` header is: \n\n vary: X-My-Custom-Header, x-fh-requested-host, accept-encoding, cookie, authorization\n\nWith these settings, two otherwise identical requests with different\n`X-My-Custom-Header` headers are cached separately. Note that Hosting adds\n`Cookie` and `Authorization` to the `Vary` header by default when a request is\nmade for dynamic content. This ensures that any session or cookie authorization\nheader you use is made part of the cache key, which prevents accidental leaks\nof content.\n\nAlso note:\n\n- Only `GET` and `HEAD` requests can be cached. HTTPS requests using other\n methods are never cached.\n\n- Be careful when adding settings to the `Vary` header. The more settings\n that you add, the less likely it is that the CDN can serve cached content.\n Also remember that `Vary` is based on **request** headers, not **response**\n headers.\n\nUsing cookies\n\nWhen using Firebase Hosting together with Cloud Functions or\nCloud Run, cookies are generally stripped from incoming requests. This\nis necessary to allow for efficient CDN [cache behavior](#set_cache-control).\nOnly the specially-named `__session` cookie is permitted to pass through to the\nexecution of your app.\n\nWhen present, the `__session` cookie is automatically made a part of the cache\nkey, meaning that it's impossible for two users with different cookies to\nreceive the other's cached response. Only use the `__session` cookie if your\napp serves different content depending on user authorization."]]