Star us on GitHub
Star
Menu

Next.js Page Router Api

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

This section applies to Next.js Page Router routes only. Each Page Router route must be wrapped individually.

  • Create a file to export your PageRouterHighlight wrapper function:
// utils/page-router-highlight.config.ts: import { CONSTANTS } from '../constants' import { PageRouterHighlight } from '@highlight-run/next/server' export const withPageRouterHighlight = PageRouterHighlight({ projectID: CONSTANTS.NEXT_PUBLIC_HIGHLIGHT_PROJECT_ID, })
Copy
  • Wrap your /pages/api functions with withPageRouterHighlight:
// pages/api/nodejs-page-router-test.ts import { NextApiRequest, NextApiResponse } from 'next' import { withPageRouterHighlight } from '../../utils/page-router-highlight.config' export default withPageRouterHighlight(function handler(req: NextApiRequest, res: NextApiResponse) { console.info('Here: pages/api/nodejs-page-router-test.ts') if (req.url?.includes('error')) { throw new Error('Error: pages/api/nodejs-page-router-test.ts') } else { res.send('Success: pages/api/nodejs-page-router-test.ts') } })
Copy
Validation
  • Run your app in dev mode with npm run dev.
  • Copy/paste the above code snippet into /pages/api/nodejs-page-router-test.ts and hit the endpoint in your browser or with curl to watch it work.
curl http://localhost:3000/api/nodejs-page-router-test?error
Copy