Deploying SvelteKit to Vercel
So youâve got a SvelteKit app, and you want to put it in production⌠No worries! Nowadays, we can deploy to Vercel without writing any extra code đ Hereâs howâŚ
Prerequisites
The two âone-time setupâ things you need for any deployment are an account with GitHub and an account with Vercel. Theyâre both free for our purposes, and what I consider best in class at what they do!
You may sign-up for Vercel with GitHub SSO, which would automatically make your repos discoverable in the Vercel dashboard.
GitHub Setup
We first need to setup version control for our SvelteKit app; this is not done for us using the SvelteKit wizard with npm init svelte@next [APP-NAME-HERE]
, so we need to cd
into our app directory in a terminal, and run:
git init
git add -A
git commit -m "đ Initial commit"
Then, we go to GitHub and hit + -> New Repository on the top right.
In the next screen, we fill in a name and description to taste, then hit the New Repository button.
The next screen will be of our new, empty, repo, and will have a block of instructions in the end, of what to paste and execute back in the terminal, in our app directory!
git remote add origin git@github.com:[YOUR-REPO-HERE]
git branch -M main
git push -u origin main
Doing that means our GitHub setup is complete đ
Vercel Setup
Deploy time! Letâs go to our Vercel Dashboard and hit the âNew Projectâ button.
The next screen will have a dedicated section titled Import Git Repository. If we are in the correct organisation, the repo we just pushed to should be the first result, if not feel free to hunt it down using the search. Hit the âImportâ button next to it!
Vercel will automatically recognise weâre deploying a SvelteKit app, so we donât need to do anything more other than hit the Deploy button!
Extra Setup
The above is all we need for a sample app, but there are a couple things we may want to edit, according to our appâs needs; these can be modified later too, so no worries if we got too excited and pressed Deploy already.
Vercel will automatically figure out if weâre using yarn or npm as our package managers. However, if weâre using something more hipster like pnpm, weâd need to override the install
command to use that: hit the override toggle and fill in pnpm install
!
Finally, there is a section on Environment Variables: If our backend code needs API keys to integrate with something like Stripe, Sendgrid or Notion, or a POSTGRES_URL
to connect to a DB directly, this is where weâd set them up.
Do note that since SvelteKit uses Vite, only environment variables prefixed with VITE_
will be available to our source code by default!
Hit DEPLOY and visit our production app!
Deployment will take a bit of time, but we wonât need to touch our setup again: Every time we push to our main
branch, our app will automatically be deployed to production!
As a sweet bonus, whenever we open a Pull Request, Vercel will deploy it for us to a dedicated preview environment, so we can review it with our own eyeballs before merging, or even get our e2e tests to visit it and validate everything works!
Thatâs something companies would literally pay millions for in yearly staff salaries, and weâre now getting it for free, both in money, and in time. Thatâs nuts.
And by we, I mean individual developers and small teams; Vercel does charge a pretty penny to the large companies that use it, and I personally think itâs still well worth it, but thatâs a deeper discussion to be had!