Skip to content

docs: nextjs ssr#488

Merged
dinwwwh merged 7 commits intomainfrom
docs/nextjs-ssr
May 12, 2025
Merged

docs: nextjs ssr#488
dinwwwh merged 7 commits intomainfrom
docs/nextjs-ssr

Conversation

@dinwwwh
Copy link
Copy Markdown
Member

@dinwwwh dinwwwh commented May 12, 2025

Summary by CodeRabbit

  • New Features

    • Added comprehensive documentation for integrating oRPC with Next.js, including both App Router and Pages Router approaches and SSR optimization strategies.
    • Introduced support for the HTTP HEAD method in relevant API routes within the Next.js playground.
  • Bug Fixes

    • Updated directory and package naming from "nextjs" to "next" across documentation, playground, and configuration files to ensure consistency.
  • Documentation

    • Replaced and expanded the Next.js integration guide with a more detailed version.
    • Updated playground documentation and navigation links to reflect new directory naming.
    • Added a redirect from the old Next.js integration documentation URL to the new one.
  • Refactor

    • Improved SSR client setup and usage in the Next.js playground for optimized server-side rendering.
    • Updated client instantiation and React Query integration for better SSR compatibility.
  • Style

    • Switched to using React Suspense for loading states in paginated queries, streamlining UI code.

@vercel
Copy link
Copy Markdown

vercel Bot commented May 12, 2025

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

Name Status Preview Comments Updated (UTC)
orpc ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 12, 2025 4:10am

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 12, 2025

Walkthrough

This update refactors all references from "nextjs" to "next" in documentation, configuration, and playgrounds. It introduces a new, comprehensive Next.js integration guide, updates code to support optimized SSR with global client instances, adds HEAD method support to route handlers, and updates React Query usage to leverage Suspense.

Changes

File(s) Change Summary
CONTRIBUTING.md, apps/content/docs/playgrounds.md, playgrounds/next/package.json Updated directory and package references from "nextjs" to "next" in documentation and metadata.
apps/content/.vitepress/config.ts, apps/content/public/_redirects Changed sidebar link and added redirect from /docs/integrations/nextjs to /docs/integrations/next.
apps/content/docs/integrations/next.md Added new, detailed guide for integrating oRPC with Next.js, including SSR optimization and client/server setup.
apps/content/docs/integrations/nextjs.md Removed the old Next.js integration documentation file.
playgrounds/next/src/app/api/[[...rest]]/route.ts, playgrounds/next/src/app/rpc/[[...rest]]/route.ts Added export for HTTP HEAD method, handled by handleRequest.
playgrounds/next/src/app/layout.tsx Added import for server-only oRPC client setup.
playgrounds/next/src/app/orpc-query.tsx Switched from useInfiniteQuery to useSuspenseInfiniteQuery for React Query, removing manual loading UI.
playgrounds/next/src/lib/orpc.server.ts Introduced a server-only module that sets up a global oRPC client for SSR, providing context from headers.
playgrounds/next/src/lib/orpc.ts Refactored client initialization for SSR optimization, added global variable declaration, and updated exports.
playgrounds/next/src/playground-client.ts Changed import from orpcClient to client in the oRPC client usage.

Sequence Diagram(s)

sequenceDiagram
    participant Browser
    participant NextApp
    participant orpc.server.ts (SSR)
    participant orpc.ts (Client)
    participant oRPC Router

    Browser->>NextApp: Initial page request (SSR)
    NextApp->>orpc.server.ts (SSR): Import and create global client with context from headers
    orpc.server.ts (SSR)->>oRPC Router: Execute RPC call with context
    oRPC Router-->>orpc.server.ts (SSR): Return data
    orpc.server.ts (SSR)-->>NextApp: Provide data to render
    NextApp-->>Browser: Send rendered HTML

    Browser->>NextApp: Client-side navigation/RPC
    NextApp->>orpc.ts (Client): Import client (uses global if available, else creates new)
    orpc.ts (Client)->>oRPC Router: Execute RPC call
    oRPC Router-->>orpc.ts (Client): Return data
    orpc.ts (Client)-->>NextApp: Provide data to render
Loading

Possibly related PRs

  • unnoq/orpc#429: Introduced the original CONTRIBUTING.md; directly related as this PR updates the same file.
  • unnoq/orpc#421: Added SSR optimization guide and related code for Next.js integration, closely tied to the new documentation and SSR setup introduced in this PR.

Poem

In the warren of code, a new path we select,
From "nextjs" to "next", our docs now connect.
HEAD requests now hop with ease,
SSR flows with global keys.
Suspense brings a restful breeze—
Hooray for a playground that’s sure to please!
🐇✨

Tip

⚡️ Faster reviews with caching
  • CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.

Enjoy the performance boost—your workflow just got faster.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 12, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

📢 Thoughts on this report? Let us know!

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 12, 2025

More templates

@orpc/arktype

npm i https://pkg.pr.new/@orpc/arktype@488

@orpc/client

npm i https://pkg.pr.new/@orpc/client@488

@orpc/contract

npm i https://pkg.pr.new/@orpc/contract@488

@orpc/nest

npm i https://pkg.pr.new/@orpc/nest@488

@orpc/openapi

npm i https://pkg.pr.new/@orpc/openapi@488

@orpc/react

npm i https://pkg.pr.new/@orpc/react@488

@orpc/openapi-client

npm i https://pkg.pr.new/@orpc/openapi-client@488

@orpc/react-query

npm i https://pkg.pr.new/@orpc/react-query@488

@orpc/server

npm i https://pkg.pr.new/@orpc/server@488

@orpc/shared

npm i https://pkg.pr.new/@orpc/shared@488

@orpc/solid-query

npm i https://pkg.pr.new/@orpc/solid-query@488

@orpc/standard-server

npm i https://pkg.pr.new/@orpc/standard-server@488

@orpc/standard-server-fetch

npm i https://pkg.pr.new/@orpc/standard-server-fetch@488

@orpc/standard-server-node

npm i https://pkg.pr.new/@orpc/standard-server-node@488

@orpc/standard-server-peer

npm i https://pkg.pr.new/@orpc/standard-server-peer@488

@orpc/svelte-query

npm i https://pkg.pr.new/@orpc/svelte-query@488

@orpc/valibot

npm i https://pkg.pr.new/@orpc/valibot@488

@orpc/vue-colada

npm i https://pkg.pr.new/@orpc/vue-colada@488

@orpc/vue-query

npm i https://pkg.pr.new/@orpc/vue-query@488

@orpc/zod

npm i https://pkg.pr.new/@orpc/zod@488

commit: 2dc37ed

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (3)
playgrounds/next/src/app/layout.tsx (1)

1-1: Use aliased import for orpc.server.

For consistency with other imports (e.g., @/lib/orpc in your client code), consider replacing the relative path with the path alias:

- import '../lib/orpc.server'
+ import '@/lib/orpc.server'
apps/content/.vitepress/config.ts (1)

100-100: Update sidebar integration link for Next.js.

The sidebar entry link has been updated to /docs/integrations/next, matching the renamed documentation file. If desired, you may also update the displayed text from "Next.js" to "Next" for naming consistency.

apps/content/docs/integrations/next.md (1)

133-136: Grammar issue in SSR optimization introduction

There's a minor grammar error in this sentence.

-To reduce HTTP requests and improve latency during SSR, you can utilize a [Server-Side Client](/docs/client/server-side) during SSR. Below is a quick setup, see [Optimize SSR](/docs/best-practices/optimize-ssr) for a more details.
+To reduce HTTP requests and improve latency during SSR, you can utilize a [Server-Side Client](/docs/client/server-side) during SSR. Below is a quick setup, see [Optimize SSR](/docs/best-practices/optimize-ssr) for more details.
🧰 Tools
🪛 LanguageTool

[grammar] ~135-~135: The plural noun “details” cannot be used with the article “a”. Did you mean “a more detail” or “more details”?
Context: ...(/docs/best-practices/optimize-ssr) for a more details. ::: code-group ```ts [lib/orpc.ts] i...

(A_NNS)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 518cbe1 and 2dc37ed.

⛔ Files ignored due to path filters (7)
  • playgrounds/next/public/file.svg is excluded by !**/*.svg
  • playgrounds/next/public/globe.svg is excluded by !**/*.svg
  • playgrounds/next/public/next.svg is excluded by !**/*.svg
  • playgrounds/next/public/vercel.svg is excluded by !**/*.svg
  • playgrounds/next/public/window.svg is excluded by !**/*.svg
  • playgrounds/next/src/app/favicon.ico is excluded by !**/*.ico
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (14)
  • CONTRIBUTING.md (1 hunks)
  • apps/content/.vitepress/config.ts (1 hunks)
  • apps/content/docs/integrations/next.md (1 hunks)
  • apps/content/docs/integrations/nextjs.md (0 hunks)
  • apps/content/docs/playgrounds.md (2 hunks)
  • apps/content/public/_redirects (1 hunks)
  • playgrounds/next/package.json (1 hunks)
  • playgrounds/next/src/app/api/[[...rest]]/route.ts (1 hunks)
  • playgrounds/next/src/app/layout.tsx (1 hunks)
  • playgrounds/next/src/app/orpc-query.tsx (1 hunks)
  • playgrounds/next/src/app/rpc/[[...rest]]/route.ts (1 hunks)
  • playgrounds/next/src/lib/orpc.server.ts (1 hunks)
  • playgrounds/next/src/lib/orpc.ts (2 hunks)
  • playgrounds/next/src/playground-client.ts (1 hunks)
💤 Files with no reviewable changes (1)
  • apps/content/docs/integrations/nextjs.md
🧰 Additional context used
🧬 Code Graph Analysis (3)
playgrounds/next/src/app/api/[[...rest]]/route.ts (1)
playgrounds/next/src/app/rpc/[[...rest]]/route.ts (1)
  • HEAD (27-27)
playgrounds/next/src/app/rpc/[[...rest]]/route.ts (1)
playgrounds/next/src/app/api/[[...rest]]/route.ts (1)
  • HEAD (48-48)
playgrounds/next/src/lib/orpc.server.ts (1)
packages/server/src/router-client.ts (1)
  • createRouterClient (26-68)
🪛 LanguageTool
apps/content/docs/integrations/next.md

[grammar] ~135-~135: The plural noun “details” cannot be used with the article “a”. Did you mean “a more detail” or “more details”?
Context: ...(/docs/best-practices/optimize-ssr) for a more details. ::: code-group ```ts [lib/orpc.ts] i...

(A_NNS)

⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: publish-commit
  • GitHub Check: lint
🔇 Additional comments (18)
apps/content/public/_redirects (2)

3-3: Add /sponsor redirect rule.

This rule ensures visitors to /sponsor are redirected to the GitHub Sponsors page. Confirm that this path is not already covered and matches the site's expected URL structure.


4-4: Redirect legacy Next.js docs path.

Adds a 301 redirect from /docs/integrations/nextjs to /docs/integrations/next to preserve backward compatibility.

playgrounds/next/src/playground-client.ts (1)

1-1: Update ORPC client import.

The import now uses client from @/lib/orpc and aliases it to orpc, aligning with the refactored client export. The rest of the code correctly references orpc.

playgrounds/next/package.json (1)

2-2: Update package name to @orpc/next-playground.

Aligns the playground package name with the renamed directory and integration path. No functional impact.

CONTRIBUTING.md (1)

27-27: Path update looks good

The path change from "nextjs" to "next" aligns with the overall renaming approach in this PR.

playgrounds/next/src/app/api/[[...rest]]/route.ts (1)

48-48: Good addition of HEAD method support

Adding support for the HEAD HTTP method improves API compatibility and follows HTTP standards. The HEAD method allows clients to request metadata without the response body.

apps/content/docs/playgrounds.md (2)

15-15: Updated Next.js playground links correctly

The links to StackBlitz and GitHub source have been properly updated to point to the new "next" directory path.


32-32: Updated local development clone command

The degit command now correctly references the new "next" directory path for local development setup.

playgrounds/next/src/app/rpc/[[...rest]]/route.ts (1)

27-27: Good addition of HEAD method support

Adding support for the HEAD HTTP method maintains consistency with the API route handler and follows HTTP standards. This ensures all endpoints have uniform HTTP method support.

playgrounds/next/src/app/orpc-query.tsx (1)

4-4: Good modernization using Suspense for loading states

Great change switching from useInfiniteQuery to useSuspenseInfiniteQuery. This leverages React's Suspense feature to handle loading states, removing the need for manual loading state checks and simplifying the component code.

Note that you've correctly kept explicit error handling in place while delegating loading state management to Suspense.

Also applies to: 7-7

playgrounds/next/src/lib/orpc.server.ts (1)

1-23: Well-implemented SSR optimization pattern

This server-only module correctly sets up a global ORPC client instance for SSR optimization. The implementation:

  1. Properly uses the 'server only' directive
  2. Creates a reusable client with createRouterClient
  3. Provides a context function that safely retrieves headers
  4. Includes excellent documentation about context safety considerations

The comments are particularly valuable, clearly explaining that only globally safe context should be included since this instance is shared across all requests.

playgrounds/next/src/lib/orpc.ts (3)

8-15: Good type safety for global SSR optimization

Well-structured global type declaration that ensures TypeScript correctly understands the global client variable. The comments clearly explain the purpose of this setup and link to relevant documentation.


17-27: Clean link implementation

The link setup is clean and focused on its core responsibility without unnecessary complexity.


29-31: Elegant SSR fallback pattern

Great implementation of the conditional client assignment that smoothly falls back to a client-side client when the server-side global client is not available. This pattern effectively enables the SSR optimization while maintaining compatibility across environments.

apps/content/docs/integrations/next.md (4)

1-13: Excellent introduction and overview

This introduction provides a clear overview of oRPC integration with Next.js, covering both App Router and Pages Router approaches while linking to relevant external documentation.


14-89: Comprehensive server-side integration guidance

The server section thoroughly covers integration with both App Router and Pages Router paradigms. The inclusion of the HEAD method handler is particularly valuable for proper HTTP protocol support. The warning about disabling the Next.js body parser for Pages Router is an important detail that could save developers significant debugging time.


90-132: Clear client-side implementation patterns

Good explanation of the isomorphic function workaround needed for Next.js, with practical code examples. The global headers approach is a clean solution, and offering the alternative React Context approach shows flexibility.


137-190: Excellent SSR optimization example

The SSR optimization section provides a clear and practical implementation pattern that perfectly aligns with the code changes in the other files. The implementation details about context safety in a shared environment are particularly valuable, as they help prevent common pitfalls when optimizing for SSR.

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.

1 participant