workers limitation and server-side-rendering, data storage and sveltekit. Help me understand please
Hello
I have been researching on workers and still haven figured out, if i can use it for a fullstack application.
If yes, how can i be able to do it. Also, if so, how does it work?
I am struggling to figure it out.
1. When having sveltekit framwork running on pages, does it automatically work some magic with “workers” in the background? (Even without me configuring further into it?)
2. In the end the real question is, can i store data from the static site html “forms”, just as if it were running on a server? If so, is this determined by url paths? Can i use POST? What can i do, what cant i do. What do you recommend for me? Where can i learn more! 🤯🤷
Thanks in advance for helping me understand!
1 Reply
Hey 👋
Yes you can use workers and Cloudflare pages to run fullstack applications.
I don't have a huge amount of knowledge in svelte particularly, but I can maybe give a generic explanation that applies to most of these meta frameworks (Next, Remix, Nuxt, Sveltekit etc)
To answer your first question.
Tldr, there are two build outputs for most of these apps, a client bundle which has your usual JS, css etc and a server bundle.
Most frameworks have adapters for the server bundle that can target different runtimes. E.g Sveltekit has a cloudlfare pages adapter - https://kit.svelte.dev/docs/adapter-cloudflare
When you use the adapter for Cloudflare pages in all these frameworks, there is an underlying worker that is built and deployed. This worker handles the SSR + any "server" actions that you run in your app.
E.g a lot of the metaframeworks have a way of writing code that runs only on the server (I believe in sveltekit it's the
load
function).
These are usually built out and run within the worker that the adapter generates for you
When using Cloudflare pages, try not to worry about what is client vs what is a worker. Just use the framework and it's adapter and everything is handled for you automatically.
Now, to answer your second question. Yes you can absolutely save data from static html forms. Just use the regular actions / POST endpoint setup that your framework has. These pieces of code are usually run only on the server (aka your worker).
When you use an adapter, it will typically give you a way to use bindings to other Cloudflare resoruces, e.g the sveltekit adapter has docs on this - https://kit.svelte.dev/docs/adapter-cloudflare#bindings
It's up to you how you want to store data, but if you're just starting out I'd recommend D1. It's an sqlite database and it can be bound do your pages project very easily. From there you can just make SQL inserts / queries in your action / endpoint.
Check out the D1 docs to learn more about it. Here's a link on how to use it with sveltekit - https://developers.cloudflare.com/d1/examples/d1-and-sveltekit/
If you feel like you don't need an SQL database, you can also just use Cloudflare KV - https://developers.cloudflare.com/pages/platform/functions/bindings/#kv-namespaces
https://developers.cloudflare.com/kv/
Hopefully that helps and gives some clarity. Feel free to ask if you have any more questions 👍