Cloudflare
How-Tos
Static assets

Static assets (public folder)

The static assets of Next located in the public folder (opens in a new tab) are served using Workers Static Assets (opens in a new tab).

Workers Static Assets can intercept requests to the application (run_worker_first=false (opens in a new tab) which is the default). This is the most cost efficient option as asset requests will not be billed in that case.

Another option is to run the Worker first (run_worker_first=true) to offer more flexibility at the expense of a higher cost.

Note that run_worker_first could also be set to a list of patterns - this is a great option if you need more flexibility for only a subset of the static assets.

run_worker_first=false

When run_worker_first is set to false, requests are intercepted before reaching the worker and are not billed:

wrangler.jsonc
{
  "name": "my-app",
  // ...
  "assets": {
    "directory": ".open-next/assets",
    "binding": "ASSETS",
    // Optional as false is the default value
    "run_worker_first": false,
  },
  // ...
}

This is the most cost efficient option to use when you do not need to serve assets behind the middleware or Next rewrites and headers from the Next config (opens in a new tab).

When run_worker_first=false you can still configure headers (opens in a new tab) and redirects (opens in a new tab) via Worker Static Assets.

run_worker_first=true

When run_worker_first is set to true, all the requests will reach the Worker and be billed:

wrangler.jsonc
{
  "name": "my-app",
  // ...
  "assets": {
    "directory": ".open-next/assets",
    "binding": "ASSETS",
    "run_worker_first": true,
    // Serve public files at their exact path only, like Next.js
    "html_handling": "none",
    // "404-page" is also allowed
    "not_found_handling": "none",
  },
  // ...
}

The Open Next asset resolver will be used to retrieve the assets from the Worker.

Since @opennextjs/cloudflare 1.20.16, public files are only served at their exact path when the Worker runs first, like Next.js. The build fails when run_worker_first is enabled (true or a list with at least one non-negated pattern) unless html_handling is set to "none" and not_found_handling is set to "none" or "404-page". Other values would serve the files at other paths (i.e. /about for public/about.html), which a middleware matching the exact path of the file would not cover.

When run_worker_first=true, assets are served behind the middleware and Next rewrites and headers from the Next config (opens in a new tab). The headers (opens in a new tab) and redirects (opens in a new tab) configured for the Worker Static Assets do not apply in this case.

run_worker_first=true should be used if you plan to use skew protection.