So I created this information system with Sveltekit, Ubuntu 22.04 as the server OS, PM2 as the process manager and I’m running the app in Node.Js. I’ve read the Sveltekit documentation on how to build and deploy the app for node adapter and here’s my svelte.config.js
code:
import adapter from '@sveltejs/adapter-node';
import { vitePreprocess } from '@sveltejs/kit/vite';
import path from 'path';
/** @type {import('@sveltejs/kit').Config} */
const config = {
// Consult https://kit.svelte.dev/docs/integrations#preprocessors
// for more information about preprocessors
preprocess: vitePreprocess(),
kit: {
// adapter-auto only supports some environments, see https://kit.svelte.dev/docs/adapter-auto for a list.
// If your environment is not supported or you settled on a specific environment, switch out the adapter.
// See https://kit.svelte.dev/docs/adapters for more information about adapters.
adapter: adapter({
// default options are shown
out: 'build',
precompress: false,
envPrefix: '',
polyfill: true
}),
// Disable CSRF
csrf: {
checkOrigin: false,
},
alias: {
// these are the aliases and paths to them
'$src': path.resolve('./src')
}
},
vitePlugin: {
inspector: false
}
};
export default config;
After building it, all I need to do is submit the command npm run preview
in order to run the app.
Since I’m using PM2, I created an ecosystem.config.cjs
file so that I can run the app with PM2 command. Here’s the configuration:
module.exports = {
apps: [
{
name: "app_sistamu",
script: "npm",
args: "run preview -- --host --port 8080",
watch: false,
instances: "max",
exec_mode: "cluster"
},
{
name: "app_sistamu_DEV",
script: "npm",
args: "run dev -- --host --port 5173",
out_file: "/dev/null",
watch: false
}
],
};
After all the preparation, all I have to do is merging everything from development branch to master branch. Here’s what I do:
git merge -m "Merging branch development to master" development; npm install; npm run build; pm2 reload ecosystem.config.cjs --only app_sistamu
With this my application can now run on the server. There’s just one thing. The next time if I want to make a changes, I need to rebuild the app and then reload the PM2. I mean yeah it makes sense because in order to deploy the app I need to build it again. But this causes a problem where during the build process, the app returns a 502 Bad Gateway error. But after everything is done, the app is back to normal again without any error.
This raises a question, how to deploy the app properly without disrupting user’s interaction with the app?
2
Answers
To make my comment an answer:
If you’re doing
and you say the app is disrupted during the build, that probably means that
npm run build
is clearing out or even deleting thedist
directory thatnpm run preview
is serving.With a deployment like that, you could probably build into another directory, then quickly swap the two to minimize the disruption:
Remember to clear out the
old-dist
directories, or they’ll start piling up. 🙂A very common solution is to
A typical workflow in this case is to break the building of the application and the deployment of the application to a two different parts.
Then on the production server.
This does not only apply to SvelteKit only, but any web server and web development today.
The downside this approach is that you need to learn how to use Docker first, and it is a lot to learn. However the Docker knowledge is reusable, and works for many different deployment workflows regardless of tools and industry.