Custom SvelteKit Server: dev + production with startup tasks #13841
stephane-klein
started this conversation in
Show and tell
Replies: 1 comment 1 reply
-
Very cool. Have you checked out this hook? I think it does something similar. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Need to execute tasks at startup of your SvelteKit app? Here's a Proof of Concept of a SvelteKit Node custom server, which has the following characteristics:
npm run dev
) as well as production mode (npm run build; npm run preview
)This allows me to create a Monolithic application.
Here's this POC: https://github.com/stephane-klein/poc-sveltekit-custom-server
I decided to share this project here because I thought it might be useful to some people, and also to ask you some questions at the end of this document.
To create this POC, I followed the official documentation SvelteKit Node custom server.
Here's what the custom_server
src/server.js
looks like (click here to see the complete file version). Here, I've broken down this file to comment on it:Below, I can launch the data model migration right at service startup:
And then, I instantiate node-cron to launch a task every hour:
The
src/server.js
file has two operating modes. The first is "dev" mode, which instantiates a Vite server:This works like vanilla SvelteKit
npm run dev
, with auto-reload...Then, here's the code that's executed in built mode (
npm run build; npm run preview
):For the rest of the project, nothing special, it's a simple Express server:
The
src/server.js
file is copied to the/build/
folder when executingpnpm run build
.I tested the POC's proper functioning in a Docker container.
Difficulty Point
SvelteKit uses module aliases, such as
$lib
.Problem: by default, these module aliases are not configured when executing
node src/server.js
.To allow me to import modules from
src/lib/server/*
insrc/server.js
like:I used the
esm-module-alias
library.This complicates the project a bit, I had to configure this in
/package.json
:--loader esm-module-alias/loader --no-warnings
aliases
sectionAnd in
/vite.config.js
:alias
This complicates the project slightly, but I find it quite acceptable.
Summary and Questions
For now, I haven't pushed the implementation of this POC very far, but so far, everything seems to work very well.
The Developer Experience remains very good, identical to a classic SvelteKit.
Questions:
esm-module-alias
or to automatically configure@lib/
?Thanks in advance for your contributions.
This note is also available here in French with a little more context.
Crosspost: I also published this message on Reddit Svelte https://old.reddit.com/r/sveltejs/comments/1kxtz1u/custom_sveltekit_server_dev_production_with/?
Beta Was this translation helpful? Give feedback.
All reactions