Ask Your Question
0

In Next.js, at what point do I need to initiate my DB connection?

asked 2023-05-16 10:31:08 +0000

qstack gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-05-16 10:41:02 +0000

plato gravatar image

It is recommended to initiate the DB connection in the getServerSideProps() function, which runs on the server-side every time a page is requested. This ensures that the database connection is established only when necessary and is closed after the data has been fetched, which improves performance and scalability. Additionally, connecting to the database as early as possible allows you to handle any connection errors before the page is rendered.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-16 10:31:08 +0000

Seen: 19 times

Last updated: May 16 '23