Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Next.js #533

Merged
merged 1 commit into from
Mar 14, 2023
Merged

Add support for Next.js #533

merged 1 commit into from
Mar 14, 2023

Conversation

timostamm
Copy link
Member

This adds basic support for Next.js via the new package @bufbuild/connect-next. It passed our tests, but still needs to be added to our test suite.

The setup includes creating two files:

  1. connect.ts is where you register your RPCs:
// connect.ts
import { ConnectRouter } from "@bufbuild/connect";

export default function(router: ConnectRouter) {
  // implement rpc Say(SayRequest) returns (SayResponse)
  router.rpc(ElizaService, ElizaService.methods.say, async (req) => ({
    sentence: `you said: ${req.sentence}`,
  }));
}
  1. pages/api/[[..connect]].ts is a Next.js catch-all API route:
// pages/api/[[..connect]].ts
import { nextJsApiRouter } from "@bufbuild/connect-next";
import routes from "../../connect";

const {handler, config} = nextJsApiRouter({ routes });
export {handler as default, config};

With that server running, you can make requests with any Connect or gRPC-Web client.
Note that Next.js serves all your RPCs with the /api prefix.

Currently, @bufbuild/connect-next does not support the Vercel Edge runtime. It requires the Node.js server runtime, which is used by default when deploying to Vercel.

@timostamm timostamm requested a review from smaye81 March 14, 2023 21:23
@timostamm timostamm merged commit 17444ea into main Mar 14, 2023
@timostamm timostamm deleted the tstamm/next-js branch March 14, 2023 21:57
This was referenced Mar 15, 2023
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