-
Notifications
You must be signed in to change notification settings - Fork 10k
Document how to run User Worker on only specific paths #23218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -127,3 +127,44 @@ export default class extends WorkerEntrypoint<Env> { | |
| } | ||
| ``` | ||
| </TypeScriptExample> | ||
|
|
||
| ### Run Worker only on selective paths | ||
|
|
||
| You can configure a User Worker to only be invoked on specific paths, by using an array of positive route patterns, followed by a negative wildcard pattern. This configuration can be very helpful when hosting a static website with a Worker as a simple backend service: | ||
|
|
||
| For more information you can take a look at the [routing reference diagram](/workers/static-assets/routing/static-site-generation/#reference). | ||
|
|
||
| <WranglerConfig> | ||
|
|
||
| ```json | ||
| { | ||
| "name": "my-worker", | ||
| "compatibility_date": "$today", | ||
| "main": "./worker/index.ts", | ||
| "assets": { | ||
| "directory": "./dist/", | ||
| "not_found_handling": "single-page-application", | ||
| "binding": "ASSETS", | ||
| "run_worker_first": ["/api/*", "!**"] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll need to double-check, but I had thought that: (a) rules need to start with either @matthewdavidrodgers , can you confirm?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes to all three points; this is more effectively written as
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to illustrate a situation where a negative path is necessary, it must also match a broader positive rule to exempt those paths from being directed to the worker. |
||
| } | ||
| } | ||
| ``` | ||
|
|
||
| </WranglerConfig> | ||
|
|
||
| <TypeScriptExample filename="./worker/index.ts"> | ||
|
|
||
| ```ts | ||
| import { WorkerEntrypoint } from "cloudflare:workers"; | ||
|
|
||
| export default class extends WorkerEntrypoint<Env> { | ||
| async fetch(request: Request) { | ||
| // This Worker script is only called for requests on paths starting with `/api/` | ||
| return Response.json({ | ||
| success: true, | ||
| message: 'Hello from API endpoint', | ||
| }); | ||
| } | ||
| } | ||
| ``` | ||
| </TypeScriptExample> | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.