Skip to content

Commit 8c2cb84

Browse files
committed
Reference run_worker_first in tutorial
1 parent 501562e commit 8c2cb84

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

src/content/docs/workers/vite-plugin/tutorial.mdx

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,8 @@ The `main` field specifies the entry file for your Worker code.
147147
### Add your API Worker
148148

149149
```ts title="worker/index.ts"
150-
interface Env {
151-
ASSETS: Fetcher;
152-
}
153-
154150
export default {
155-
fetch(request, env) {
151+
fetch(request) {
156152
const url = new URL(request.url);
157153

158154
if (url.pathname.startsWith("/api/")) {
@@ -163,7 +159,7 @@ export default {
163159

164160
return new Response(null, { status: 404 });
165161
},
166-
} satisfies ExportedHandler<Env>;
162+
} satisfies ExportedHandler;
167163
```
168164

169165
The Worker above will be invoked for any non-navigation request that does not match a static asset.
@@ -172,6 +168,21 @@ It returns a JSON response if the `pathname` starts with `/api/` and otherwise r
172168
:::note
173169
For top-level navigation requests, browsers send a `Sec-Fetch-Mode: navigate` header.
174170
If this is present and the URL does not match a static asset, the `not_found_handling` behavior will be invoked rather than the Worker.
171+
This implicit routing is the default behavior.
172+
173+
If you would instead like to define the routes that invoke your Worker explicitly, you can provide an array of route patterns to [`run_worker_first`](/workers/static-assets/binding/#run_worker_first).
174+
This opts out of interpreting the `Sec-Fetch-Mode` header.
175+
176+
<WranglerConfig>
177+
178+
```toml
179+
name = "cloudflare-vite-tutorial"
180+
compatibility_date = "2025-04-03"
181+
assets = { not_found_handling = "single-page-application", run_worker_first = ["/api/*"] }
182+
main = "./worker/index.ts"
183+
```
184+
185+
</WranglerConfig>
175186
:::
176187

177188
### Call the API from the client

0 commit comments

Comments
 (0)