Host SvelteKit apps with SSR-support via ploi.io (on Hetzner Cloud)
Matthias Andrasch
Posted on December 16, 2022
SvelteKit 1.0 was officially released a few hours ago 🥳. Time to dive into NodeJS and SvelteKit hosting. One key advantage of SvelteKit is that you can generate static sites as well as take advantage of Server Side Rendering (SSR). The best thing: You can mix these approaches.
Mix and match prerendered pages for maximum performance with dynamic server-side rendering for maximum flexibility.
In order to use the SSR features, you need to host your app as NodeJS application.
One option for this is to use the awesome server management service ploi.
ploi.io
Ploi.io is like a nice graphical user interface for managing and configuring virtual private servers (VPS). Ploi does not host your site, it's just a middleware service connecting to your servers.
It basically offers three key features:
- Create and configure (virtual) servers
- Connect git repositories to these servers
- Deploy applications via deploy scripts
🇪🇺 An additional advantage is that ploi enables you to deploy NodeJS apps to various server providers like Hetzner Cloud. Hetzner offers servers which are located completely in the European Union (which is good for GDPR-compliance if you or your customers are located in the EU). I blogged about this here briefly.
Prerequisites
Steps I won't cover in this tutorial:
- Sign up for customer account on ploi.io
- Sign up for a customer account on Hetzner Cloud
- Install a new server on Hetzner Cloud via ploi.io (Guide: Server installation). I selected the CX11 type.
- Add an external domain to your server (Guide: How do I connect my domain). You can also use test domains.
Tutorial: Deploy and host SvelteKit
Create a new site on our server
First we need to create a new site on our server:
Expand the advanced settings and use the following:
(Set the project directory to /
, otherwise you won't be able to request an SSL cert in the next steps)
Install project from a git repository
Afterwards we can install our project from a git repository:
I added my GitHub account and used my demo repo sveltekit-company-demo:
After adding a repository you can edit the deploy script of your new site:
Replace it with the following, we will need to add npm run build
:
cd /home/ploi/sveltekit-company-demo.mandrasch.dev
git pull origin main
# install
npm install
npm run build
# Server restarting is handled by plois NodeJS settings
echo "🚀 Application deployed!"
SSL certificate
We will also need a SSL certificate. Thanks to the Lets's Encrypt initiative this is easily done nowadays:
NodeJS settings (pm2)
Now we need to make adjustments to the NodeJS settings. ploi will use pm2 by default to host our SvelteKit app, Supervisor is also possible.
Change the NodeJS settings to the following:
Important: If you create your first site, it will run on port 3000. SvelteKits adapter-node assumes port 3000 by default. But if you add another site on a server, ploi will use a different port like 3001, 3002, etc. for each new project.
Therefore we need to make sure to set the env variable for the specific port correctly:
PORT=3001 node build/index.js
Thanks very much to aarondiel!
Switch to adapter-node
In order to run our SvelteKit Web App with NodeJS, we also need to apply the following changes to our projects source code.
We need to install adapter-node:
npm i -D @sveltejs/adapter-node
And we need to replace
import adapter from '@sveltejs/adapter-auto';
with
import adapter from '@sveltejs/adapter-node';
in svelte.config.js
:
import adapter from '@sveltejs/adapter-node';
import { vitePreprocess } from '@sveltejs/kit/vite';
/** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {
adapter: adapter()
},
preprocess: vitePreprocess()
};
export default config;
See SvelteKits official docs for all information:
Adapters > Supported Environments > NodeJS
Commit and push these changes.
Deploy (to the build/-directory)
It is time to deploy our app. Let's hit the "Deploy now" button!
You can check the status in the deployments section below:
Spawn the app with pm2
Our app is now successfully prepared in the build/
directory on the server, but we still need to start our app via the pm2 service.
Hit "Spawn" in the NodeJS settings dashboard:
You should see a brief success message and the buttons beneath should change as following:
Congrats, your SvelteKit app is now live! 🥳🥳🥳
If you experience issues, see troubleshooting below.
Have I missed something or can this be optimized? How do you host SvelteKit apps? Let me know in the comments, much appreciated!
Further resources
Troubleshooting
You can check the NodeJS error log for information if something failed. In my case I wasn't aware I need to set the PORT=3001 if I use another port than 300.
Investigating errors is also possible via SSH and pm2 list
or pm2 logs
command:
See pm2 quickstart docs for more information.
What is pm2?
"PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks." (pm2)
Other similiar tools are nodemon and supervisor.
Reboot configuration
If this is your first time using pm2 on your Hetzner server, you need to add pm2 to the $PATH for server reboots. Follow the ploi.io-guide How to make pm2 start on boot.
In my case I logged in to the Hetzner server ssh ploi@x.x.x.x
via terminal / SSH. I previously added my laptops SSH key to ploi, see https://ploi.io/documentation/ssh for more information.
I ran the command pm2 startup
and pasted the suggested command (sudo env PATH=$PATH ...
):
The root server password was retrieved via mail after the initial server completion (https://ploi.io/documentation/ssh/how-do-i-login-as-root-user):
What is SSR?
- Rich Harris explains why SvelteKit pushes for Server Side Rendering (and against SPA / CSR)
- https://kit.svelte.dev/docs/glossary
- https://learn.svelte.dev/tutorial/ssr
- ... and more resources on YouTube, Twitter, Mastodon, etc.
SvelteKit hosting and GDPR
I blogged about this topic briefly:
How to host SvelteKit SSR apps (GDPR-compliant)?
In general: Big big kudos to Vercel for sponsoring the open source development of Svelte and SvelteKit in a massive way (https://vercel.com/blog/vercel-welcomes-rich-harris-creator-of-svelte)! 🙏👏
Posted on December 16, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.