gja / cloudflare-worker-local

Run a Cloudflare Worker Locally

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Binary site assets not transferring correctly

mhamann opened this issue · comments

I have a CF Worker that also uses the "site" functionality to serve up static assets like images. This works fine in the real wrangler dev environment, but I would like something that works entirely locally so I don't have to jump through hoops to refresh static files like CSS, JS, etc.

When I run it with cloudflare-worker-local, the images seem to transfer across the wire with 200 OK messages, but they are mangled somehow. Text-based files come across just fine, but anything binary doesn't. It must be some sort of content encoding issue, but I haven't yet been able to figure out what's causing it.

Any ideas? I'll keep digging in the meantime...

I found the root cause to be that the file KV store was reading all files from disk as utf-8. That breaks anything that's essentially a binary format, thus the issue with images but not with CSS or HTML files.

One mechanism for fixing it is to return everything from the disk as a buffer than then send that back. I haven't fully grokked the entire codebase, but making that change did fix the problem. I had to fix all of the KV tests since they also assumed text-only content. PR is #76.

Please let me know what you think of the approach or if you have other thoughts/ideas on fixing it.