How to use Serverless Cloud with SvelteKit

Apr 20, 2022

With Serverless Cloud, getting started with SvelteKit is incredibly easy thanks to our templates and built-in adapter.

Creating an App

First, make a new directory for your project and switch to it:

mkdir svelte-demo
cd svelte-demo

Next, start the Cloud Shell with the following command:

npx @serverless/cloud --template sveltekit

Follow the on-screen instructions to log in, and name your app. Soon you’ll find yourself looking at a prompt similar to this:

You’re almost ready, just type “dev” and hit enter:

This will launch the svelte-kit dev server within the Cloud Shell, and you will see output like so:

Now, open http://localhost:3000 in your browser.  Congratulations, you’re running a local dev instance of the SvelteKit starter - but wait, it’s been adapted to use the Cloud SDK!  Open up src/routes/todos/index.js - notice anything different?  The default SvelteKit starter uses a remote API to store data, but with Serverless Cloud, data is right at your fingertips.

Not only that, but it takes only a few lines of code to write event handlers that respond to changes to this data - check out index.js in the project root, and you’ll see a data.on handler. Whenever a todo is modified, that event handler will fire.  Try it from your localhost dev url - add some todos, and you’ll see logs printed in your console almost immediately.

Developer sandboxes are great, but let’s make a preview to share with a friend.  Just type “share”.  This will build and deploy a completely separate instance of your application right in the cloud in under a minute, and print a URL out when done.  You can also deploy to a named stage with the “deploy” command - e.g. “deploy staging”.  Finally, you could run “promote staging production”, which re-uses the build output from the source environment for determinism.

A good next step is to explore the docs to learn more about all the features included with Serverless Cloud - you will experience workflows that offer a level of productivity you’ve never seen before, from fast deployments to quick and easy reseeding of test data, and we’re working hard on adding more features every release.

Summary

In this article, you’ve seen how to quickly get started using SvelteKit with Serverless Cloud and the built-in Data SDK. Stay tuned for future installments where we’ll show you how to set up GitHub Actions to take advantage of preview instances on pull requests, and much more! Join us on Discord for further discussion!

Subscribe to our newsletter to get the latest product updates, tips, and best practices!

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.