Skip to content

Support run_worker_first in Vite plugin #9575

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

Merged
merged 18 commits into from
Jun 16, 2025
Merged

Conversation

jamesopstad
Copy link
Contributor

@jamesopstad jamesopstad commented Jun 12, 2025

Fixes #8430.
Fixes #8879.
Fixes #9109.

Support run_worker_first and static routing.


  • 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
  • Wrangler V3 Backport
    • TODO (before merge)
    • Wrangler PR:
    • Not necessary because: Not a Wrangler change

@jamesopstad jamesopstad requested a review from a team as a code owner June 12, 2025 10:41
@jamesopstad jamesopstad added the e2e Run wrangler + vite-plugin e2e tests on a PR label Jun 12, 2025
@jamesopstad jamesopstad requested a review from a team as a code owner June 12, 2025 10:41
@jamesopstad jamesopstad added vite-plugin Relating to the `@cloudflare/vite-plugin` package c3-e2e Run c3 e2e tests on a PR every-os Run tests (unit/e2e/c3-e2e) on every OS, rather than just macOS labels Jun 12, 2025
Copy link

changeset-bot bot commented Jun 12, 2025

🦋 Changeset detected

Latest commit: 1498a8f

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

This PR includes changesets to release 1 package
Name Type
@cloudflare/vite-plugin 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-project-automation github-project-automation bot moved this to Untriaged in workers-sdk Jun 12, 2025
@jamesopstad jamesopstad changed the title Support run_worker_first and static routing Support run_worker_first in Vite plugin Jun 12, 2025
Copy link

pkg-pr-new bot commented Jun 12, 2025

create-cloudflare

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

@cloudflare/kv-asset-handler

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

miniflare

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

@cloudflare/pages-shared

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

@cloudflare/unenv-preset

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

@cloudflare/vite-plugin

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

@cloudflare/vitest-pool-workers

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

@cloudflare/workers-editor-shared

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

wrangler

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

commit: 1498a8f

@jamesopstad jamesopstad force-pushed the james/run-worker-first branch from 3876562 to 5ef4d73 Compare June 12, 2025 11:59
@@ -1,5 +1,5 @@
{
"extends": "@cloudflare/workers-tsconfig/base.json",
"extends": "@cloudflare/workers-tsconfig/worker.json",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This change was necessary to prevent type errors when importing from workers-shared as workers-shared uses @cloudflare/worker-types.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm. OK.
I think we should be able to avoid that... I'll have a think about it. But not blocking this PR.

@jamesopstad jamesopstad force-pushed the james/run-worker-first branch from 5ef4d73 to 355cc8d Compare June 12, 2025 16:49
@jamesopstad jamesopstad requested a review from a team as a code owner June 12, 2025 16:49
@jamesopstad jamesopstad force-pushed the james/run-worker-first branch 6 times, most recently from f08bae5 to 64ab9fe Compare June 13, 2025 13:52
@@ -1,17 +1,30 @@
import asset from "./asset.txt?no-inline";
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
import asset from "./asset.txt?no-inline";
import assetURL from "./asset.txt?no-inline";

Copy link
Contributor

Choose a reason for hiding this comment

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

or

Suggested change
import asset from "./asset.txt?no-inline";
import assetPath from "./asset.txt?no-inline";

Comment on lines +354 to +356
entryWorkerConfig.assets?.run_worker_first === true
? { user_worker: ["/*"] }
: resolvedPluginConfig.staticRouting;
Copy link
Contributor

Choose a reason for hiding this comment

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

How about moving this bit of logic into the plugin-config or asset-config files?


preMiddleware = async (req, res, next) => {
// Only the URL pathname is used to match rules
const request = new Request(new URL(req.url!, UNKNOWN_HOST));
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this needed? Can't you just pas req through to each matcher?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

req is a Node.js http.IncomingMessage. The matchers require a Request object, even though they only use the URL pathname.

({ request }: { request: Request }) => {

Would be nice to change this but I guess it's for consistency with the other utils (also odd that generateStaticRoutingRuleMatcher is in the Asset Worker utils when it's only used by the Router Worker).


preMiddleware = async (req, res, next) => {
// Only the URL pathname is used to match rules
const request = new Request(new URL(req.url!, UNKNOWN_HOST));
Copy link
Contributor

Choose a reason for hiding this comment

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

Ideally avoid ! assertions and use a runtime assertion to narrow the type:

Suggested change
const request = new Request(new URL(req.url!, UNKNOWN_HOST));
assert(req.url, "expected request URL to be defined");
const request = new Request(new URL(req.url, UNKNOWN_HOST));

@@ -1,5 +1,5 @@
{
"extends": "@cloudflare/workers-tsconfig/base.json",
"extends": "@cloudflare/workers-tsconfig/worker.json",
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm. OK.
I think we should be able to avoid that... I'll have a think about it. But not blocking this PR.

@github-project-automation github-project-automation bot moved this from Untriaged to Approved in workers-sdk Jun 16, 2025
@jamesopstad jamesopstad force-pushed the james/run-worker-first branch from 64ab9fe to 1498a8f Compare June 16, 2025 16:01
@jamesopstad jamesopstad added this pull request to the merge queue Jun 16, 2025
Merged via the queue into main with commit 5601fc3 Jun 16, 2025
32 of 33 checks passed
@jamesopstad jamesopstad deleted the james/run-worker-first branch June 16, 2025 16:52
@github-project-automation github-project-automation bot moved this from Approved to Done in workers-sdk Jun 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c3-e2e Run c3 e2e tests on a PR e2e Run wrangler + vite-plugin e2e tests on a PR every-os Run tests (unit/e2e/c3-e2e) on every OS, rather than just macOS vite-plugin Relating to the `@cloudflare/vite-plugin` package
Projects
Archived in project
2 participants