Phân phát nội dung động và dịch vụ vi mô lưu trữ bằng tính năng Lưu trữ Firebase
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Firebase Hosting tích hợp với các lựa chọn điện toán không máy chủ, bao gồm cả Cloud Functions for Firebase và Cloud Run. Khi dùng Firebase Hosting với các lựa chọn này, bạn có thể lưu trữ các vi dịch vụ bằng cách chuyển hướng các yêu cầu HTTPS để kích hoạt các hàm và ứng dụng được chứa trong vùng chứa để chạy trong một môi trường được quản lý và bảo mật.
Cloud Functions for Firebase: Bạn viết và triển khai một hàm, đây là mã phụ trợ phản hồi một trình kích hoạt cụ thể. Sau đó, bằng cách sử dụng Firebase Hosting, bạn có thể chuyển các yêu cầu HTTPS để kích hoạt chức năng chạy.
Cloud Run: Bạn viết và triển khai một ứng dụng được đóng gói trong một hình ảnh vùng chứa. Sau đó, bằng cách sử dụng Firebase Hosting, bạn có thể chuyển các yêu cầu HTTPS để kích hoạt ứng dụng được chứa trong vùng chứa chạy.
Trường hợp sử dụng
Bạn có thể sử dụng các lựa chọn điện toán không máy chủ với Firebase Hosting bằng cách nào?
Phân phát nội dung động – Ngoài việc phân phát nội dung tĩnh trên trang web Hosting, bạn có thể phân phát các phản hồi được tạo động từ một hàm hoặc ứng dụng được chứa hoá đang thực hiện logic phía máy chủ.
Ví dụ: bạn có thể trỏ một mẫu URL (chẳng hạn như /blog/<blog-post-id>) đến một hàm sử dụng tham số mã bài đăng trên blog của URL để truy xuất nội dung một cách linh hoạt từ cơ sở dữ liệu của bạn.
Tạo API REST – Bạn có thể tạo một API vi dịch vụ bằng cách sử dụng các hàm.
Ví dụ: các hàm có thể xử lý chức năng đăng nhập cho trang web của bạn. Mặc dù trang web của bạn được lưu trữ tại /, nhưng mọi yêu cầu đến /api đều được chuyển hướng đến API vi dịch vụ của bạn. Để xem ví dụ, hãy tham khảo mẫu nguồn mở này.
Ví dụ: nếu một hàm chỉ tạo nội dung mới theo định kỳ, bạn có thể tăng tốc ứng dụng bằng cách lưu nội dung đã tạo vào bộ nhớ đệm trong ít nhất một khoảng thời gian ngắn. Bạn cũng có thể giảm chi phí thực thi vì nội dung được phân phát từ CDN thay vì thông qua một hàm được kích hoạt hoặc ứng dụng được chứa trong vùng chứa.
Kết xuất trước các ứng dụng một trang – Bạn có thể cải thiện SEO và tối ưu hoá việc chia sẻ trên nhiều mạng xã hội bằng cách tạo các thẻ meta động. Để tìm hiểu thêm, hãy xem video này hoặc xem mẫu nguồn mở này.
Chọn một lựa chọn không máy chủ
Mặc dù cả Cloud Functions for Firebase và Cloud Run đều tích hợp với Firebase Hosting và cung cấp một môi trường phi máy chủ được quản lý hoàn toàn, có khả năng tự động mở rộng quy mô và bảo mật, nhưng bạn có thể tận dụng hai lựa chọn này cho các trường hợp sử dụng khác nhau và mức độ cấu hình tuỳ chỉnh mong muốn.
Khi sử dụng một trong hai lựa chọn không máy chủ, tốt nhất là bạn nên đặt lựa chọn đó cùng với các máy chủ cho Firebase Hosting bằng cách triển khai ở một trong các khu vực sau:
us-west1
us-central1
us-east1
europe-west1
asia-east1
Bảng sau đây mô tả một số điểm cần cân nhắc cơ bản khi sử dụng Cloud Functions for Firebase so với Cloud Run. Để xem danh sách đầy đủ về hạn mức, giới hạn và chỉ số, hãy tham khảo tài liệu chi tiết của từng sản phẩm (Cloud Functions for Firebase hoặc Cloud Run).
Mức độ cân nhắc
Cloud Functions for Firebase
Cloud Run
Thiết lập
CLI Firebase kết hợp nhiều tác vụ thành các lệnh đơn lẻ, từ việc khởi chạy đến việc tạo và triển khai.
Các vùng chứa cung cấp nhiều lựa chọn tuỳ chỉnh hơn, vì vậy, các tác vụ thiết lập, tạo và triển khai bao gồm các bước riêng biệt.
Môi trường thời gian chạy
Yêu cầu Node.js, nhưng bạn có thể chỉ định phiên bản Node.js cần sử dụng.
Khi tạo vùng chứa, bạn chỉ định môi trường thời gian chạy.
Hỗ trợ ngôn ngữ và khung
JavaScript và TypeScript
Các khung web, chẳng hạn như Express.js, đều được hỗ trợ.
[null,null,["Cập nhật lần gần đây nhất: 2025-08-08 UTC."],[],[],null,["\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nFirebase Hosting integrates with serverless computing options, including\nCloud Functions for Firebase and Cloud Run. Using Firebase Hosting with\nthese options, you can host microservices by directing HTTPS requests to trigger\nyour functions and containerized apps to run in a managed, secure environment.\n\n[**Cloud Functions for Firebase**](/docs/hosting/functions): You write and deploy a\nfunction, which is backend code that responds to a specific trigger. Then, using\nFirebase Hosting, you can direct HTTPS requests to trigger your function\nto run.\n\n[**Cloud Run**](/docs/hosting/cloud-run): You write and deploy an\napplication packaged in a container image. Then, using Firebase Hosting, you\ncan direct HTTPS requests to trigger your containerized app to run.\n| **Note:** If you're using Angular or Next.js to develop a full-stack Web app with server-side rendering or AI features, try out the preview of [App Hosting](/docs/app-hosting). App Hosting is a unified product for managing static and dynamic content together.\n\nUse cases\n\nHow can you use serverless computing options with Firebase Hosting?\n\n- **Serve dynamic content** --- In addition to serving static content on\n your Hosting site, you can serve dynamically generated responses from a\n function or containerized app that is performing server-side logic.\n\n For example, you can point a URL pattern (like `/blog/\u003cblog-post-id\u003e`) to a\n function that uses the URL's blog post ID parameter to retrieve content\n dynamically from your database.\n- **Build REST APIs** --- You can create a microservice API using functions.\n\n For instance, functions can handle the sign-in functionality for your\n website. While your website is hosted at `/`, any request to `/api` is\n redirected to your microservice API. For an example, check out\n [this open-source sample](//github.com/firebase/functions-samples/tree/Node-8/authenticated-json-api).\n- **Cache dynamic content** --- You can\n [configure caching](/docs/hosting/manage-cache) of your dynamic content on\n a global CDN.\n\n For example, if a function generates new content only periodically, you\n can speed up your app by caching the generated content for at least a short\n period of time. You can also potentially reduce execution costs because the\n content is served from the CDN rather than via a triggered function or\n containerized app.\n- **Prerender your single-page apps** --- You can improve SEO and optimize\n sharing across various social networks by creating dynamic `meta` tags. To\n learn more, watch this [video](//www.youtube.com/watch?v=82tZAPMHfT4)\n or check out\n [this open-source sample](//github.com/firebase/functions-samples/tree/Node-8/isomorphic-react-app).\n\nChoosing a serverless option\n\nWhile both [**Cloud Functions for Firebase**](/docs/hosting/functions) and\n[**Cloud Run**](/docs/hosting/cloud-run) integrate with\nFirebase Hosting and offer a fully managed, autoscaling, and secure\nserverless environment, the two options can be leveraged for different use cases\nand desired level of customized configuration.\n\nWhen using either serverless option, it is best to co-locate it with the servers\nfor Firebase Hosting by deploying in one of the following regions:\n\n- `us-west1`\n- `us-central1`\n- `us-east1`\n- `europe-west1`\n- `asia-east1`\n\nThe following table describes some basic considerations for using\nCloud Functions for Firebase versus Cloud Run. For a full listing of\nquotas, limits, and metrics, refer to each product's detailed documentation\n([Cloud Functions for Firebase](/docs/functions/quotas) or\n[Cloud Run](//cloud.google.com/run/quotas)).\n\n| **Consideration** | **Cloud Functions for Firebase** | **Cloud Run** |\n|-------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| **Setup** | The Firebase CLI bundles multiple tasks into single commands, from initializing to building and deploying. | Containers offer more customizable options, so setup, build, and deployment tasks involve discrete steps. |\n| **Runtime environment** | Requires Node.js, but you can specify [which version](/docs/functions/manage-functions#set_runtime_options) of Node.js to use. | When [building your container](/docs/hosting/cloud-run#containerize), you specify the runtime environment. |\n| **Language and frameworks support** | JavaScript and TypeScript Web frameworks, like Express.js, are supported. | Any language that Dockerfiles support, including [Go, Node.js, Python, Java, and others](//cloud.google.com/run/docs/quickstarts/build-and-deploy#writing) Web frameworks for each language are supported. |\n| **Timeout for Hosting request** | 60 seconds (see Note below) | 60 seconds (see Note below) |\n| **Concurrency** | 1 request per function instance (no concurrency per instance) | Up to 1,000 concurrent requests per container instance |\n| **Billing** | [Cloud Functions usage](/pricing) Free usage quota, but a [Cloud Billing account](//cloud.google.com/billing/docs/how-to/manage-billing-account) is required. See the [Firebase FAQ](/support/faq#functions-pricing). | [Cloud Run usage](//cloud.google.com/run/pricing) + [Container Registry storage](//cloud.google.com/container-registry/) Free usage quota, but a [Cloud Billing account](//cloud.google.com/billing/docs/how-to/manage-billing-account) is required |\n| **Billing** | | If your Firebase project is on the Spark pricing plan, and you associate your Firebase project with a Cloud Billing account, then your Firebase project is automatically upgraded to the Blaze pricing plan. Review the [Firebase pricing page](/pricing) for a comparison of the Spark and Blaze plans. ||\n\n| **Note:** Even though Cloud Functions and Cloud Run have longer request timeouts, Firebase Hosting is subject to a 60-second request timeout. If your app requires more than 60 seconds to run, you'll receive an HTTPS status code `504` (request timeout)."]]