Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Next.js example to v13 #1958

Open
wants to merge 5 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 18 additions & 22 deletions examples/js/nextjs/lib/bugsnag.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
import { PHASE_PRODUCTION_BUILD } from 'next/constants'
import Bugsnag from '@bugsnag/js'

export function start() {
// next.js executes top-level code at build time. See https://github.com/vercel/next.js/discussions/16840 for further example
// So use NEXT_PHASE to avoid Bugsnag.start being executed during the build phase
// See https://nextjs.org/docs/api-reference/next.config.js/introduction and https://github.com/vercel/next.js/blob/canary/packages/next/shared/lib/constants.ts#L1-L5 for
// more details on NEXT_PHASE
if (process.env.NEXT_PHASE !== "phase-production-build") {
if (process.env.NEXT_IS_SERVER === true) {
Bugsnag.start({
apiKey: process.env.NEXT_PUBLIC_BUGSNAG_API_KEY,
appVersion: process.env.NEXT_BUILD_ID,
// @bugsnag/plugin-aws-lambda must only be imported on the server
plugins: [require('@bugsnag/plugin-aws-lambda')]
})
} else {
// If preferred two separate Bugsnag projects e.g. a javascript and a node project could be used rather than a single one
Bugsnag.start({
apiKey: process.env.NEXT_PUBLIC_BUGSNAG_API_KEY,
appVersion: process.env.NEXT_BUILD_ID,
plugins: []
})
};
// Next.js executes top-level code at build time, so use NEXT_PHASE to avoid Bugsnag.start being executed during the build phase
if (process.env.NEXT_PHASE === PHASE_PRODUCTION_BUILD) return

if (typeof window === 'undefined') {
Bugsnag.start({
apiKey: process.env.NEXT_PUBLIC_BUGSNAG_API_KEY,
appVersion: process.env.NEXT_BUILD_ID,
// @bugsnag/plugin-aws-lambda must only be imported on the server
plugins: [require('@bugsnag/plugin-aws-lambda')],
})
} else {
// If preferred two separate Bugsnag projects e.g. a javascript and a node project could be used rather than a single one
Bugsnag.start({
apiKey: process.env.NEXT_PUBLIC_BUGSNAG_API_KEY,
appVersion: process.env.NEXT_BUILD_ID,
plugins: [],
})
}
}

export function getServerlessHandler() {
return Bugsnag.getPlugin('awsLambda').createHandler()
}


19 changes: 7 additions & 12 deletions examples/js/nextjs/next.config.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
const {
BugsnagSourceMapUploaderPlugin,
BugsnagBuildReporterPlugin,
} = require('webpack-bugsnag-plugins');
} = require('webpack-bugsnag-plugins')

module.exports = {
/** @type {import('next').NextConfig} */
const nextConfig = {
productionBrowserSourceMaps: true,
webpack(config, { buildId, isServer, webpack }) {
webpack(config, { buildId, webpack }) {
config.plugins.push(
new webpack.DefinePlugin({
// Define the build id so that it can be accessed in the client when reporting errors
'process.env.NEXT_BUILD_ID': JSON.stringify(buildId),
'process.env.NEXT_IS_SERVER': JSON.stringify(isServer),
})
}),
)

// Avoid including '@bugsnag/plugin-aws-lambda' module in the client side bundle
// See https://arunoda.me/blog/ssr-and-server-only-modules
if (!isServer) {
config.plugins.push(new webpack.IgnorePlugin({resourceRegExp: /@bugsnag\/plugin-aws-lambda/}));
}

// Upload source maps on production build
if (
Expand All @@ -45,3 +38,5 @@ module.exports = {
return config
},
}

module.exports = nextConfig
Loading