@@ -165,6 +165,50 @@ function getWebpackPluginOptions(honeybadgerNextJsConfig: HoneybadgerNextJsConfi
165
165
}
166
166
}
167
167
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
+
168
212
export function setupHoneybadger ( config , honeybadgerNextJsConfig ?: HoneybadgerNextJsConfig ) {
169
213
if ( ! honeybadgerNextJsConfig ) {
170
214
honeybadgerNextJsConfig = {
@@ -175,6 +219,8 @@ export function setupHoneybadger(config, honeybadgerNextJsConfig?: HoneybadgerNe
175
219
176
220
_silent = honeybadgerNextJsConfig . silent ?? true
177
221
222
+ addServerExternalPackagesOption ( config )
223
+
178
224
return {
179
225
...config ,
180
226
webpack : mergeWithExistingWebpackConfig ( config . webpack , honeybadgerNextJsConfig )
0 commit comments