Next.js
AppSignal for Node.js 3.0.0 | This integration requires AppSignal for Node.js 3.0.0 or higher. |
Next.js 9.3.0 | This integration requires Next.js 9.3.0 or higher. |
The AppSignal integration for Next.js.
It is recommended to be used with @appsignal/javascript
and @appsignal/react
on the client-side for full-stack performance monitoring and error tracking.
At this time, it's only possible to use this integration with a custom server script. The integration does not work when using the Next CLI (e.g. next start
).
If you plan to use this in a serverless environment, we recommend using just @appsignal/javascript
and the @appsignal/react
integration.
Installation
When using a custom server script, Next.js is instrumented automatically by the AppSignal for Node.js package.
Usage with Express
You must add express
as a dependency to your project. Then, create a server.js
in your project root and add the following:
import { expressErrorHandler } from "@appsignal/nodejs";
import express from "express";
import { parse } from "url";
import next from "next";
// or: const { expressErrorHandler } = require("@appsignal/nodejs");
// or: const express = require("express");
// or: const { parse } = require("url");
// or: const next = require("next");
const dev = process.env.NODE_ENV !== "production";
const app = next({ dev });
const handle = app.getRequestHandler();
const port = parseInt(process.env.PORT, 10) || 3000;
app
.prepare()
.then(() => {
express()
.use(handle)
.use(expressErrorHandler())
.listen(port, () => {
console.log(`> Ready on http://localhost:${port}`);
});
})
.catch((err) => {
console.error(err);
process.exit(1);
});
Make sure to update your package.json
to use the custom server and require the AppSignal configuration file:
"scripts": {
"dev": "node server.js",
"build": "next build",
"start": "NODE_ENV=production node --require ./appsignal.cjs server.js"
}
Features
The Next.js integration will send AppSignal a trace for each request.
If the custom server uses Express, any errors raised when handling the request will be reported to AppSignal.