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

docs: Clarify how to use tRPC in Next.js Route Handlers #5728

Merged
merged 2 commits into from
May 26, 2024

Conversation

joulev
Copy link
Contributor

@joulev joulev commented May 19, 2024

Closes #N/A

🎯 Changes

Make the example code clearer on how to use tRPC in Next.js App Router Route Handlers.

Why?

The existing example code is already correct, however it could cause some confusion, since it uses the const x = y => z syntax (which is not immediately clear that the function returns z especially for JavaScript beginners).

In the Next.js Discord server, there has been at least one help question where the user tries to do this

import { fetchRequestHandler } from "@trpc/server/adapters/fetch";
import { appRouter } from "@/server";

function handler(req: Request) {
  fetchRequestHandler({
    endpoint: "/api/trpc",
    req,
    router: appRouter,
    createContext: () => ({}),
  });
}

export { handler as GET, handler as POST };

and it doesn't work because the returned value of fetchRequestHandler is not returned again.

This PR slightly improves the example code so that the need to re-return the value is made more apparent.

✅ Checklist

  • I have followed the steps listed in the Contributing guide.
  • If necessary, I have added documentation related to the changes made.
  • I have added or updated the tests related to the changes made.

@joulev joulev requested a review from a team as a code owner May 19, 2024 08:19
Copy link

vercel bot commented May 19, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
www ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 19, 2024 8:21am

Copy link

vercel bot commented May 19, 2024

@joulev is attempting to deploy a commit to the trpc Team on Vercel.

A member of the Team first needs to authorize it.

Copy link

This pull request has been locked because we are very unlikely to see comments on closed issues. If you think, this PR is still necessary, create a new one with the same branch. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 27, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants