Skip to content

Commit 5d73daf

Browse files
authored
fix(nextjs): add honeybadger in external packages (#1352)
1 parent 1554425 commit 5d73daf

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

packages/nextjs/src/index.ts

+46
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,50 @@ function getWebpackPluginOptions(honeybadgerNextJsConfig: HoneybadgerNextJsConfi
165165
}
166166
}
167167

168+
function getNextJsVersionInstalled(): [major: string, minor: string, patch: string] | null {
169+
try {
170+
return require('next/package.json').version?.split('.')
171+
} catch (e) {
172+
return null
173+
}
174+
}
175+
176+
/**
177+
* NextJs will report a warning if the `serverExternalPackages` option is not present.
178+
* This is because @honeybadger-io/js will try to require configuration files dynamically (https://github.com/honeybadger-io/honeybadger-js/pull/1268).
179+
*
180+
* First reported here: https://github.com/honeybadger-io/honeybadger-js/issues/1351
181+
*/
182+
function addServerExternalPackagesOption(config) {
183+
// this should be available in the upcoming version of Next.js (14.3.0)
184+
if (config.serverExternalPackages && Array.isArray(config.serverExternalPackages)) {
185+
log('debug', 'adding @honeybadger-io/js to serverExternalPackages')
186+
config.serverExternalPackages.push('@honeybadger-io/js')
187+
return
188+
}
189+
190+
if (config.experimental?.serverComponentsExternalPackages && Array.isArray(config.experimental?.serverComponentsExternalPackages)) {
191+
log('debug', 'adding @honeybadger-io/js to experimental.serverComponentsExternalPackages')
192+
config.experimental.serverComponentsExternalPackages.push('@honeybadger-io/js')
193+
return
194+
}
195+
196+
const nextJsVersion = getNextJsVersionInstalled();
197+
if (nextJsVersion) {
198+
if (+nextJsVersion[0] >= 14 && +nextJsVersion[1] >= 3) {
199+
log('debug', 'adding serverExternalPackages option with value ["@honeybadger-io/js"]')
200+
config.serverExternalPackages = ['@honeybadger-io/js']
201+
}
202+
else {
203+
log('debug', 'adding experimental.serverComponentsExternalPackages option with value ["@honeybadger-io/js"]')
204+
if (!config.experimental) {
205+
config.experimental = {}
206+
}
207+
config.experimental.serverComponentsExternalPackages = ['@honeybadger-io/js']
208+
}
209+
}
210+
}
211+
168212
export function setupHoneybadger(config, honeybadgerNextJsConfig?: HoneybadgerNextJsConfig) {
169213
if (!honeybadgerNextJsConfig) {
170214
honeybadgerNextJsConfig = {
@@ -175,6 +219,8 @@ export function setupHoneybadger(config, honeybadgerNextJsConfig?: HoneybadgerNe
175219

176220
_silent = honeybadgerNextJsConfig.silent ?? true
177221

222+
addServerExternalPackagesOption(config)
223+
178224
return {
179225
...config,
180226
webpack: mergeWithExistingWebpackConfig(config.webpack, honeybadgerNextJsConfig)

0 commit comments

Comments
 (0)