Migrating từ Workers Sites to Pages Migrating from Workers Sites to Pages
Hướng dẫn chi tiết đồng bộ từ docs Cloudflare — mỗi section có backlink tới đúng vị trí trên trang gốc. Detailed guide synced from Cloudflare docs — each section links to the matching anchor on the official page.
← Danh mục ← CatalogGiải thích nhanh Quick context
Thuộc Developer Platform — thực hành triển khai code, API và dữ liệu trên edge/serverless. Tutorial «Migrating từ Workers Sites to Pages» giúp bạn làm quen luồng triển khai thật — phù hợp đọc trước khi mở tài liệu gốc tiếng Anh. Đọc phần tóm tắt và lưu ý trước — sau đó mở docs gốc để copy lệnh và cấu hình chi tiết.
Learn how to migrate from Workers Sites to Cloudflare Pages.
Lưu ý trước khi làm Notes before you start
- Đây là bản tóm tắt trên Orange Cloud Learning Hub — không thay thế tài liệu chính thức. This is a summary on Orange Cloud Learning Hub — it does not replace the official documentation.
- Luôn mở liên kết «Tài liệu gốc» bên dưới khi cần lệnh CLI, snippet code và ảnh minh họa đầy đủ. Open the Official docs link below for CLI commands, code snippets, and full screenshots.
- Cần tài khoản Cloudflare, Wrangler CLI và (thường) hoàn thành hướng dẫn Get started của Workers/Pages. Requires a Cloudflare account, Wrangler CLI, and (typically) completing the Workers/Pages Get started guide.
- Docs Cloudflare cập nhật thường xuyên — đối chiếu ngày «Rà soát lần cuối» trên trang gốc khi triển khai production. Cloudflare docs change frequently — verify the Last reviewed date on the official page before production use.
Tài liệu gốc — rà soát lần cuối: almost 6 years ago Official docs — last reviewed: almost 6 years ago
Tổng quan Overview
Trong tutorial này, bạn sẽ migrate an existing Cloudflare Workers Sites application to Cloudflare Pages.
Read the "Overview" section below — open the official docs link for full screenshots and configuration tabs.
In this tutorial, you will learn how to migrate an existing Cloudflare Workers Sites application to Cloudflare Pages.
As a prerequisite, you should have a Cloudflare Workers Sites project, created with Wrangler ↗.
Cloudflare Pages provides built-in defaults for every aspect of serving your site. You can port custom behavior in your Worker — such as custom caching logic — to your Cloudflare Pages project using Functions. This enables an easy-to-use, file-based routing system. You can also migrate your custom headers and redirects to Pages.
You may already have a reasonably complex Worker and/or it would be tedious to splice it up into Pages' file-based routing system. For these cases, Pages offers developers the ability to define a _worker.js file in the output directory of your Pages project.
When using a worker.js file, the entire /functions directory is ignored - this includes its routing and middleware characteristics. Instead, the worker.js file is deployed as is and must be written using the Module Worker syntax.
By migrating to Cloudflare Pages, you will be able to access features like preview deployments and automatic branch deploys with no extra configuration needed.
Liên kết liên quan (docs Cloudflare) Related links (Cloudflare docs)
Gỡ unnecessary code Remove unnecessary code
Phần «Gỡ unnecessary code» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
Read the "Remove unnecessary code" section below — open the official docs link for full screenshots and configuration tabs.
Workers Sites projects consist of the following pieces:
- An application built with a static site tool or a static collection of HTML, CSS and JavaScript files.
- If using a static site tool, a build directory (called
bucketin the Wrangler configuration file) where the static project builds your HTML, CSS, and JavaScript files. - A Worker application for serving that build directory. For most projects, this is likely to be the
workers-sitedirectory.
When moving to Cloudflare Pages, remove the Workers application and any associated Wrangler configuration files or build output. Instead, note and record your build command (if you have one), and the bucket field, or build directory, from the Wrangler file in your project's directory.
Liên kết liên quan (docs Cloudflare) Related links (Cloudflare docs)
Di chuyển headers và redirects Migrate headers and redirects
Phần «Di chuyển headers và redirects» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
Read the "Migrate headers and redirects" section below — open the official docs link for full screenshots and configuration tabs.
You can migrate your redirects to Pages, by creating a _redirects file in your output directory. Pages currently offers limited support for advanced redirects. More support will be added in the future. For a list of support types, refer to the Redirects documentation.
A project is limited to 2,000 static redirects and 100 dynamic redirects, for a combined total of 2,100 redirects. Each redirect declaration has a 1,000-character limit. Malformed definitions are ignored. If there are multiple redirects for the same source path, the topmost redirect is applied.
Make sure that static redirects are before dynamic redirects in your _redirects file.
In addition to a _redirects file, Cloudflare also offers Bulk Redirects, which handles redirects that surpasses the 2,100 redirect rules limit set by Pages.
Your custom headers can also be moved into a headers file in your output directory. It is important to note that custom headers defined in the headers file are not currently applied to responses from Functions, even if the Function route matches the URL pattern. To learn more about handling headers, refer to Headers.
Liên kết liên quan (docs Cloudflare) Related links (Cloudflare docs)
Kết nối vào git provider Connect to your git provider
Phần «Kết nối vào git provider» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
Read the "Connect to your git provider" section below — open the official docs link for full screenshots and configuration tabs.
After you have recorded your build command and build directory in a separate location, remove everything else from your application, and push the new version of your project up to your git provider. Follow the Get started guide to add your project to Cloudflare Pages, using the build command and build directory that you saved earlier.
If you choose to use a custom domain for your Pages project, you can set it to the same custom domain as your currently deployed Workers application. Follow the steps for adding a custom domain to your Pages project.
Before you deploy, you will need to delete your old Workers routes to start sending requests to Cloudflare Pages.
Liên kết liên quan (docs Cloudflare) Related links (Cloudflare docs)
Using Direct Upload Using Direct Upload
Phần «Using Direct Upload» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
Read the "Using Direct Upload" section below — open the official docs link for full screenshots and configuration tabs.
If your Workers site has its custom build settings, you can bring your prebuilt assets to Pages with Direct Upload. In addition, you can serve your website's assets right to the Cloudflare global network by either using the Wrangler CLI or the drag and drop option.
These options allow you to create and name a new project from the CLI or dashboard. After your project deployment is complete, you can set the custom domain by following the adding a custom domain steps to your Pages project.
Liên kết liên quan (docs Cloudflare) Related links (Cloudflare docs)
Cleaning up your old ứng dụng và assigning domain Cleaning up your old application and assigning the domain
Phần «Cleaning up your old ứng dụng và assigning domain» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
Read the "Cleaning up your old application and assigning the domain" section below — open the official docs link for full screenshots and configuration tabs.
After you have deployed your Pages application, to delete your Worker:
- In the Cloudflare dashboard, go to the Workers & Pages page.
- Select your Worker.
- Go to Manage \> Delete Worker.
With your Workers application removed, requests will go to your Pages application. You have successfully migrated your Workers Sites project to Cloudflare Pages by completing this guide.
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"item":{"@id":"/directory/","name":"Directory"}},{"@type":"ListItem","position":2,"item":{"@id":"/pages/","name":"Pages"}},{"@type":"ListItem","position":3,"item":{"@id":"/pages/migrations/","name":"Migration guides"}},{"@type":"ListItem","position":4,"item":{"@id":"/pages/migrations/migrating-from-workers/","name":"Migrating from Workers Sites to Pages"}}]} Xem bản đầy đủ trên developers.cloudflare.com (ảnh, tab cấu hình). View the full guide on developers.cloudflare.com (images, config tabs).
Tài liệu gốc ↗ Official docs ↗