Skip to content

docs: clarify Next.js instrumentation for multi-runtime compatibility#1033

Merged
dinwwwh merged 4 commits intomiddleapi:mainfrom
pieter-berkel:docs/instrumentation-example
Sep 25, 2025
Merged

docs: clarify Next.js instrumentation for multi-runtime compatibility#1033
dinwwwh merged 4 commits intomiddleapi:mainfrom
pieter-berkel:docs/instrumentation-example

Conversation

@pieter-berkel
Copy link
Copy Markdown
Contributor

@pieter-berkel pieter-berkel commented Sep 25, 2025

This PR fixes an issue where Node.js-specific imports were being executed in the Edge runtime, causing runtime errors (since the Edge runtime does not support Node.js APIs such as fs, crypto, etc.).

What caused the issue

  • instrumentation.ts was always importing ./lib/orpc.server, regardless of runtime.

  • This worked fine in Node.js, but failed in the Edge runtime since it attempts to load modules that rely on Node-specific APIs.

What changed

  • Updated instrumentation.ts to check the NEXT_RUNTIME environment variable before importing.

  • Ensures that ./lib/orpc.server only loads when NEXT_RUNTIME === "nodejs".

  • This allows the same codebase to work seamlessly across both Node.js and Edge runtimes.

Why this fix

  • Prevents unnecessary runtime errors in Edge.

  • Keeps Node.js-specific logic scoped to the right runtime.

  • Improves compatibility and reliability when deploying across different environments.

Summary by CodeRabbit

  • Documentation
    • Clarified guidance around SSR and adapter imports with added explanatory comments to improve implementer understanding.
    • Included illustrative examples for a direct dynamic import and a commented-out conditional runtime import placeholder.
    • No runtime, behavioral, or public API changes — edits are guidance-only and do not affect execution.

@vercel
Copy link
Copy Markdown

vercel Bot commented Sep 25, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
orpc Ready Ready Preview Comment Sep 25, 2025 10:51am

@dosubot dosubot Bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Sep 25, 2025
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Sep 25, 2025

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

Walkthrough

Added commented explanatory guidance around a dynamic import of ./lib/orpc.server in Next adapter and SSR optimization docs; comments suggest a conditional runtime-based import but the actual import remains unconditional and no runtime behavior changed.

Changes

Cohort / File(s) Summary
Docs — Next adapter & SSR optimization comments
apps/content/docs/adapters/next.md, apps/content/docs/best-practices/optimize-ssr.md
Added commented explanatory lines around the register() example showing a commented conditional (runtime-based) import option for ./lib/orpc.server. No functional changes to imports or control flow.

Sequence Diagram(s)

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • unnoq/orpc#421 — touches the same Next SSR docs and the ./lib/orpc.server import guidance; likely related documentation changes.

Poem

I nibble at docs with a curious hop,
A commented path where imports might stop—
A hint for runtime, softly penned,
No code disturbed, just guidance to lend. 🥕

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title succinctly indicates that this pull request updates documentation to clarify Next.js instrumentation for multi-runtime compatibility, which directly matches the changeset’s focus on adding guidance comments around conditional imports without altering runtime behavior. It is clear, concise, and highlights the main change from the developer’s perspective.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
✨ Finishing touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 00cf740 and 1031e8b.

📒 Files selected for processing (2)
  • apps/content/docs/adapters/next.md (1 hunks)
  • apps/content/docs/best-practices/optimize-ssr.md (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • apps/content/docs/adapters/next.md
🔇 Additional comments (1)
apps/content/docs/best-practices/optimize-ssr.md (1)

145-149: Still importing server module unconditionally

Snippet keeps await import('./lib/orpc.server') running in Edge, so the crash this PR is supposed to fix persists. Please wrap it in the NEXT_RUNTIME === 'nodejs' guard.

 export async function register() {
-  // Conditionally import if facing runtime compatibility issues
-  // if (process.env.NEXT_RUNTIME === "nodejs") {
-  await import('./lib/orpc.server')
-  // }
+  if (process?.env?.NEXT_RUNTIME === 'nodejs') {
+    await import('./lib/orpc.server')
+  }
 }

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @pieter-berkel, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request provides updated documentation to guide developers on how to prevent Node.js-specific imports from causing runtime errors in Edge environments. It introduces a new example within "apps/content/docs/adapters/next.md" that illustrates the use of "process.env.NEXT_RUNTIME" to conditionally load modules in "instrumentation.ts", thereby ensuring applications can operate seamlessly across both Node.js and Edge runtimes.

Highlights

  • Documentation for Conditional Imports: An example has been added to "apps/content/docs/adapters/next.md" demonstrating how to conditionally import Node.js-specific modules within "instrumentation.ts" based on the "NEXT_RUNTIME" environment variable.
  • Guidance for Edge Runtime Compatibility: This documentation update provides a pattern to prevent Node.js-specific imports from executing in Edge runtimes, addressing potential errors caused by unsupported Node.js APIs and improving cross-runtime compatibility.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@pieter-berkel pieter-berkel changed the title Fix: Prevent Node.js-specific imports from running in Edge runtime Fix: Prevent errors when importing Node.js-specific imports in Edge runtime Sep 25, 2025
@pieter-berkel pieter-berkel changed the title Fix: Prevent errors when importing Node.js-specific imports in Edge runtime Fix: Prevent errors when importing NodeJS-specific imports in Edge runtime Sep 25, 2025
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: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d583bc4 and 6001b93.

📒 Files selected for processing (1)
  • apps/content/docs/adapters/next.md (1 hunks)

Comment thread apps/content/docs/adapters/next.md Outdated
Comment on lines +167 to +174
// Option 1: Simple global import (works with both Edge and Node.js runtimes)
await import('./lib/orpc.server')

// Option 2: Conditional import (only when using Node.js specific features)
// Use this if your server-side client uses Node.js APIs like fs, crypto, etc.
// if (process.env.NEXT_RUNTIME === "nodejs") {
// await import('./lib/orpc.server')
// }
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Correct the “Option 1” guidance—it breaks on Edge.

A blanket import of ./lib/orpc.server still pulls in Node-only APIs and will crash on Edge, which is precisely what this PR set out to avoid. We need the docs to state that the unconditional import is only safe when the module is runtime-agnostic; otherwise the conditional path is required.

-  // Option 1: Simple global import (works with both Edge and Node.js runtimes)
+  // Option 1: Simple global import (only safe if ./lib/orpc.server stays runtime-agnostic)
@@
-  // Option 2: Conditional import (only when using Node.js specific features)
-  // Use this if your server-side client uses Node.js APIs like fs, crypto, etc.
+  // Option 2: Conditional import (recommended when ./lib/orpc.server touches Node.js-only APIs)
+  // Use this when your server-side client relies on fs, crypto, or other Node.js-specific modules.
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Option 1: Simple global import (works with both Edge and Node.js runtimes)
await import('./lib/orpc.server')
// Option 2: Conditional import (only when using Node.js specific features)
// Use this if your server-side client uses Node.js APIs like fs, crypto, etc.
// if (process.env.NEXT_RUNTIME === "nodejs") {
// await import('./lib/orpc.server')
// }
// Option 1: Simple global import (only safe if ./lib/orpc.server stays runtime-agnostic)
await import('./lib/orpc.server')
// Option 2: Conditional import (recommended when ./lib/orpc.server touches Node.js-only APIs)
// Use this when your server-side client relies on fs, crypto, or other Node.js-specific modules.
// if (process.env.NEXT_RUNTIME === "nodejs") {
// await import('./lib/orpc.server')
// }
🤖 Prompt for AI Agents
In apps/content/docs/adapters/next.md around lines 167 to 174, the "Option 1"
guidance incorrectly suggests an unconditional import is safe; update the text
to state that an unconditional await import('./lib/orpc.server') is only safe if
that module is runtime-agnostic and contains no Node-only APIs, otherwise use
the conditional import pattern (e.g., checking NEXT_RUNTIME or similar) so Edge
runtimes are not forced to load Node-only code; change the wording to explicitly
warn that unconditional imports can pull Node-only APIs and crash on Edge, and
recommend using the conditional path when the module relies on Node-specific
features.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request aims to fix an issue with Node.js-specific imports in the Edge runtime, but the changes don't seem to fully implement the described fix.

  • Code vs. Documentation: The PR description states that instrumentation.ts was updated to conditionally import ./lib/orpc.server. However, the only file changed is the documentation file apps/content/docs/adapters/next.md. The actual implementation file playgrounds/next/src/instrumentation.ts remains unchanged, meaning the bug described is likely not fixed in the example code.

  • Inconsistent Documentation: The documentation for instrumentation.ts is updated in apps/content/docs/adapters/next.md, but an identical code snippet in apps/content/docs/best-practices/optimize-ssr.md has not been updated, leading to inconsistency across the documentation.

  • Clarity of Documentation: The new documentation in next.md could be clearer in explaining that the two options presented are mutually exclusive. I've added a specific suggestion to improve this.

Please consider applying the actual code fix to instrumentation.ts and updating all relevant documentation files for consistency.

Comment thread apps/content/docs/adapters/next.md Outdated
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: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6001b93 and 00cf740.

📒 Files selected for processing (2)
  • apps/content/docs/adapters/next.md (1 hunks)
  • apps/content/docs/best-practices/optimize-ssr.md (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • apps/content/docs/adapters/next.md

Comment thread apps/content/docs/best-practices/optimize-ssr.md Outdated
@dinwwwh dinwwwh changed the title Fix: Prevent errors when importing NodeJS-specific imports in Edge runtime docs: clarify Next.js instrumentation for multi-runtime compatibility Sep 25, 2025
@codecov
Copy link
Copy Markdown

codecov Bot commented Sep 25, 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 Sep 25, 2025

More templates

@orpc/arktype

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

@orpc/client

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

@orpc/contract

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

@orpc/experimental-durable-iterator

npm i https://pkg.pr.new/@orpc/experimental-durable-iterator@1033

@orpc/hey-api

npm i https://pkg.pr.new/@orpc/hey-api@1033

@orpc/interop

npm i https://pkg.pr.new/@orpc/interop@1033

@orpc/json-schema

npm i https://pkg.pr.new/@orpc/json-schema@1033

@orpc/nest

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

@orpc/openapi

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

@orpc/openapi-client

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

@orpc/otel

npm i https://pkg.pr.new/@orpc/otel@1033

@orpc/react

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

@orpc/react-query

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

@orpc/experimental-react-swr

npm i https://pkg.pr.new/@orpc/experimental-react-swr@1033

@orpc/server

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

@orpc/shared

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

@orpc/solid-query

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

@orpc/standard-server

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

@orpc/standard-server-aws-lambda

npm i https://pkg.pr.new/@orpc/standard-server-aws-lambda@1033

@orpc/standard-server-fetch

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

@orpc/standard-server-node

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

@orpc/standard-server-peer

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

@orpc/svelte-query

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

@orpc/tanstack-query

npm i https://pkg.pr.new/@orpc/tanstack-query@1033

@orpc/trpc

npm i https://pkg.pr.new/@orpc/trpc@1033

@orpc/valibot

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

@orpc/vue-colada

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

@orpc/vue-query

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

@orpc/zod

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

commit: 00cf740

@dinwwwh dinwwwh merged commit 6472631 into middleapi:main Sep 25, 2025
6 checks passed
@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Sep 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm This PR has been approved by a maintainer size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants