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

Reconfigure Honeybadger to upload and make use of source maps #235

Merged
merged 1 commit into from
Feb 10, 2023
Merged
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,6 @@ yarn-error.log*
*.tfvars
/terraform/.terraform
/terraform/*.plan

# ephemeral build artifacts
/lib/honeybadger/config.vars.js
27 changes: 21 additions & 6 deletions lib/honeybadger/config.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
import {
HONEYBADGER_API_KEY,
HONEYBADGER_ENV,
HONEYBADGER_REVISION,
} from "./config.vars.js";
import Honeybadger from "@honeybadger-io/js";

const setupHoneyBadger = () => {
// https://docs.honeybadger.io/lib/javascript/reference/configuration.html
const sharedHoneybadgerConfig = {
apiKey: process.env.HONEYBADGER_API_KEY,
environment: process.env.HONEYBADGER_ENV || process.env.NODE_ENV,
apiKey: HONEYBADGER_API_KEY,
environment: HONEYBADGER_ENV || process.env.NODE_ENV,
projectRoot: "webpack://_N_E/./",

// Uncomment to report errors in development:
reportData: true,

revision: process.env.AWS_COMMIT_ID,
revision: HONEYBADGER_REVISION,
};

if (typeof window === "undefined") {
// Node config
const projectRoot = process.cwd();
Honeybadger.configure({
...sharedHoneybadgerConfig,
projectRoot: "webpack:///./",
}).beforeNotify((notice) => {
notice.backtrace.forEach((line) => {
notice.backtrace = notice.backtrace.map((line) => {
if (line.file) {
line.file = line.file.replace(
`${projectRoot}/.next/server`,
`${process.env.HONEYBADGER_ASSETS_URL}/..`
`${process.env.NEXT_PUBLIC_DC_URL}/_next/..`
);
}
return line;
Expand All @@ -35,6 +40,16 @@ const setupHoneyBadger = () => {
Honeybadger.configure({
...sharedHoneybadgerConfig,
projectRoot: "webpack://_N_E/./",
}).beforeNotify((notice) => {
notice.backtrace = notice.backtrace.map((line) => {
if (line.file) {
line.file = line.file.replace(
/^.+\/_next\//,
`${process.env.NEXT_PUBLIC_DC_URL}/_next/`
);
}
return line;
});
});
}

Expand Down
26 changes: 19 additions & 7 deletions next.config.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
const fs = require("fs");
const HoneybadgerSourceMapPlugin = require("@honeybadger-io/webpack");

// Use the HoneybadgerSourceMapPlugin to upload the source maps during build step
const {
HONEYBADGER_API_KEY,
HONEYBADGER_ENV,
HONEYBADGER_ASSETS_URL,
HONEYBADGER_REPORT_DATA,
NEXT_PUBLIC_DC_URL,
} = process.env;
const NODE_ENV = process.env.HONEYBADGER_ENV || process.env.NODE_ENV;
const HONEYBADGER_REVISION = process.env.AWS_COMMIT_ID;
const HONEYBADGER_REVISION = process.env.HONEYBADGER_REVISION || process.env.AWS_COMMIT_ID;

const HoneybadgerConfig = JSON.stringify({
HONEYBADGER_API_KEY,
HONEYBADGER_ENV,
HONEYBADGER_REPORT_DATA,
HONEYBADGER_REVISION,
}, null, 2);

fs.writeFileSync(
"lib/honeybadger/config.vars.js",
`module.exports = ${HoneybadgerConfig};`
);

/** @type {import('next').NextConfig} */
module.exports = {
Expand All @@ -28,30 +41,29 @@ module.exports = {
],
},
reactStrictMode: true,
swcMinify: false,
swcMinify: true,
webpack: (config) => {
// When all the Honeybadger configuration env variables are
// available/configured The Honeybadger webpack plugin gets pushed to the
// webpack plugins to build and upload the source maps to Honeybadger.
// This is an alternative to manually uploading the source maps.
// See https://docs.honeybadger.io/lib/javascript/guides/using-source-maps.html
// Note: This is disabled in development mode.

if (
HONEYBADGER_API_KEY &&
HONEYBADGER_ASSETS_URL &&
NODE_ENV === "production"
(NODE_ENV === "production" || NODE_ENV === "staging")
) {
// `config.devtool` must be 'hidden-source-map' or 'source-map' to properly pass sourcemaps.
// Next.js uses regular `source-map` which doesnt pass its sourcemaps to Webpack.
// https://github.com/vercel/next.js/blob/89ec21ed686dd79a5770b5c669abaff8f55d8fef/packages/next/build/webpack/config/blocks/base.ts#L40
// Use the hidden-source-map option when you don't want the source maps to be
// publicly available on the servers, only to the error reporting
config.devtool = "hidden-source-map";

config.plugins.push(
new HoneybadgerSourceMapPlugin({
apiKey: HONEYBADGER_API_KEY,
assetsUrl: HONEYBADGER_ASSETS_URL,
assetsUrl: `${NEXT_PUBLIC_DC_URL}/_next`,
revision: HONEYBADGER_REVISION,
})
);
Expand Down