initialize / main file for server side operations like db connections #53955
Replies: 3 comments 3 replies
-
There's this experimental feature: https://nextjs.org/docs/app/building-your-application/optimizing/instrumentation I am not sure if it is meant to be used to start-up DB connections though. |
Beta Was this translation helpful? Give feedback.
-
Some serverless environments like Google Cloud Functions (v2) now allow handling multiple concurrent requests within the same function instance. See https://cloud.google.com/functions/docs/concepts/version-comparison#new-in-2nd-gen This is a description of the problem I'm facing (making it tooggable to prevent spamming the thread)
I'm finding it extremely difficult to work with NextJS as a full-stack framework. What I'm doing might be a bit niche, but I want to embed a GraphQL server into NextJS (via an app route) and then configure the GraphQL client to perform a standard HTTP request when CSR and an in-memory request when SSR. Both Apollo Client and URQL support this using the It gets tricky to configure the GraphQL client for the server environment without completely breaking the SSR of the page or leaking server-side code into the browser bundle. Both of these occur when the server requires some async bootstrapping, e.g. creating a DB connection, and when dynamically importing your executable schema (to prevent the code being bundled into the browser) which itself returns a Promise. Another approach I tried was to wrap the client in a server-action, but again this can only return a Promise. This forces creating the Graphql client to become async which means you have to use Suspense or conditional rendering deep within your component tree (i.e. in the root layout) while you wait for the promise to resolve. This basically means the SSR render always shows the loading state and you would then have to use streaming SSR on all of your pages to get any use at all out of SSR. It seems excessively difficult to separate logic within a component that can only run on the server. Data fetching isn't the only use case where you want to have server-only code. In this case, the NextJS server has some dependencies that need to be initialised before the rendering can start (when server-side). The two use cases that need to be addressed to make SSR feasible are:
The former use case seems like it would be solved using the instrumentation hook previously mentioned, but it doesn't seem like the NextJS pages can import anything initialised in that file. I've seen this same problem when trying to share dependencies across Jest test suites because they run in isolation in the event loop. I guess the former isn't easy to solve as it basically requires React to allow you to wait for the promise to resolve before rendering anything (like it would be on a traditional server). Of course, this increases latency, but that seems better in some cases than being forced to stream the content after the dependencies are available. |
Beta Was this translation helpful? Give feedback.
-
The It's a bit annoying to have to use 2 experimental features, but it does work. We have to flag all the server-only modules using the |
Beta Was this translation helpful? Give feedback.
-
Goals
Non-Goals
Background
it's annoying to call db connection every http request (unless your code is serverless which makes sense
Proposal
create a file to initialize or add a way to do so in the next.config or add an entity file called "server.ts" ? that perform server side actions only.
another option is to create a pre/post files for layout for instance for actions before rendering start
Beta Was this translation helpful? Give feedback.
All reactions