Skip to content

Conversation

@yangshun
Copy link
Contributor

@yangshun yangshun commented Oct 26, 2025

Main changes necessary to support deployment to Vercel:

  1. Put local devserver code within process.env.npm_lifecycle_event === 'dev' block so they don't run in a serverless function environment
  2. Add engineType = "client" to schema.prisma as this mode doesn't require the engine binaries, but you'll need to use a pg driver adapter. Docs: https://www.prisma.io/docs/orm/prisma-client/deployment/serverless/deploy-to-vercel
  3. Use tsbuild to build the app into a single file. Without building, there are weird TypeScript errors within @pothos-core when Vercel tries to run the raw entry file. Select dist as the output directory in the Vercel project
  4. Add dmmf: getDatamodel() to prisma options when initializing the Pothos SchemaBuilder. I believe this is needed because of the engineType = "client" change
  5. Add pnpm dev:setup as a postinstall command so that the generated files will be present
  6. As of now, Vercel does not support Node 24 yet, so lower the requirement to Node 22

While this PR works, it contains quite a bit of changes and you might have opinions on how they're done, so feel free to use it as a reference and implement it the way you like.

@yangshun yangshun changed the title Demo: how to support deployment to Vercel Demo: support serverless deployment (e.g. Vercel) Oct 26, 2025
],
prisma: {
client: prisma,
dmmf: getDatamodel(),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is safe to add regardless of deployment runtime. cc @hayes correct me if I'm wrong

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this do?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to https://pothos-graphql.dev/docs/plugins/prisma/setup#set-up-the-builder:

This give pothos information about your tables, relations, and indexes to help it generate optimal queries at runtime. This used to be attached to the prisma client, but has been removed in most runtimes/modes to reduce bundle size.

import { PrismaPg } from '@prisma/adapter-pg';
import { PrismaClient } from './prisma-client/client.ts';

const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL });
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

importFileExtension = "ts"
output = "./prisma-client"
provider = "prisma-client"
engineType = "client"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

src/index.tsx Outdated
Comment on lines 47 to 57
if (process.env.npm_lifecycle_event === 'dev') {
try {
await prisma.$connect();
} catch (error) {
// eslint-disable-next-line no-console
console.error(
`${styleText(['red', 'bold'], 'Prisma Database Connection Error')}\n`,
error,
);
process.exit(1);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting! This is actually purely for prod since you want to fail fast when the db is not available in any other hosting scenario.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Lemme move this out.

@cpojer
Copy link
Member

cpojer commented Oct 28, 2025

This is pretty great! I'm inclined to merge this (and would appreciate any details about deployment in the README too, since it's something people have asked for).

I'm a bit scared about switching to the new Prisma engine since when it first launched it was super broken. I have not migrated any projects to it yet, but I assume it is stable enough for new projects?

@yangshun
Copy link
Contributor Author

yangshun commented Oct 28, 2025

This is pretty great! I'm inclined to merge this (and would appreciate any details about deployment in the README too, since it's something people have asked for).

Sure, will add some guidance around deployment in the README

I'm a bit scared about switching to the new Prisma engine since when it first launched it was super broken. I have not migrated any projects to it yet, but I assume it is stable enough for new projects?

I haven't used the new engine for any serious projects yet, but it works for this repo at least (tested live on Vercel) 🤣

@yangshun yangshun marked this pull request as draft October 28, 2025 02:38
@yangshun yangshun marked this pull request as ready for review November 2, 2025 02:50
@yangshun yangshun changed the title Demo: support serverless deployment (e.g. Vercel) Support serverless deployment Nov 2, 2025
@yangshun
Copy link
Contributor Author

yangshun commented Nov 2, 2025

Updated the PR

Copy link
Member

@cpojer cpojer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thank you so much!

@cpojer cpojer merged commit abd8da8 into nkzw-tech:main Nov 2, 2025
1 check passed
@yangshun yangshun deleted the serverless branch November 2, 2025 11:55
@cpojer
Copy link
Member

cpojer commented Nov 2, 2025

I separated the app server from dev server here: 3a37572

@yangshun
Copy link
Contributor Author

yangshun commented Nov 2, 2025

I separated the app server from dev server here: 3a37572

Got it, it works well. I tested by deploying the latest commit here (you need to log in first)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants