How can i use d1 locally(for test) on sveltekit?
I use sveltekit on cloudflare(with cloudflare adapter). Recently i fall in love on cloudflare serverless echo system.
I want to test my code on dev mode but evnet.platform is always undefined.
run script:
npx wrangler pages dev --local --d1=DB -- npm run dev
result:
--compatibility-date=2023-07-06
How can i use d1 locally?
refer:
- https://kit.svelte.dev/docs/adapter-cloudflare#bindingsSvelteKit docs
Cloudflare Pages • SvelteKit documentation
6 Replies
At the moment, you have to build SvelteKit before running wrangler
Then, should i use this method?
---
vite build --watch
- this creates the cloudflare directories in the .svelte-kit directory
- vite dev doesn't seem to create the cloudflare directories so I've used vite build
wrangler pages dev .svelte-kit/cloudflare --live-reload
- this starts the app
---
https://stackoverflow.com/questions/74904528/how-to-run-sveltekit-cloudflare-page-locally
Stack Overflow
How to run SvelteKit & Cloudflare Page locally?
I'm trying to create a new app using SvelteKit & Cloudflare Pages and I'm struggling with running the application locally in a proper manner. Currently, in order to get the app running locally ...
Sorry for reviving this, but I've been trying for a couple days now to get this working and wrangler live-reload spits errors whenever vite rebuilds... it looks like wrangler reloads too fast and the files are not yet built. Did you experience this ?
I've got similar issues, yes. Did you solve them yet?
I am so close to just keeping cloudflare pages but no bindings, just http coms to the other cloudflare services because I can have real good DX while locally developing then
Are you ready for a janky solution 😛 Let me find what I ended up doing
I've decided to use nodemon to automate some stuff. These are my commands in package.json. I run both commands in 2 different terminals when doing local dev, first one builds, second one serves.
The idea is to make the build + serve steps syncrhonous. To do that I monitor my /src folder for changes and build the app on change.Then I create a dummy file (in this case
reload.trigger
) when the build is done.
Second, I monitor for the dummy file creation then trigger wrangler to serve my pages. Then delete the trigger file. (note I also make sure to kill workerd because of some weird port already in use error than wrangler gets into sometimes)
Disclaimer: I haven't worked with this since the date of my post and it is possible wrangler does this better now
I had something similar as well 🙂 I researched a bunch yesterday and finally figured out how to have the super fast hotreload and still use cf bindings: https://github.com/sveltejs/kit/issues/4292#issuecomment-1732643660. Just if anyone reads this later, this is probably the best solution with near to now drawbacks
GitHub
Platform context fallbacks · Issue #4292 · sveltejs/kit
Describe the problem When developing with svelte-kit dev, the event.platform object is always empty, with no great way to mock it. When building using an adapter like Cloudflare Workers, event.plat...