Skip to content

Add support for static routing to Workers Assets#9416

Merged
CarmenPopoviciu merged 8 commits intocloudflare:mainfrom
matthewdavidrodgers:matthewrodgers/WC-3583-static-routing-redo
Jun 6, 2025
Merged

Add support for static routing to Workers Assets#9416
CarmenPopoviciu merged 8 commits intocloudflare:mainfrom
matthewdavidrodgers:matthewrodgers/WC-3583-static-routing-redo

Conversation

@matthewdavidrodgers
Copy link
Contributor

@matthewdavidrodgers matthewdavidrodgers commented May 30, 2025

For WC-3583

Implements the proposal noted here #9143

In brief: when static routing is present for a Worker with assets, routing via those static rules takes precedence. When a request is evaluated in the Router Worker, the request path is first compared to the "asset" rules (which are to be specific via "negative" rules, e.g. "!/api/assets"). If any match, the request is forwarded directly to the Asset Worker. If instead any "user_worker" rules match, the request is forwarded directly to the User Worker. If neither match (or static routing was not provided), the existing behavior takes over.

As part of this explicit routing, when static routing is present, the check against "Sec-Fetch-Mode: navigate" (to guess if this should serve an asset or go to the User Worker for not_found_handling) is disabled. Routing can be controlled by setting routing rules via "run_worker_first", and asset serving (including when an index.html or 404.html page is served) will be more simple.


  • Tests
    • TODO (before merge)
    • Tests included
    • Tests not necessary because:
  • Wrangler / Vite E2E Tests CI Job required? (Use "e2e" label or ask maintainer to run separately)
    • I don't know
    • Required
    • Not required because:
  • Public documentation
    • TODO (before merge)
    • Cloudflare docs PR(s):
    • Documentation not necessary because: others are providing this
  • Wrangler V3 Backport
    • TODO (before merge)
    • Wrangler PR:
    • Not necessary because: not a wrangler change

@matthewdavidrodgers matthewdavidrodgers requested review from a team as code owners May 30, 2025 21:33
@changeset-bot
Copy link

changeset-bot bot commented May 30, 2025

🦋 Changeset detected

Latest commit: ccad014

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@cloudflare/workers-shared Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions
Copy link
Contributor

github-actions bot commented Jun 2, 2025

A Wrangler prerelease is available for testing. You can install this latest build in your project with:

npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/15464636933/npm-package-wrangler-9416
Prereleases for other packages:

cloudflare-workers-bindings-extension:

wget https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/15464636933/npm-package-cloudflare-workers-bindings-extension-9416 -O ./cloudflare-workers-bindings-extension.0.0.0-v5790de821.vsix && code --install-extension ./cloudflare-workers-bindings-extension.0.0.0-v5790de821.vsix

create-cloudflare:

npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/15464636933/npm-package-create-cloudflare-9416 --no-auto-update

@cloudflare/kv-asset-handler:

npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/15464636933/npm-package-cloudflare-kv-asset-handler-9416

miniflare:

npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/15464636933/npm-package-miniflare-9416

@cloudflare/pages-shared:

npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/15464636933/npm-package-cloudflare-pages-shared-9416

@cloudflare/unenv-preset:

npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/15464636933/npm-package-cloudflare-unenv-preset-9416

@cloudflare/vite-plugin:

npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/15464636933/npm-package-cloudflare-vite-plugin-9416

@cloudflare/vitest-pool-workers:

npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/15464636933/npm-package-cloudflare-vitest-pool-workers-9416

@cloudflare/workers-editor-shared:

npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/15464636933/npm-package-cloudflare-workers-editor-shared-9416

@cloudflare/workers-shared:

npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/15464636933/npm-package-cloudflare-workers-shared-9416

@cloudflare/workflows-shared:

npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/15464636933/npm-package-cloudflare-workflows-shared-9416

Note that these links will no longer work once the GitHub Actions artifact expires.

@matthewdavidrodgers matthewdavidrodgers force-pushed the matthewrodgers/WC-3583-static-routing-redo branch 2 times, most recently from f655309 to 96c27f9 Compare June 2, 2025 21:35
@github-project-automation github-project-automation bot moved this from Untriaged to Approved in workers-sdk Jun 2, 2025
@matthewdavidrodgers matthewdavidrodgers force-pushed the matthewrodgers/WC-3583-static-routing-redo branch from 96c27f9 to da1c46f Compare June 3, 2025 01:14
@emily-shen emily-shen added the e2e Run wrangler + vite-plugin e2e tests on a PR label Jun 3, 2025
- When a request matches a static routing "exclude" rule, it's directly
forwarded to the asset worker.

- When a request matches a static routing "include" rule, it's directly
forwarded to the user worker.

- Otherwise, previous behavior takes over

This also adds a new analytics field (double6) for what routing decision
was made
vitest yelled at me and told me this should be awaited and may error in
future releases
When the Router worker has static routing, the check against
"Sec-Fetch-Mode: navigate" is unnecessary. We have explicit static
routing to indicate whether or not we should go to a User worker or
the Asset worker, and should not try and guess via usually-set browser
headers

This adds a new parameter to unstable_canFetch RPC method, which should
be fine for backwards compatibility, and can be extended in the future
if needed. This was necessary because the Asset worker checks the
Request headers for Sec-Fetch-Mode to indicate if it can serve an asset
(including an index.html or 404.html page based on not_found_handling),
but static routing is only provided to the Router worker. Thus, we need
to pass more information over RPC
Now that we're not providing this configuration via _routes.json, we can
relax some of the configuration. Much of this happens on the backend,
but some field names have been changed ("include" -> "worker",
"exclude"->"asset")
@emily-shen
Copy link
Contributor

as its on a fork, the tests were run here: #9491. however, if i used actually the same sha (so github could associate tests passing on that PR with this PR), it was still running the test on the fork 🫠

so i updated the commit message so the sha is different, it ran on the fork successfully, but now the passing tests don't get associated with the original PR.

so we're going to bypass checks on this PR, but the tests did pass, somewhere 🫠

@pkg-pr-new
Copy link

pkg-pr-new bot commented Jun 6, 2025

create-cloudflare

npm i https://pkg.pr.new/create-cloudflare@9416

@cloudflare/kv-asset-handler

npm i https://pkg.pr.new/@cloudflare/kv-asset-handler@9416

miniflare

npm i https://pkg.pr.new/miniflare@9416

@cloudflare/pages-shared

npm i https://pkg.pr.new/@cloudflare/pages-shared@9416

@cloudflare/unenv-preset

npm i https://pkg.pr.new/@cloudflare/unenv-preset@9416

@cloudflare/vite-plugin

npm i https://pkg.pr.new/@cloudflare/vite-plugin@9416

@cloudflare/vitest-pool-workers

npm i https://pkg.pr.new/@cloudflare/vitest-pool-workers@9416

@cloudflare/workers-editor-shared

npm i https://pkg.pr.new/@cloudflare/workers-editor-shared@9416

wrangler

npm i https://pkg.pr.new/wrangler@9416

commit: ccad014

@CarmenPopoviciu
Copy link
Contributor

CarmenPopoviciu commented Jun 6, 2025

I went the less elegant way and checked out this PR locally, then pushed to the remote repo and PRed from my branch. This seems to have done the trick and running the tests on my PR ran the tests on this PR as well. All is 🍏 so this is ready to merge 🎉

@CarmenPopoviciu CarmenPopoviciu merged commit 3383021 into cloudflare:main Jun 6, 2025
75 of 125 checks passed
@github-project-automation github-project-automation bot moved this from Approved to Done in workers-sdk Jun 6, 2025
@workers-devprod workers-devprod added the contribution [Holopin] Recognizes an open-source contribution, big or small label Jun 6, 2025
@holopin-bot
Copy link

holopin-bot bot commented Jun 6, 2025

Congratulations @matthewdavidrodgers, the maintainer of this repository has issued you a holobyte! Here it is: https://holopin.io/holobyte/cmbkzd7pe1100707jvz3ellhhz

This badge can only be claimed by you, so make sure that your GitHub account is linked to your Holopin account. You can manage those preferences here: https://holopin.io/account.
Or if you're new to Holopin, you can simply sign up with GitHub, which will do the trick!

@workers-devprod workers-devprod mentioned this pull request Jun 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contribution [Holopin] Recognizes an open-source contribution, big or small e2e Run wrangler + vite-plugin e2e tests on a PR

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

7 participants