# Migrate this Cloudflare Pages project to Workers Implement the migration to Workers with Static Assets in this repository. Preserve the application's production-facing contract unless the user approves a change: public routes, redirects, methods, status codes, meaningful content, and security and cache behavior. Do not treat local emulator output or legacy adapter internals as the contract. Stop after local verification: do not deploy, upload a Preview, change dashboard settings or domains, or disable Pages without permission. Use the project's package manager and lockfile. Keep a supported, compatible Wrangler version; install or upgrade it only if the selected migration needs it. Avoid unrelated framework or dependency upgrades. Use the current [Pages migration guide](https://developers.cloudflare.com/workers/static-assets/migration-guides/migrate-from-pages/) and the [Workers framework guides](https://developers.cloudflare.com/workers/framework-guides/) where applicable. ## 1. Establish the Pages baseline - Find the project root, build and deploy commands, Wrangler config, framework and adapter versions, and Pages build output directory. Inspect CI and any available Pages settings for differences between local, production, and preview builds. Do not assume settings absent from the repository are unused. - Run the existing build before changing it. Inspect the built output for `_worker.js` (file or directory), `_routes.json` (checked in or generated), `_headers`, `_redirects`, and `404.html`. Inspect `functions/` and its middleware. In Pages advanced mode, `_worker.js` takes precedence over `functions/`; do not start running previously inactive Functions. - Run `wrangler pages dev` locally if possible. Record representative responses: a static asset, a deep link and missing URL, SSR or API endpoints, and any redirects or custom headers. Use the app's routes, tests, configuration, and any available hosted observations to distinguish public behavior from local or adapter artifacts. If an asset and a function share a URL, record which wins. For file-based Functions without a custom `_routes.json`, also inspect the routes Pages generates for deployment: the Functions build supports `--output-routes-path` alongside `--outdir`. Local Pages dev alone does not establish the deployed invocation scope. If you cannot collect a baseline, state what you could not verify and proceed without guessing. ## 2. Choose the migration path from the build output Do not run `wrangler setup` against an existing Pages config with `pages_build_output_dir`. Without a detected framework, a `functions/` directory also blocks setup. Keep the Pages config as a rollback copy; use the framework's published Workers instructions or migrate file-based Functions manually. - **Static or client-rendered:** If there is no server code, use an assets-only Worker. A client-side framework or Vite alone does not require a Worker entrypoint, adapter, or `@cloudflare/vite-plugin`. Point `assets.directory` at the built output and omit `main` and `assets.binding`. - **Framework SSR or APIs:** Follow the Workers path for the installed framework and adapter versions. Keep the adapter if it already supports Workers. Replace a Pages-only adapter or preset where needed, such as `@cloudflare/next-on-pages` or Nitro's `cloudflare_pages`. For Next.js, check the [current Workers framework guide](https://developers.cloudflare.com/workers/framework-guides/web-apps/nextjs/) and compatibility with the app's routes and runtime features before selecting a replacement. Do not treat wrapping a deprecated Pages adapter's output as a completed migration. Use the supported adapter's entrypoint. Add a route-scoped shim only for a demonstrated public behavior gap. Do not recreate legacy diagnostic headers, generated file aliases, or framework internals based only on local observations. Check that Pages build variables such as `CF_PAGES` do not force the adapter back to Pages. Rebuild and inspect the actual Worker entrypoint and assets directory. If the framework generates Wrangler config (for example, Nitro's `cloudflare_module` with `deployConfig` enabled), inspect `.wrangler/deploy/config.json` and its target. Edit the source config or adapter options, not the generated config, and do not pass `--config` to bypass the generated config unintentionally. - **Advanced mode (`_worker.js`):** Use the built file or the entry file inside the directory (typically `_worker.js/index.js`) as `main`, unless the framework configures it. Keep sibling chunks and modules available to Wrangler. If the handler calls `env.ASSETS`, set `assets.binding` to `ASSETS` unless the adapter already does. Keep the entire server bundle outside `assets.directory`, or exclude its file or directory from assets on every build. - **File-based Pages Functions:** Preserve the existing handlers and middleware. To keep them during the migration, run `wrangler pages functions build --outdir ./.wrangler/pages-functions` in the project build, outside `assets.directory`, and point `main` at the generated `index.js` or a wrapper around it. Pass the original `--compatibility-date` and any `--compatibility-flags` explicitly; this standalone command does not read Workers config. If the Functions use `assets:` imports, also pass `--build-output-directory` pointing to the final asset output. Configure `assets.binding` as `ASSETS` for the compiled Worker's asset fallback. Do not rewrite the application just to change deployment targets. ## 3. Preserve configuration and routing - Replace `pages_build_output_dir` with Workers Static Assets configuration, or let the framework emit it. Keep one authoritative source Wrangler config, in the existing format when possible. Preserve the existing compatibility date, flags, and applicable settings. If no date was set, check the Pages runtime settings before choosing one for server code. An assets-only project with no previous date can use today's date. - Choose fallback behavior from the baseline, not the framework name. For a Pages-style `index.html` fallback, use `assets.not_found_handling: "single-page-application"`, even on a multi-page site that relied on it. For a static custom 404, use `"404-page"`. Do not let asset fallbacks replace framework SSR or API responses. Check status, `Location`, and query strings for canonical HTML redirects (such as `.html` and trailing-slash URLs). Workers may return `307` where Pages returned `308`. If the status must stay the same, use an explicit `_redirects` rule only if it also preserves method behavior: it can redirect `POST` where Pages returned `405`. If the redirect must be limited to `GET` and `HEAD`, handle it in a route-scoped Worker and let other methods follow the normal handler or asset path. - Preserve when Pages invoked the handler before assets, including collisions without middleware or a user-authored `_routes.json`. Use the effective Pages include and exclude rules, including generated rules where applicable. Use `assets.run_worker_first: true` if all requests must run the Worker first. In an array, a matching `!` rule sends requests directly to assets, even on a miss. An asset miss matching neither a positive nor a negative rule can still reach the Worker. Pages `/api/*` also matches `/api`; add the bare path explicitly for includes and excludes when translating a splat. Test both trailing-slash forms. If an array cannot preserve strict handler scope, check excludes before includes in a Worker wrapper. Call the legacy handler only for included paths and return `env.ASSETS.fetch(request)` otherwise. Use `run_worker_first: true` with that guard if necessary, but note that it also invokes the Worker for asset requests. Verify asset hits and misses. - Keep `_headers` and `_redirects` in the final assets output. Compare headers even if the project has no `_headers`: Pages adds defaults such as `Access-Control-Allow-Origin`, `Referrer-Policy`, and `X-Content-Type-Options` when serving assets. Restore needed defaults only on confirmed static asset or prerendered responses, including intentional overrides and removals. Identify those responses from the asset handler, route source, or build output, not from status or `Content-Type` alone. When restoring static defaults in a wrapper, pass dynamic responses (including errors) and any response marked `private` or `no-store` through unchanged. Do not remove `Vary` values or replace a restricted origin with `*`. Set required headers on Worker-generated responses explicitly, including redirects you recreate. - Verify that generated server code, its source maps, and files Pages previously ignored (such as `_routes.json`, `functions`, `.wrangler`, `node_modules`, `.git`, or `.DS_Store`) are not public. Remove them from the asset output or generate root `.assetsignore` entries. Make exclusions in build inputs or in a build step, not by hand in generated output. - Carry over each runtime binding and variable with the same name and intended resource. Map Pages production and preview separately. For branch previews, set an explicit top-level `previews` block in the Wrangler config (empty if no separate settings are needed), not `env.preview`. Follow [Worker Previews configuration](https://developers.cloudflare.com/workers/previews/configuration/) for settings that stay at the top level, and point Preview bindings at test resources rather than production resources. Record dashboard-only settings that still need confirmation. Keep build-time variables separate from runtime ones. Report only secret names, never values; do not copy or commit secrets. ## 4. Wire up and verify the migration - Update scripts and CI to use the selected framework's Workers commands or `wrangler dev` and `wrangler deploy` (and `wrangler preview` for branch previews where applicable). Keep framework-specific commands that own the build. Test the actual dev and deploy workflows with generated output absent. If a workflow needs a separate build, make that prerequisite explicit in the script, CI, or documented command sequence rather than relying on output left over from the Pages build. - Run the actual deployment build from clean output; a generic project build might not produce the Worker. Run `wrangler deploy --dry-run` when Wrangler owns deployment, using the same effective config and build sequence. For other deployment tools, use their documented local or dry-run verification without deploying. Confirm the entrypoint, asset directory, and bindings; a dry-run does not confirm routing or dashboard settings. - For translated splats, test the bare path (for example, `/api`), `/api/`, and a nested path on both includes and excludes. Test asset hits and misses, including when `not_found_handling` serves a fallback instead of invoking the Worker. - If zone Cache Rules cached Pages-generated responses on the old custom domain, use Workers Cache and appropriate `Cache-Control` headers for eligible Worker-generated responses. Cache Rules can still apply to the Worker's `fetch()` subrequests. Verify generated-response caching in an approved test deployment or report it as unverified; do not assume local responses prove edge caching. - Run Workers locally and compare it against the Pages baseline. Check status, redirects, meaningful body content, and relevant headers such as `Content-Type`, `Cache-Control`, and custom headers. Where applicable, test a static hit and miss, a deep link, an SSR response or API method, a function/asset collision, and assets and misses inside and outside includes and excludes. Compare a plain request and a browser-style `Sec-Fetch-Mode: navigate` request on routes that may be handled by either assets or the Worker. If you add a response shim, verify that an out-of-scope response still keeps its own cache and security headers. Request generated server paths and verify their contents are not served, even if a fallback returns `200`. - Summarize what changed, which behavior you verified, and any mismatch or unverified assumption. List manual next steps for dashboard-only settings, secret setup, Worker Previews or Workers Builds, hostname cutover, and eventual Pages cleanup. A `pages.dev` hostname does not transfer to Workers; plan for any callers that still use it. Leave the current Pages deployment running.