honojs / vite-plugins

Vite Plugins for Hono
https://hono.dev
132 stars 34 forks source link

hono cloudflare pages + vite dev server fails to load Worker #187

Closed benjamine closed 1 month ago

benjamine commented 1 month ago

Steps to Reproduce:

  1. follow the guide at https://hono.dev/docs/getting-started/cloudflare-pages
  2. at /src/client.ts add code loading a Worker (according to vite docs) as:
console.log("loading client.ts")

const worker = new Worker(new URL('./worker.ts', import.meta.url), {
    type: 'module',
  });
  1. add a /src/worker.ts:
console.log("worker is running");

self.onmessage = (event) => {
  console.log("message from App:", event.data);
};
  1. start the vite dev server and open home page.

The worker never loads, as the server responds with a 404 when trying to load the worker module:

image

Note:

Dependencies:

  "dependencies": {
    "@hono/vite-cloudflare-pages": "^0.4.2",
    "hono": "^4.6.3"
  },
  "devDependencies": {
    "@cloudflare/workers-types": "^4.20240529.0",
    "@hono/vite-build": "^1.0.0",
    "@hono/vite-dev-server": "^0.15.1",
    "vite": "^5.2.12",
    "wrangler": "^3.57.2"
  }
benjamine commented 1 month ago

my-app.zip

uploading a zipped version of example project to help reproduce (used bun as package manager, use bun i and bun run dev to test).

yusukebe commented 1 month ago

Hi @benjamine

Set up the exclude option of the dev server.

devServer({
  exclude: [/^\/src\/worker.ts/, ...defaultOptions.exclude], // <===
  entry: 'src/index.tsx'
})
benjamine commented 1 month ago

@yusukebe you're my hero! πŸ™Œ that worked.

you probably already figured this out, but I'm guessing an official patch would be adding something like \/\?worker/ (catch both ?worker and ?worker_file), or just allow any querystring after .ts/.tsx files. at https://github.com/honojs/vite-plugins/blob/ae8938828d3d37ca37cd790dd87bb34752f0294b/packages/dev-server/src/dev-server.ts#L50-L59 I'll keep it in my vite config for now πŸ‘

εΏƒγ‹γ‚‰ζ„Ÿθ¬γ—γ¦γ„γΎγ™γ€‚

yusukebe commented 1 month ago

@benjamine

Good! Can we close this issue?

benjamine commented 1 month ago

I'd suggest patching that regex list for any others trying to use web workers too, vite has a couple query strings like those.

(happy to make a separate PR for that if you think it makes sense)

Otherwise this can be closed πŸ‘

yusukebe commented 1 month ago

I'd suggest patching that regex list for any others trying to use web workers too, vite has a couple query strings like those.

I think it's not necessary to modify vite-plugins/packages/dev-server/src/dev-server.ts because users can change the value themselves. I made it an option for the purpose.

benjamine commented 1 month ago

closed πŸ‘,

ps: I'd still suggest maybe a mention in docs at least? or throw a more explicit error?

as this behavior would also confuse anyone else trying to use a web worker the way vite recommends.

yusukebe commented 1 month ago

PS: I'd still suggest maybe a mention in the docs at least or throwing a more explicit error.

There is a description of the exclude option here currently:

https://github.com/honojs/vite-plugins/tree/main/packages/dev-server#exclude

But, as you did not realize, it is not friendly. Is it good to add this page? https://hono.dev/docs/getting-started/cloudflare-pages

I don't want to make it an error, but adding docs is good!