Star us on GitHub
Star
Menu

Next.js App Router Api

Installation
npm install @highlight-run/next
Copy
API route instrumentation

Node.js

Each App Router route must be wrapped individually.

  • Add @highlight-run/node to experimental.serverComponentsExternalPackages in your next.config.js.
// next.config.js const nextConfig = { experimental: { serverComponentsExternalPackages: ['@highlight-run/node'], }, } module.exports = nextConfig
Copy
  • Create a file to export your AppRouterHighlight wrapper function:
// utils/app-router-highlight.config.ts: import { AppRouterHighlight } from '@highlight-run/next/server' import { CONSTANTS } from '../constants' export const withAppRouterHighlight = AppRouterHighlight({ projectID: CONSTANTS.NEXT_PUBLIC_HIGHLIGHT_PROJECT_ID, })
Copy
  • Wrap your /app functions with withAppRouterHighlight:
// app/nodejs-app-router-test/route.ts import { NextRequest } from 'next/server' import { withAppRouterHighlight } from '../../utils/app-router-highlight.config' export const GET = withAppRouterHighlight(async function GET(request: NextRequest) { console.info('Here: app/nodejs-app-router-test/route.ts') if (request.url?.includes('error')) { throw new Error('Error: app/nodejs-app-router-test (App Router)') } else { return new Response('Success: app/nodejs-app-router-test') } })
Copy
Validation
  • Run your app in dev mode with npm run dev.
  • Copy/paste the above code snippet into /app/api/nodejs-app-router-test.ts and hit the endpoint in your browser or with curl to watch it work.
curl http://localhost:3000/nodejs-app-router-test?error
Copy