Conversation
request handle() called nextApp.prepare() on every request. Since Next 13.4.15 that is no longer a no-op in production: each call re-runs the router server initialization, leaking a process-level error listener and rebuilding the render server, until the instance is OOM-killed. Prepare once at module load and await the shared promise, matching the custom server example in the Next.js docs and the existing express, nuxt3 and sveltekit adapters: https://nextjs.org/docs/pages/guides/custom-server Fixes #574
Contributor
There was a problem hiding this comment.
Code Review
This pull request extracts the nextApp.prepare() call to the module's top level to avoid preparing the Next.js app on every request. However, executing this heavy initialization at module load time can cause deployment failures and slow down cold starts in Firebase/Cloud Functions. The reviewer suggested lazily initializing the preparation promise on the first request instead.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
handle()calledawait nextApp.prepare()on every request. Since Next 13.4.15 that is no longer a no-op in production: each call re-runs the router server initialization, leaking a process-level error listener and rebuilding the render server, until the instance runs out of memory.Fixes #574.
Fix
Prepare once at module load and await the shared promise — matching the documented custom server example and the existing
express,nuxt3andsveltekitadapters.Verification
Deployed to Cloud Run at 512MiB. Next 14.2.35, 501 routes, 1500 requests.
Before: listeners grew by one per request, memory grew 0.47 MB per request, and the container was killed twice for exceeding its memory limit.
After: listeners flat at 3, memory flat at ~144 MB, zero restarts.