fix: prevent Prisma from being bundled in client-side code #567
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.
Changes made:
Pages updated:
Why this fix is needed:
Next.js creates two bundles for each page - server (SSR) and client (navigation). Top-level imports are included in BOTH bundles. When users click Link components for client-side navigation, Next.js loads the client bundle which includes Prisma, causing 'PrismaClient is unable to be run in the browser' runtime errors.
This issue didn’t surface consistently because Prisma only causes errors when a page’s client bundle is loaded. Many navigation flows in this project triggered full page reloads, meaning the server bundle ran and Prisma stayed server-side.
The bug only appears when Next.js loads the client-side version of the page — which happens during:
In this codebase, many dashboard and admin pages were usually reached via tags or manual URL navigation, which forced full reloads and hid the problem. As soon as users navigated with components or the app was tested in production mode, Next.js loaded the client bundle containing Prisma, causing the runtime error.
Dynamic importing Prisma inside getServerSideProps ensures Prisma is never included in client bundles, fixing the issue.
Checklist:
Update index.md)Not associated with an issue.