Backend: Error Monitoring
Go
Java
JS
Python
Ruby
Backend: Logging
Go
JS
Python
Ruby
Java
Hosting Providers
Fullstack Frameworks
Overview
Self Host & Local Dev
Menu
Next.js App Router Api
Installation
npm install @highlight-run/next
API route instrumentation
Node.js
Each App Router route must be wrapped individually.
- Add
@highlight-run/node
toexperimental.serverComponentsExternalPackages
in yournext.config.js
.
// next.config.js const nextConfig = { experimental: { serverComponentsExternalPackages: ['@highlight-run/node'], }, } module.exports = nextConfig
- 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, })
- Wrap your
/app
functions withwithAppRouterHighlight
:
// 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') } })
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 withcurl
to watch it work.
curl http://localhost:3000/nodejs-app-router-test?error