Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
41a5874
Adds more details to overview, basic workflow, and How it works section
korinne Jan 29, 2025
fbba83f
Update src/content/docs/workers/static-assets/index.mdx
korinne Jan 29, 2025
7b760ad
Update src/content/docs/workers/static-assets/index.mdx
korinne Jan 29, 2025
7dd6bbe
Update src/content/docs/workers/static-assets/index.mdx
korinne Jan 29, 2025
acf2961
Update src/content/docs/workers/static-assets/index.mdx
korinne Jan 29, 2025
122c2c2
Update src/content/docs/workers/static-assets/index.mdx
korinne Jan 29, 2025
d32e32d
Update src/content/docs/workers/static-assets/index.mdx
korinne Jan 29, 2025
c337adf
Updates overview, adds try it out section
korinne Jan 31, 2025
51eaaf6
Fixes typos, adds some bolding
korinne Jan 31, 2025
c2b7fca
adds line highlighting for code samples
korinne Feb 1, 2025
10c00c8
Update src/content/docs/workers/frameworks/index.mdx
Oxyjun Feb 5, 2025
d52902c
Update src/content/docs/workers/frameworks/index.mdx
Oxyjun Feb 5, 2025
a809133
Update src/content/docs/workers/static-assets/billing-and-limitations…
Oxyjun Feb 5, 2025
feeea0f
Update src/content/docs/workers/static-assets/index.mdx
Oxyjun Feb 5, 2025
414f5f4
Update src/content/docs/workers/static-assets/index.mdx
Oxyjun Feb 5, 2025
78a0bc7
Update src/content/docs/workers/static-assets/index.mdx
Oxyjun Feb 5, 2025
c41babf
Update src/content/docs/workers/static-assets/index.mdx
Oxyjun Feb 5, 2025
e8dc03e
Update src/content/docs/workers/static-assets/index.mdx
Oxyjun Feb 5, 2025
4e79672
Update src/content/docs/workers/static-assets/index.mdx
Oxyjun Feb 5, 2025
b730fd1
Update src/content/docs/workers/static-assets/index.mdx
Oxyjun Feb 5, 2025
40f1967
Update src/content/docs/workers/static-assets/routing.mdx
Oxyjun Feb 5, 2025
e8d96d9
Update src/content/docs/workers/static-assets/index.mdx
Oxyjun Feb 5, 2025
879227c
Update src/content/docs/workers/static-assets/index.mdx
Oxyjun Feb 5, 2025
3aefe38
Update src/content/docs/workers/static-assets/index.mdx
Oxyjun Feb 5, 2025
e693569
Update src/content/docs/workers/static-assets/index.mdx
Oxyjun Feb 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/content/docs/workers/static-assets/binding.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pcx_content_type: concept
title: Configuration and Bindings
sidebar:
order: 7
order: 8
head: []
description: Details on how to configure Workers static assets and its binding.
---
Expand Down
152 changes: 133 additions & 19 deletions src/content/docs/workers/static-assets/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ description: Create full-stack applications deployed to Cloudflare Workers.
---

import {
Aside,
Badge,
Details,
Description,
InlineBadge,
DirectoryListing,
Expand All @@ -21,35 +23,147 @@ import {
LinkButton,
Stream,
Flex,
WranglerConfig,
Steps,
} from "~/components";

<Description>
Create fast, scalable front-end applications deployed to Cloudflare Workers.
</Description>
Cloudflare Workers simplify hosting and serving static like (such as HTML, CSS, images, and other resources) by combining a globally distributed CDN with lightweight, serverless backend logic.

With Workers, you can:

- **Host static sites** or **Single Page Applications (SPAs)** across Cloudflare's network.

- **Add backend logic** (such as APIs or custom middleware) alongside your assets without needing a dedicated server.

- Serve content to users with **low latency** using **built-in caching** for faster load times for users worldwide.

- **Build full-stack applications** using Cloudflare's storage solutions, such as [R2](/r2/) for object storage, [KV](/kv/) for distributed key-value storage, [D1](/d1/) for relational databases, and [Durable Objects](/durable-objects/) for stateful, consistent storage and coordination. You can also connect to existing databases using [Hyperdrive](/hyperdrive/)

---

## Basic Workflow

---

From a high-level, here's how you can get started with deploying a simple static site on Workers:

<Steps>
1. Build your static assets.
```sh
npm run build
```
<Details header="Don't have an existing project?">
You can quickly scaffold a project using [Vite](https://vite.dev/guide/#scaffolding-your-first-vite-project):
```sh
npm create vite@latest
```
And follow the prompts! For first-timers, just select `vanilla` as your template.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good but can there be example in npm create cloudflare@latest that gets you fully setup?

</Details>
2. Add and configure a Wrangler configuration file (`wrangler.toml` or `wrangler.json`)

<WranglerConfig>
```toml
name = "my-static-site"
compatibility_date = "2025-01-01"
[assets]
directory = "./dist"
```
</WranglerConfig>

In this configuration file, you define the **directory** where Cloudflare can find your built assets that need to be uploaded. To do this, we've added an `[assets]` block, and specified the `directory` as `dist/`.

3. Deploy with Wrangler

```sh
npx wrangler deploy
```

And let Cloudflare Workers handle asset uploading, routing, and caching automatically!

</Steps>

---

## What you can build
## How it works

When you deploy your app with Wrangler, it deploys both your Worker script and your static assets in a single operation. This deployment operates as a tightly integrated "unit" running across Cloudflare's network, combining static file hosting, custom logic, and global caching.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
When you deploy your app with Wrangler, it deploys both your Worker script and your static assets in a single operation. This deployment operates as a tightly integrated "unit" running across Cloudflare's network, combining static file hosting, custom logic, and global caching.
When you deploy a Worker to Cloudflare that has static assets, it deploys both your Worker's code and your static assets as one integrated "unit" running across Cloudflare's network, combining static file hosting, custom logic, and caching.


Within this deployment, the Worker has two primary responsibilies:

1. **Asset handling**
2. **Running custom logic**

### Asset handling
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would suggest cutting or tightening paragraphs below


Static asset hosting on Workers allows you to serve files such as HTML, JavaScript, CSS, images, and other resources directly from Cloudflare's global network. This setup eliminates the need for a traditional web server, simplifying deployment and optimizing performance.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Static asset hosting on Workers allows you to serve files such as HTML, JavaScript, CSS, images, and other resources directly from Cloudflare's global network. This setup eliminates the need for a traditional web server, simplifying deployment and optimizing performance.

Think repeating?


The **assets directory** specified in your Wrangler configuration file is central to this design. During deployment, Wrangler automatically uploads the files from this directory to Cloudflare’s infrastructure. Once deployed, requests for these assets are routed efficiently to locations closest to your users.

This approach ensures that static files like `index.html`, JavaScript bundles, stylesheets, and images are served with minimal latency. Workers efficiently handle file resolution, meaning they serve files as-is from the defined directory, or return a fallback response (such as an error or default page) when a requested file is not found.

<Aside type="tip">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tip suggests this is nice-to-have optional, would expect somewhere to enumerate — what are the configuration options and what do they mean?

You can control how assets are served, how unmatched routes are handled, as
well as override routing behavior in your Wrangler configuration file.

For example, if you have a Single Page Application and want to fallback to
`index.html` for unmatched routes, you can define this in your [Wrangler configuration](/workers/static-assets/routing/#alternate-configuration-options):

<WranglerConfig>
```toml
name = "my-static-site"
compatibility_date = "2025-01-01"
[assets]
directory = "./dist"
not_found_handling = "single-page-application"

```
</WranglerConfig>

</Aside>

### Running custom logic

One of the defining features of Cloudflare Workers is the ability to execute custom backend logic alongside your static assets. This logic can be used for handling requests, modifying responses, or implementing custom functionality for your application.

For example, you can implement the following directly in your Worker:

- **Dynamic routing:** Inspect incoming requests and route them based on their paths or headers. For example, you can route requests to `/api` to custom API logic, while static files are automatically served from your specified directory.

- **API endpoints:** Define endpoints to handle user requests, fetch data from external APIs, or interact with databases.

- **Response modifications:** Modify responses dynamically, such as adding headers, compressing payloads, or injecting data into static content.

- **Stateful logic:** For scenarios requiring consistent state across requests, you can use [Durable Objects](/durable-objects/). Durable objects allow you to maintain state for use-cases such as real-time collaboration, session storage, global counters, and more.

<Aside type="tip">
You can change the default routing behavior (which prioritizes serving assets ahead of running Worker code) so that your [Worker code runs before an asset is served](/workers/static-assets/routing/#invoking-worker-script-ahead-of-assets/).

For example, if you want to protect an asset behind authentication, you can override the routing behavior in your Wrangler configuration file:

### Static Websites
<WranglerConfig>
```toml
name = "my-static-site"
compatibility_date = "2025-01-01"
[assets]
directory = "./dist"
binding = "ASSETS"
experimental_serve_directly = false
```
</WranglerConfig>

Deploy static sites like blogs, portfolios, or documentation pages. Serve HTML, CSS, JavaScript, and media files directly from Cloudflare's network for low-latency access worldwide.
</Aside>

<LinkButton href="/workers/static-assets/get-started/#deploy-a-static-site">
Deploy a static site
</LinkButton>
### Caching static assets

### Full-stack Applications
Cloudflare provides automatic caching for static assets across its network, ensuring fast delivery to users worldwide. When a static asset is requested, it is automatically cached for future requests.

Build dynamic and interactive server-side rendered (SSR) applications on Cloudflare Workers.
#### Cache behavior

You can combine asset hosting with Cloudflare's data and storage products such as [Workers KV](/kv/), [Durable Objects](/durable-objects/), and [R2 Storage](/r2/) to build full-stack applications that serve both front-end and back-end logic in a single Worker.
- **First Request:** When an asset is requested for the first time, it is fetched from storage and cached at the nearest location.

<LinkButton href="/workers/static-assets/get-started/#deploy-a-full-stack-application">
Deploy a full-stack application
</LinkButton>
- **Subsequent Requests:** Future requests for the same asset are served directly from the cache, significantly reducing latency.

### Supported frameworks
## Supported frameworks

The following Frameworks are currently supported on Cloudflare Workers:

Expand All @@ -61,19 +175,19 @@ Static Assets for Workers is currently in open beta. If you're looking for a fra

:::

### Pricing
## Pricing

Requests to a project with static assets can either return static assets or invoke the Worker script, depending on if the request [matches a static asset or not](/workers/static-assets/routing/).

**Requests to static assets are free and unlimited**. Requests to the Worker script (e.g. in the case of SSR content) are billed according to Workers pricing. Refer to [pricing](/workers/platform/pricing/#example-2) for an example.

There is no additional cost for storing Assets.

### Troubleshooting
## Troubleshooting

- `assets.bucket is a required field` — if you see this error, you need to update Wrangler to at least `3.78.10` or later. `bucket` is not a required field.

### Limitations
## Limitations

The following limitations apply for Workers with static assets:

Expand Down
3 changes: 2 additions & 1 deletion src/content/docs/workers/static-assets/routing.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pcx_content_type: concept
title: Routing
sidebar:
order: 7
order: 9
head: []
description: How Workers assets' routing and its configuration works.
---
Expand Down Expand Up @@ -322,3 +322,4 @@ In this example, requests to `example.com/blog/` will serve the `index.html` fil

If you have a file outside the configured path, it will not be served. For example, if you have a `home.html` file in the root of your asset directory, it will not be served when requesting `example.com/blog/home`.
However, if needed, these files can still be manually fetched over [the binding](/workers/static-assets/binding/#binding).
```
Loading