cloudflare / wrangler-legacy

🀠 Home to Wrangler v1 (deprecated)
https://workers.cloudflare.com
Apache License 2.0
3.2k stars 337 forks source link

Serving both static HTML and JS using `build.upload.rules` type `Data` #2134

Closed APTy closed 3 years ago

APTy commented 3 years ago

πŸ› Bug report

Describe the bug

Unexpected behavior serving javascript files using build.upload.rules when two entries of type Data are supplied (one for **/*.html and one for **/*.js)

Reproduce the bug

A list of steps to reproduce the bug.

  1. Run git clone https://github.com/APTy/workers-chat-demo.git && cd workers-chat-demo a. This is a fork of https://github.com/cloudflare/workers-chat-demo with an extra commit (https://github.com/APTy/workers-chat-demo/commit/f02308f9d617837f54ee2739dff1cdf96be9c2c1) on top.
  2. Run wrangler publish && wrangler dev
  3. Run curl localhost:8787

Expected behavior

We expect to see the content of this file: https://github.com/APTy/workers-chat-demo/blob/master/src/foo.js being served:

$ curl localhost:8787
console.log("test");

What we see instead

$ curl localhost:8787
[object Object]

What's peculiar is that foo.js is executed by the cloudflare worker.. The "test" print statement shows up on the server's logs! 😱 This is mildly concerning because the docs say that the "Data" type should only be serializing these files into an ArrayBuffer and not executing it.

$ wrangler dev
πŸ‘‚  Listening on http://127.0.0.1:8787
[2021-11-15 20:01:57] GET edge-chat-demo.calandra.workers.dev/ HTTP/1.1 200 OK
test

Environment and versions

Fill out the following information about your environment.

[build.upload] format = "modules" dir = "src" main = "./chat.mjs"

[works] the following line correctly serves the javascript file

rules = [{type = "Data", globs = ["*/.js"]}]

[doesn't work] the following line does not serve the javascript file correctly

rules = [{type = "Data", globs = ["*/.html"]}, {type = "Data", globs = ["*/.js"]}]

[durable_objects] bindings = [ { name = "rooms", class_name = "ChatRoom" }, { name = "limiters", class_name = "RateLimiter" } ]

Indicate that you want the ChatRoom and RateLimiter classes to be callable as Durable Objects.

[[migrations]] tag = "v1" # Should be unique for each entry new_classes = ["ChatRoom", "RateLimiter"]

nilslice commented 3 years ago

@APTy - Hi, thanks for putting together a great report here. This is actually the expected behavior, and with a small tweak to your wrangler.toml, you can achieve the outcome you're looking for:

- rules = [{type = "Data", globs = ["**/*.html"]}, {type = "Data", globs = ["**/*.js"]}]
+ rules = [{type = "Data", globs = ["**/*.html"], fallthrough = true}, {type = "Data", globs = ["**/*.js"]}]

adding fallthrough = true to your Data module type rule will indicate that other matches can be considered, a la the docs:

fallthrough (optional) This option allows further rules for this module type to be considered if set to true. If not specified or set to false, further rules for this module type will be ignored.

(search for "fallthrough" on this page: https://developers.cloudflare.com/workers/cli-wrangler/configuration)

Closing the issue, but please feel free to leave more comments / questions.