unjs / nitro

Next Generation Server Toolkit. Create web servers with everything you need and deploy them wherever you prefer.
https://nitro.unjs.io
MIT License
5.94k stars 497 forks source link

Provide local bindings for Cloudflare runtime features in development #2044

Open XiNiHa opened 9 months ago

XiNiHa commented 9 months ago

Describe the feature

Currently, it is not possible to use Cloudflare runtime-specific feature bindings (like KV, DO, R2, D1, ...) in development. I thought that was a fundamental limitation of the way how Wrangler works. However, I found that the Astro Cloudflare adaptor provides local bindings, using Miniflare in development. It'll be a huge productivity boost on apps that utilize Cloudflare runtime-specific features since it'll allow us to keep iterating on the dev server, rather than building the production bundle per every file saves.

Additional information

ChristianJacobsen commented 7 months ago

This can move forward with Wrangler's new getBindingsProxy or their just-released getPlatformProxy that provides cf, ctx, and caches as well as the bindings. That way we can use Nitro's dev server with its HMR and no longer have to do the intermediary build using Wrangler's command and watch_dir options.

I can have a look at it if none of the maintainers would like to do so themselves.

pi0 commented 7 months ago

Here is a Nitro module we made in collaboration with CF team: https://github.com/pi0/nitro-cloudflare-dev/tree/main that leverages this API (sorry for lack of docs)

In the close feature it will be even better by being able to directly use Wrangler as Dev server rntime.

ChristianJacobsen commented 7 months ago

Amazing news @pi0! Can't wait to make use of it!

jamaluddinrumi commented 7 months ago

i need it, too.

for, now i use:

package.json

scripts: {
    ...
    "dev:wrangler": "NITRO_PRESET=cloudflare_module wrangler dev .output/server/index.mjs --env development --site .output/public --port 8101",
    "proxy": "local-ssl-proxy --key ../127.0.0.1+7-key.pem --cert ../127.0.0.1+7.pem --source 4343 --target 8101",
    ...
}

for my local development.

and i need to restart dev:wrangler script whenever there any changes happen in my D1 records.

i'm still blessed with the current nitro & don't have any problems. but it will makes nitro better if i don't have to do restart so often.

EDIT:

sorry, it doesn't relate. i need to restart whenever any changes happen in my D1 records because my route is served from cache.

it's because i use Cached Event HandlerdefineCachedEventHandler instead of defineEventHandler for my route.