T
TanStack5mo ago
like-gold

Server Start or Init functions

I plan to deploy via docker on our own infra (aws) and I'd like to run some setup script which loads secrets from a vault before the server actually starts (in next.js I used the instrumentation.ts file) - what would be the equivalent or best practice for this in "Start"?
15 Replies
genetic-orange
genetic-orange5mo ago
you can create a nitro.config.ts file at the root of your project with this content
export default defineNitroConfig({
plugins: ['plugins/startup.ts'],
})
export default defineNitroConfig({
plugins: ['plugins/startup.ts'],
})
and a plugins/startup.ts file with this content
export default async () => {
// do your thing
}
export default async () => {
// do your thing
}
watch out though: it's NOT blocking, it runs in parallel to the server starting, not before. currently i dont think there is any way to run something before the server starts
like-gold
like-goldOP5mo ago
thanks @notKamui - I'll try it out.
deep-jade
deep-jade3mo ago
@notKamui I'm assuming you are still using this in production? Does it still work?
genetic-orange
genetic-orange3mo ago
it does but the plugin is only registered for prod bundle not dev you have to run manually for dev ^
deep-jade
deep-jade3mo ago
@notKamui I'm assuming you are not running the latest version right? From what I've read they have or are about to remove nitro from TSS
genetic-orange
genetic-orange3mo ago
i they are about to but not yet but yes, this is only temporary i plan on just using bun serve when nitro is yeeted you have another solution ? @sh03
deep-jade
deep-jade3mo ago
Unfortunately no. Wdym by using bun serve? That you'll ditch TSS and just use bun serve or that you'll use it with TSS somehow? @notKamui ^
deep-jade
deep-jade3mo ago
Just a heads up btw: bun has some memory leak issues in production (when I tried it it would keep filling up the RAM and the container would die). See: https://github.com/oven-sh/bun/issues/16339
GitHub
Memory leak since miggration from node 20 to bun 1.1.43 of a nextjs...
What version of Bun is running? 1.1.43 What platform is your computer? oven/bun:1.1.43-alpine docker image What steps can reproduce the bug? I recently today performed a migration from node 20 to b...
deep-jade
deep-jade3mo ago
AFAIK they haven't solved the root cause yet
genetic-orange
genetic-orange3mo ago
i mean once nitro is gone, it's up to you to chose the server for TSS. people have mentioned bun serve, express, or still nitro
deep-jade
deep-jade3mo ago
Ah didn't know that. I thought they were implementing their own server to replace nitro
genetic-orange
genetic-orange3mo ago
the memleak is insane tho
deep-jade
deep-jade3mo ago
Personally I'm never gonna touch bun again (even though I strongly agree with their philosophy) but it's up to you really. Just sharing war time stories
genetic-orange
genetic-orange3mo ago
btw i can confirm this still works as of 1.131.50 this may not work for 1.132.0-alpha.x it's not perfect anyway, because the start process is not awaitable meaning if you want to setup everything explicitely before starting the server (like, db migrations), well, there is technically a time window where the server is up in an unwanted state which is why, hopefully, we will have more control over the server
deep-jade
deep-jade3mo ago
@notKamui I see, thanks 👍

Did you find this page helpful?