Guides

How to Integrate SnagRelay Widget into a Next.js Application

Add visual bug reporting to Next.js with the element, requests, session replay and error capture. Works with App Router and Pages Router in minutes.

How to Integrate SnagRelay Widget into a Next.js Application

Overview

Integrate SnagRelay into your Next.js application to capture bug reports with full context. The widget configuration is managed in your dashboard, so the code integration is minimal. This guide covers both the App Router (Next.js 13+) and the legacy Pages Router.

Prerequisites

  • A SnagRelay account (sign up free)
  • Your SnagRelay API key
  • A Next.js application (Next.js 13+ recommended)

App Router Integration (Recommended)

For Next.js 13+ with the App Router, add the widget in your root layout:

// app/layout.js
import Script from 'next/script';

export default function RootLayout({ children }) {
  return (
    <html>
      <body>
        {children}
        <Script id="snagrelay-init" strategy="afterInteractive">
          {`window.onSnagRelayLoad = function(api) { api.init(); };`}
        </Script>
        <Script
          src={`https://app.snagrelay.com/widget/load/${process.env.NEXT_PUBLIC_SNAGRELAY_KEY}?onload=onSnagRelayLoad`}
          strategy="afterInteractive"
        />
      </body>
    </html>
  );
}

Add your API key to .env.local:

NEXT_PUBLIC_SNAGRELAY_KEY=your_api_key_here

Pages Router Integration

For the legacy Pages Router, add the widget in _app.js:

// pages/_app.js
import { useEffect } from 'react';

function MyApp({ Component, pageProps }) {
  useEffect(() => {
    window.onSnagRelayLoad = function (api) {
      api.init();
    };
    
    const script = document.createElement('script');
    script.defer = true;
    script.src = `https://app.snagrelay.com/widget/load/${process.env.NEXT_PUBLIC_SNAGRELAY_KEY}?onload=onSnagRelayLoad`;
    document.head.appendChild(script);
  }, []);

  return <Component {...pageProps} />;
}

export default MyApp;

TypeScript Support

If you're using TypeScript, add the type declaration:

// types/snagrelay.d.ts
interface Window {
  onSnagRelayLoad: (api: { init: () => void }) => void;
}

Environment-Specific Loading

To only load the widget in staging or production (not local development):

// app/layout.js
import Script from 'next/script';

export default function RootLayout({ children }) {
  const isProd = process.env.NODE_ENV === 'production';
  
  return (
    <html>
      <body>
        {children}
        {isProd && (
          <>
            <Script id="snagrelay-init" strategy="afterInteractive">
              {`window.onSnagRelayLoad = function(api) { api.init(); };`}
            </Script>
            <Script
              src={`https://app.snagrelay.com/widget/load/${process.env.NEXT_PUBLIC_SNAGRELAY_KEY}?onload=onSnagRelayLoad`}
              strategy="afterInteractive"
            />
          </>
        )}
      </body>
    </html>
  );
}

That's It!

The widget configuration (position, colors, button text, session replay settings, etc.) is managed in your SnagRelay dashboard. Any changes you make there will automatically apply to your widget.

Best Practices

  • Use afterInteractive strategy — loads after the page is interactive, doesn't block rendering
  • Store key in environment variables — use NEXT_PUBLIC_SNAGRELAY_KEY so it's available client-side
  • Initialize once in root layout — don't add the widget in individual pages

Next Steps

  • Customize the widget in your dashboard
  • Set up integrations with your issue tracker
  • Configure team members and permissions

Further Reading

After install

What you get after install

Every report arrives in your inbox or tracker with the evidence already attached:

  • The element

    What the reporter pointed at: label, component and selector. Errors tied to it are outlined in red.

  • Console and JS errors

    With file and line, captured up to the moment of the report.

  • Network requests

    Status, URL and request and response bodies, failed and successful.

  • Session replay

    The moments leading up to the bug, with repro steps.

  • Environment

    URL, browser, OS and viewport, plus the screenshot. On paid plans, also video.

Optional

Connect your agent

Add the SnagRelay MCP server once, then paste a prompt that tells your agent to call get_issue_context. After you review the fix, mark_issue_resolved emails the reporter. MCP is on every plan.

https://mcp.snagrelay.com/v1

Add this URL as a remote MCP server, then click Allow. Paste a prompt that tells the agent to call get_issue_context, then mark_issue_resolved only after you have reviewed the fix.

More articles

For teams shipping with AI agents

Let your users find the bugs. Let your agent fix them.

Free to start, no card. One script tag, then your agent reads every report over MCP.

Start free