Skip to content

Commit 41a5874

Browse files
committed
Adds more details to overview, basic workflow, and How it works section
1 parent e359b27 commit 41a5874

File tree

3 files changed

+136
-21
lines changed

3 files changed

+136
-21
lines changed

src/content/docs/workers/static-assets/binding.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
pcx_content_type: concept
33
title: Configuration and Bindings
44
sidebar:
5-
order: 7
5+
order: 8
66
head: []
77
description: Details on how to configure Workers static assets and its binding.
88
---

src/content/docs/workers/static-assets/index.mdx

Lines changed: 133 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ description: Create full-stack applications deployed to Cloudflare Workers.
1010
---
1111

1212
import {
13+
Aside,
1314
Badge,
15+
Details,
1416
Description,
1517
InlineBadge,
1618
DirectoryListing,
@@ -21,35 +23,147 @@ import {
2123
LinkButton,
2224
Stream,
2325
Flex,
26+
WranglerConfig,
27+
Steps,
2428
} from "~/components";
2529

26-
<Description>
27-
Create fast, scalable front-end applications deployed to Cloudflare Workers.
28-
</Description>
30+
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.
31+
32+
With Workers, you can:
33+
34+
- **Host static sites** or **Single Page Applications (SPAs)** across Cloudflare's network.
35+
36+
- **Add backend logic** (such as APIs or custom middleware) alongside your assets without needing a dedicated server.
37+
38+
- Serve content to users with **low latency** using **built-in caching** for faster load times for users worldwide.
39+
40+
- **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/)
41+
42+
---
43+
44+
## Basic Workflow
45+
46+
---
47+
48+
From a high-level, here's how you can get started with deploying a simple static site on Workers:
49+
50+
<Steps>
51+
1. Build your static assets.
52+
```sh
53+
npm run build
54+
```
55+
<Details header="Don't have an existing project?">
56+
You can quickly scaffold a project using [Vite](https://vite.dev/guide/#scaffolding-your-first-vite-project):
57+
```sh
58+
npm create vite@latest
59+
```
60+
And follow the prompts! For first-timers, just select `vanilla` as your template.
61+
</Details>
62+
2. Add and configure a Wrangler configuration file (`wrangler.toml` or `wrangler.json`)
63+
64+
<WranglerConfig>
65+
```toml
66+
name = "my-static-site"
67+
compatibility_date = "2025-01-01"
68+
[assets]
69+
directory = "./dist"
70+
```
71+
</WranglerConfig>
72+
73+
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/`.
74+
75+
3. Deploy with Wrangler
76+
77+
```sh
78+
npx wrangler deploy
79+
```
80+
81+
And let Cloudflare Workers handle asset uploading, routing, and caching automatically!
82+
83+
</Steps>
2984

3085
---
3186

32-
## What you can build
87+
## How it works
88+
89+
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.
90+
91+
Within this deployment, the Worker has two primary responsibilies:
92+
93+
1. **Asset handling**
94+
2. **Running custom logic**
95+
96+
### Asset handling
97+
98+
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.
99+
100+
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.
101+
102+
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.
103+
104+
<Aside type="tip">
105+
You can control how assets are served, how unmatched routes are handled, as
106+
well as override routing behavior in your Wrangler configuration file.
107+
108+
For example, if you have a Single Page Application and want to fallback to
109+
`index.html` for unmatched routes, you can define this in your [Wrangler configuration](/workers/static-assets/routing/#alternate-configuration-options):
110+
111+
<WranglerConfig>
112+
```toml
113+
name = "my-static-site"
114+
compatibility_date = "2025-01-01"
115+
[assets]
116+
directory = "./dist"
117+
not_found_handling = "single-page-application"
118+
119+
```
120+
</WranglerConfig>
121+
122+
</Aside>
123+
124+
### Running custom logic
125+
126+
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.
127+
128+
For example, you can implement the following directly in your Worker:
129+
130+
- **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.
131+
132+
- **API endpoints:** Define endpoints to handle user requests, fetch data from external APIs, or interact with databases.
133+
134+
- **Response modifications:** Modify responses dynamically, such as adding headers, compressing payloads, or injecting data into static content.
135+
136+
- **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.
137+
138+
<Aside type="tip">
139+
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/).
140+
141+
For example, if you want to protect an asset behind authentication, you can override the routing behavior in your Wrangler configuration file:
33142

34-
### Static Websites
143+
<WranglerConfig>
144+
```toml
145+
name = "my-static-site"
146+
compatibility_date = "2025-01-01"
147+
[assets]
148+
directory = "./dist"
149+
binding = "ASSETS"
150+
experimental_serve_directly = false
151+
```
152+
</WranglerConfig>
35153

36-
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.
154+
</Aside>
37155

38-
<LinkButton href="/workers/static-assets/get-started/#deploy-a-static-site">
39-
Deploy a static site
40-
</LinkButton>
156+
### Caching static assets
41157

42-
### Full-stack Applications
158+
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.
43159

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

46-
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.
162+
- **First Request:** When an asset is requested for the first time, it is fetched from storage and cached at the nearest location.
47163

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

52-
### Supported frameworks
166+
## Supported frameworks
53167

54168
The following Frameworks are currently supported on Cloudflare Workers:
55169

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

62176
:::
63177

64-
### Pricing
178+
## Pricing
65179

66180
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/).
67181

68182
**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.
69183

70184
There is no additional cost for storing Assets.
71185

72-
### Troubleshooting
186+
## Troubleshooting
73187

74188
- `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.
75189

76-
### Limitations
190+
## Limitations
77191

78192
The following limitations apply for Workers with static assets:
79193

src/content/docs/workers/static-assets/routing.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
pcx_content_type: concept
33
title: Routing
44
sidebar:
5-
order: 7
5+
order: 9
66
head: []
77
description: How Workers assets' routing and its configuration works.
88
---
@@ -322,3 +322,4 @@ In this example, requests to `example.com/blog/` will serve the `index.html` fil
322322

323323
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`.
324324
However, if needed, these files can still be manually fetched over [the binding](/workers/static-assets/binding/#binding).
325+
```

0 commit comments

Comments
 (0)