## TL;DR

Cloudflare Pages compiles everything under the functions directory into a single _worker.js, and that file must stay under 1 MiB after compression. Large dependencies bundled into functions push it over. Find the biggest imports with a bundle analyzer, move heavy work to the client or to a separate Worker, and redeploy.

## The query

```text
wrangler pages deploy failed: _worker.js exceeds the 1 MiB size limit
```

## Use this when

- `wrangler pages deploy` fails naming the _worker.js size limit.
- The functions directory grew recently (new SDK, new dependency).
- The static site deploys fine but the Functions part is rejected.

## Not for

- 'wrangler publish failed: script too large' on plain Workers (different limit path).
- 'static assets total size exceeds 25 MiB' (asset limit, separate fix).
- Build failures inside the functions directory (fix the build first).

## Steps

### Step 1: Measure what is actually in the bundle

```bash
npx wrangler pages deploy ./dist --dry-run
```

Expected result: the dry run reports the compiled _worker.js size. Compare it to 1 MiB to confirm size is the only blocker.

### Step 2: Find the biggest imports

Build the functions bundle locally and inspect it, or temporarily comment out imports and re-run the dry run to bisect.

Expected result: one or two dependencies (often an SDK, a date library, or a crypto polyfill) account for most of the size. Everything you import into functions/ ships to the edge.

### Step 3: Trim the heavy imports

Replace full SDK imports with scoped ones, swap heavy libraries for lighter equivalents, and delete unused imports. Dynamic imports do not help here: Pages compiles functions into one file regardless.

Expected result: the dry-run size drops. Aim well under the limit, not exactly at it, because dependency updates regrow bundles silently.

### Step 4: Move what does not need the edge

Shift heavy rendering, PDF generation, or large SDK calls to client-side code or to a dedicated Worker invoked over HTTP. Pages Functions should be thin glue.

Expected result: functions/ contains only routing and light logic. The bundle stays small even as the site grows.

### Step 5: Redeploy

```bash
npx wrangler pages deploy ./dist
```

Expected result: the deploy passes size validation and publishes. Watch the size on every deploy; add the dry-run size check to CI.

## Why it happens

Pages Functions are not separate serverless functions at deploy time; they are compiled into one worker script. Developers treat functions/ like a Node server where bundle size is free, but the edge ships the whole bundle to every PoP. One `import` of a kitchen-sink SDK can add hundreds of kilobytes.

## Edge cases

- The limit applies to the compressed bundle. A 1.4 MiB uncompressed file that compresses to 900 KiB is fine; a 1.1 MiB file that compresses poorly is not.
- Source maps and dev-only imports count if they end up in the bundle. Strip them from production builds.
- Middleware in functions/_middleware.js is part of the same bundle. Heavy auth libraries there hit the same limit.
- If trimming is impossible, split the heaviest route into a standalone Worker and call it from the Pages Function over HTTP.

## Provenance

Resolved from the public thread: https://vectle.com/posts/pst_vkNfcD1c7xw2Wv5EputhSw
