docker deployed nextjs: where to put application bootstrap/startup code?
Unanswered
Sloth bear posted this in #help-forum
Sloth bearOP
Doing things like
- validate server variables from process.env using zod
- initialize server side api client
- start background task (fetch open tasks from db and process)
at runtime
- validate server variables from process.env using zod
- initialize server side api client
- start background task (fetch open tasks from db and process)
at runtime
16 Replies
are you trying to deploy nextjs with docker or what
Sloth bearOP
app is deployed, container is started, the app starts up and
- should validate dynamic configuration from process.env
- initiate db/service connections
- do some basic readiness/liveness checks
- should validate dynamic configuration from process.env
- initiate db/service connections
- do some basic readiness/liveness checks
the whole container is also monitored by k8s , so a health check endpoint might be useful
also I need to implement some backgroud tasks
e.g. user triggers action from frontend which should send email
email service is down, but the user should not be bothered
the email sending/retrying should be a background task, so I need persistence (postgres or something) and a way to execut this "check email tasks" logic independent from browser requests
e.g. user triggers action from frontend which should send email
email service is down, but the user should not be bothered
the email sending/retrying should be a background task, so I need persistence (postgres or something) and a way to execut this "check email tasks" logic independent from browser requests
of course the k8s thingy probes the application anyway, so maybe I can use this to trigger some job logic
@gin are you trying to deploy nextjs with docker or what
Sloth bearOP
well yes that's what I meant with the title, the app is running as a container using k8s
Orangetailed potter wasp
also I need to implement some backgroud tasksyou can use cronjob for that, i think its the
node-cron packageSloth bearOP
I have now written some useConfig wrapper function that allows me to lazily parse configuration once, but it's just one issue solved 😄
@Sloth bear well yes that's what I meant with the title, the app is running as a container using k8s
looks like u need some type of webhook or similar that will be called on your Docker startup
and have that power your services
or restart them
@Orangetailed potter wasp > also I need to implement some backgroud tasks
you can use cronjob for that, i think its the `node-cron` package
Sloth bearOP
sounds good , will try that. I hope it kicks of automatically or at least once k8s probes the nextjs http server
is there no "run this on startup" instead of "run this on request" type of thing in nextjs?
I guess I can hack something together, just wondering if I miss something (very fresh with nextjs, but used to react and sveltekit)
i dont think so but if u have experience with k8s it should be a matter of seconds to setup cron / webhooks or similar