Next.js Discord

Discord Forum

Execute a function at the launch of the app

Answered
Smaller yellow ant posted this in #help-forum
Open in Discord
Smaller yellow antOP
I would like to execute a function at the launch of my Next.js 13 app, a bit like a callback, and by "launch" I mean when I execute npm run dev or launch the server once the project in production.

More specifically I would want to connect to my MongoDB database right at the launch of the app.
Answered by aardani
No cant do, best you can do is set a singleton pattern which dedupes the function call ensuring you only run once per environment
View full answer

2 Replies

Answer
Smaller yellow antOP
That's what I did for now...