Manual Installation: Next.js
Relevant source files
The following files were used as context for generating this wiki page:react-grab is a developer tool that allows you to select UI elements in a React application and get their corresponding component stack, file paths, and source code. This is primarily intended to improve the context provided to AI coding agents.
While the recommended way to install react-grab is via the automated CLI command npx grab@latest init, manual installation is also a straightforward process for Next.js projects. This guide covers the steps for both the App Router and the Pages Router. The core principle is to inject the react-grab script into the <head> of your application only during development.
Sources: README.md:18-22, packages/cli/src/commands/init.ts:110-112
Installation Methods Overview
The following diagram illustrates the decision path for installing react-grab in a Next.js project.
This diagram shows the two main installation paths: the automated CLI and the manual method, which branches based on the Next.js router being used.
Sources: packages/cli/src/commands/init.ts:110, packages/react-grab/README.md:27-61
Core Installation in Next.js
For Next.js applications, the next/script component is the preferred method for adding third-party scripts. The react-grab script should be included conditionally, only when process.env.NODE_ENV is 'development'.
The script is loaded from the unpkg CDN. The strategy="beforeInteractive" prop is crucial as it ensures react-grab is loaded and ready before the React application becomes interactive.
Sources: packages/react-grab/README.md:31-38, packages/website/components/install-tabs.tsx:75-86
Next.js App Router
For projects using the App Router, modify the root layout file located at app/layout.tsx. Import the Script component from next/script and place it within the <head> tag.
// packages/react-grab/README.md:29-43
import Script from "next/script";
export default function RootLayout({ children }) {
return (
<html>
<head>
{/* put this in the <head> */}
{process.env.NODE_ENV === "development" && (
<Script
src="//unpkg.com/react-grab/dist/index.global.js"
crossOrigin="anonymous"
strategy="beforeInteractive"
/>
)}
</head>
<body>{children}</body>
</html>
);
}
Sources: packages/react-grab/README.md:29-43, packages/website/components/install-tabs.tsx:70-90
Next.js Pages Router
For projects using the Pages Router, the setup is similar but occurs in the pages/_document.tsx file. This file allows you to augment the <html> and <body> tags used to render a Page.
// packages/react-grab/README.md:47-61
import { Html, Head, Main, NextScript } from "next/document";
export default function Document() {
return (
<Html lang="en">
<Head>
{/* put this in the <Head> */}
{process.env.NODE_ENV === "development" && (
<Script
src="//unpkg.com/react-grab/dist/index.global.js"
crossOrigin="anonymous"
strategy="beforeInteractive"
/>
)}
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
Sources: packages/react-grab/README.md:47-61, packages/website/components/install-tabs.tsx:97-118
Agent Integration
react-grab can integrate directly with various AI coding agents, enabling a workflow where selecting a UI element can trigger an agent to make code changes automatically. This requires adding a second, agent-specific client script. This script is also loaded from unpkg but should use the strategy="lazyOnload" to avoid blocking page load.
The following sequence diagram shows the interaction when using an agent provider.
This diagram illustrates the data flow from a user interaction in the browser to the local agent server and back.
Sources: packages/website/app/blog/agent/page.tsx:430-459
Example: App Router with Claude Code
To integrate an agent like Claude Code, you add its client script alongside the main react-grab script in app/layout.tsx.
// packages/website/app/blog/agent/page.tsx:210-229
import Script from "next/script";
export default function RootLayout({ children }) {
return (
<html>
<head>
{process.env.NODE_ENV === "development" && (
<>
<Script
src="//unpkg.com/react-grab/dist/index.global.js"
strategy="beforeInteractive"
/>
<Script
src="//unpkg.com/@react-grab/claude-code/dist/client.global.js"
strategy="lazyOnload"
/>
</>
)}
</head>
<body>{children}</body>
</html>
);
}
Sources: packages/website/app/blog/agent/page.tsx:210-229, packages/react-grab/README.md:100-121
Available Agent Providers
Each agent integration requires a client-side script and a server-side command that runs alongside your development server.
| Agent | Client Script URL | package.json dev script |
|---|---|---|
| Claude Code | //unpkg.com/@react-grab/claude-code/dist/client.global.js | npx @react-grab/claude-code@latest && next dev |
| Cursor CLI | //unpkg.com/@react-grab/cursor/dist/client.global.js | npx @react-grab/cursor@latest && next dev |
| OpenCode | //unpkg.com/@react-grab/opencode/dist/client.global.js | npx @react-grab/opencode@latest && next dev |
| Codex | //unpkg.com/@react-grab/codex/dist/client.global.js | npx @react-grab/codex@latest && next dev |
| Gemini | //unpkg.com/@react-grab/gemini/dist/client.global.js | npx @react-grab/gemini@latest && next dev |
| Amp | //unpkg.com/@react-grab/amp/dist/client.global.js | npx @react-grab/amp@latest && next dev |
| Factory Droid | //unpkg.com/@react-grab/droid/dist/client.global.js | npx @react-grab/droid@latest && next dev |
Sources: packages/react-grab/README.md:92-308, packages/website/app/blog/agent/page.tsx:191-428
Summary
Manually installing react-grab in a Next.js project involves adding a single Script component to either app/layout.tsx (App Router) or pages/_document.tsx (Pages Router). The script should only be loaded in the development environment to enable component inspection. For a more powerful workflow, optional agent integrations can be added by including an additional client script and updating the development server command.