@honeybadger-io/nextjs 5.11.6 → 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/README.md +5 -42
  2. package/dist/capture-request-error.d.ts +35 -0
  3. package/dist/capture-request-error.test.d.ts +2 -0
  4. package/dist/capture-router-transition-start.d.ts +21 -0
  5. package/dist/client.d.ts +2 -0
  6. package/dist/copy-config-files.js +106 -34
  7. package/dist/copy-config-files.js.map +1 -1
  8. package/dist/edge.d.ts +2 -1
  9. package/dist/flush.d.ts +24 -0
  10. package/dist/honeybadger-nextjs-client.cjs.js +35 -0
  11. package/dist/honeybadger-nextjs-client.cjs.js.map +1 -0
  12. package/dist/honeybadger-nextjs-client.esm.js +27 -0
  13. package/dist/honeybadger-nextjs-client.esm.js.map +1 -0
  14. package/dist/honeybadger-nextjs-edge.cjs.js +320 -240
  15. package/dist/honeybadger-nextjs-edge.cjs.js.map +1 -1
  16. package/dist/honeybadger-nextjs-edge.esm.js +317 -240
  17. package/dist/honeybadger-nextjs-edge.esm.js.map +1 -1
  18. package/dist/honeybadger-nextjs.cjs.js +835 -387
  19. package/dist/honeybadger-nextjs.cjs.js.map +1 -1
  20. package/dist/honeybadger-nextjs.esm.js +829 -386
  21. package/dist/honeybadger-nextjs.esm.js.map +1 -1
  22. package/dist/index.d.ts +5 -2
  23. package/dist/insights-instrumentation.d.ts +30 -2
  24. package/dist/insights.d.ts +72 -0
  25. package/dist/insights.test.d.ts +2 -0
  26. package/dist/setup.d.ts +11 -0
  27. package/dist/setup.test.d.ts +2 -0
  28. package/dist/source-maps.d.ts +55 -0
  29. package/dist/source-maps.test.d.ts +2 -0
  30. package/dist/types.d.ts +20 -8
  31. package/package.json +33 -11
  32. package/src/capture-request-error.test.ts +245 -0
  33. package/src/capture-request-error.ts +92 -0
  34. package/src/capture-router-transition-start.ts +33 -0
  35. package/src/client.ts +4 -0
  36. package/src/copy-config-files.test.ts +250 -16
  37. package/src/copy-config-files.ts +117 -34
  38. package/src/edge.ts +12 -1
  39. package/src/flush.ts +58 -0
  40. package/src/index.ts +7 -2
  41. package/src/insights-instrumentation.ts +104 -11
  42. package/src/insights.test.ts +192 -0
  43. package/src/insights.ts +225 -0
  44. package/src/setup.test.ts +214 -0
  45. package/src/setup.ts +172 -0
  46. package/src/source-maps.test.ts +803 -0
  47. package/src/source-maps.ts +515 -0
  48. package/src/types.ts +38 -21
  49. package/templates/_error.js +14 -10
  50. package/templates/_error_app_router.js +1 -1
  51. package/templates/_error_app_router.tsx +1 -1
  52. package/templates/honeybadger.server.config.js +5 -16
  53. package/templates/instrumentation-client.js +21 -0
  54. package/templates/instrumentation.js +29 -0
  55. package/dist/webpack.d.ts +0 -3
  56. package/dist/with-honeybadger.d.ts +0 -32
  57. package/dist/with-honeybadger.test.d.ts +0 -2
  58. package/src/webpack.ts +0 -228
  59. package/src/with-honeybadger.test.ts +0 -527
  60. package/src/with-honeybadger.ts +0 -286
  61. package/templates/honeybadger.browser.config.js +0 -13
  62. package/templates/honeybadger.edge.config.js +0 -13
@@ -0,0 +1,21 @@
1
+ import Honeybadger from '@honeybadger-io/js'
2
+ import { captureRouterTransitionStart } from '@honeybadger-io/nextjs'
3
+
4
+ export const config = {
5
+ apiKey: process.env.NEXT_PUBLIC_HONEYBADGER_API_KEY,
6
+ environment: process.env.NEXT_PUBLIC_VERCEL_ENV || process.env.VERCEL_ENV || process.env.NODE_ENV,
7
+ revision: process.env.NEXT_PUBLIC_HONEYBADGER_REVISION,
8
+ // debug: true,
9
+ // reportData: true,
10
+ }
11
+
12
+ // This file runs after the document loads but before React hydrates, so the client is
13
+ // instrumented before any component code can throw.
14
+ Honeybadger.configure(config)
15
+ Honeybadger.logger.debug('Honeybadger configured for browser')
16
+
17
+ /**
18
+ * Records App Router navigations as breadcrumbs, giving faults a trail of the routes the
19
+ * user visited before the error.
20
+ */
21
+ export const onRouterTransitionStart = captureRouterTransitionStart
@@ -0,0 +1,29 @@
1
+ import { captureRequestError } from '@honeybadger-io/nextjs'
2
+ // Uncomment for Insights request events — see register() below.
3
+ // import { registerHoneybadgerInsights } from '@honeybadger-io/nextjs'
4
+
5
+ /**
6
+ * Runs once per server instance, before any request is handled. This is how Honeybadger
7
+ * is configured, under both Turbopack and webpack.
8
+ */
9
+ export async function register() {
10
+ await import('./honeybadger.server.config')
11
+
12
+ // Optional: send a `request.handled` Insights event for every request, including
13
+ // successful ones. Next.js emits the request data as OpenTelemetry spans, so this needs:
14
+ //
15
+ // npm install @vercel/otel @opentelemetry/api
16
+ //
17
+ // Then uncomment the import at the top of this file and the call below.
18
+ //
19
+ // await registerHoneybadgerInsights()
20
+ //
21
+ // If you already call registerOTel() yourself, add honeybadgerSpanProcessor() to its
22
+ // spanProcessors instead — registering OpenTelemetry twice competes for the provider.
23
+ }
24
+
25
+ /**
26
+ * Reports server errors to Honeybadger. Covers Server Components, Route Handlers,
27
+ * Server Actions, middleware and API routes.
28
+ */
29
+ export const onRequestError = captureRequestError
package/dist/webpack.d.ts DELETED
@@ -1,3 +0,0 @@
1
- import { HoneybadgerNextJsConfig } from './types';
2
- export declare function setupHoneybadger(config: any, honeybadgerNextJsConfig?: HoneybadgerNextJsConfig): any;
3
- //# sourceMappingURL=webpack.d.ts.map
@@ -1,32 +0,0 @@
1
- import Honeybadger from '@honeybadger-io/js';
2
- import { NextRequest, NextResponse } from 'next/server';
3
- import type { NextApiRequest, NextApiResponse } from 'next';
4
- type AppRouterHandler = (req: NextRequest | Request, ...args: unknown[]) => Promise<NextResponse>;
5
- type PagesApiHandler = (req: NextApiRequest, res: NextApiResponse, ...args: unknown[]) => unknown;
6
- /**
7
- * Wraps a handler function with Honeybadger error reporting. Works with App
8
- * Router route handlers, middleware, and Pages Router API routes.
9
- *
10
- * `request_id` / `correlation_id` are read from the `x-request-id` /
11
- * `request-id` and `x-correlation-id` / `x-amzn-trace-id` headers (generated
12
- * when absent). When `insights: { enabled: true, http: true }` is configured,
13
- * a `request.handled` event carrying the ids plus method, path, status and
14
- * duration is emitted per request.
15
- *
16
- * On the Node.js runtime each invocation additionally runs inside
17
- * `Honeybadger.run(...)`, so context is isolated per request and the ids are
18
- * seeded onto the event context — merged onto every event emitted during the
19
- * request, including programmatic `Honeybadger.event(...)` calls. On the edge
20
- * runtime (browser build, single global store) seeding the shared event
21
- * context would leak ids between concurrent requests, so programmatic events
22
- * there don't inherit them.
23
- *
24
- * The webpack config-file auto-injection (`honeybadger.*.config.js`) doesn't
25
- * reach API routes or edge middleware, so pass `config` to configure
26
- * Honeybadger explicitly there. It's ignored if Honeybadger is already
27
- * configured (e.g. by the auto-injected file).
28
- */
29
- export declare function withHoneybadger(handler: AppRouterHandler, config?: Parameters<typeof Honeybadger.configure>[0]): AppRouterHandler;
30
- export declare function withHoneybadger(handler: PagesApiHandler, config?: Parameters<typeof Honeybadger.configure>[0]): PagesApiHandler;
31
- export {};
32
- //# sourceMappingURL=with-honeybadger.d.ts.map
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=with-honeybadger.test.d.ts.map
package/src/webpack.ts DELETED
@@ -1,228 +0,0 @@
1
- import fs from 'fs'
2
- import path from 'path'
3
- import HoneybadgerSourceMapPlugin from '@honeybadger-io/webpack'
4
- import type { WebpackConfigContext } from 'next/dist/server/config-shared'
5
- import { HoneybadgerNextJsConfig, NextJsRuntime, HoneybadgerWebpackPluginOptions } from './types'
6
-
7
- const URL_DOCS_SOURCE_MAPS_UPLOAD = 'https://docs.honeybadger.io/lib/javascript/integration/nextjs/#source-map-upload-and-tracking-deploys'
8
- let _silent = true
9
- function log(type: 'error' | 'warn' | 'debug', msg: string): void {
10
- if (['error', 'warn'].includes(type) || !_silent) {
11
- console[type]('[HoneybadgerNextJs]', msg)
12
- }
13
- }
14
-
15
- function shouldUploadSourceMaps(honeybadgerNextJsConfig: HoneybadgerNextJsConfig, context: WebpackConfigContext): boolean {
16
- const { dev } = context
17
-
18
- if (honeybadgerNextJsConfig.disableSourceMapUpload) {
19
- return false
20
- }
21
-
22
- if (!honeybadgerNextJsConfig.webpackPluginOptions || !honeybadgerNextJsConfig.webpackPluginOptions.apiKey) {
23
- log('warn', `skipping source map upload; here's how to enable: ${URL_DOCS_SOURCE_MAPS_UPLOAD}`)
24
- return false
25
- }
26
-
27
- if (dev || process.env.NODE_ENV === 'development') {
28
- return false
29
- }
30
-
31
- return true
32
- }
33
-
34
- function mergeWithExistingWebpackConfig(nextJsWebpackConfig, honeybadgerNextJsConfig: HoneybadgerNextJsConfig) {
35
- return function webpackFunctionMergedWithHb(webpackConfig, context: WebpackConfigContext) {
36
-
37
- const { isServer, dir: projectDir, nextRuntime } = context
38
- const configType = isServer ? (nextRuntime === 'edge' ? 'edge' : 'server') : 'browser'
39
- log('debug', `reached webpackFunctionMergedWithHb isServer[${isServer}] configType[${configType}]`)
40
-
41
- let result = { ...webpackConfig }
42
- if (typeof nextJsWebpackConfig === 'function') {
43
- result = nextJsWebpackConfig(result, context)
44
- }
45
-
46
- const originalEntry = result.entry
47
- result.entry = async () => injectHoneybadgerConfigToEntry(originalEntry, projectDir, configType)
48
-
49
- if (shouldUploadSourceMaps(honeybadgerNextJsConfig, context)) {
50
- // `result.devtool` must be 'hidden-source-map' or 'source-map' to properly pass sourcemaps.
51
- // Next.js uses regular `source-map` which doesnt pass its sourcemaps to Webpack.
52
- // https://github.com/vercel/next.js/blob/89ec21ed686dd79a5770b5c669abaff8f55d8fef/packages/next/build/webpack/config/blocks/base.ts#L40
53
- // Use the hidden-source-map option when you don't want the source maps to be
54
- // publicly available on the servers, only to the error reporting
55
- result.devtool = 'hidden-source-map'
56
- if (!result.plugins) {
57
- result.plugins = []
58
- }
59
- const options = getWebpackPluginOptions(honeybadgerNextJsConfig)
60
- if (options) {
61
- result.plugins.push(new HoneybadgerSourceMapPlugin(options))
62
- }
63
- }
64
-
65
- return result
66
- }
67
- }
68
-
69
- async function injectHoneybadgerConfigToEntry(originalEntry, projectDir: string, configType: NextJsRuntime) {
70
- const result = typeof originalEntry === 'function' ? await originalEntry() : { ...originalEntry }
71
- const hbConfigFile = getHoneybadgerConfigFile(projectDir, configType)
72
- if (!hbConfigFile) {
73
- return result
74
- }
75
-
76
- const hbConfigFileRelativePath = `./${hbConfigFile}`
77
- if (!Object.keys(result).length) {
78
- log('debug', `no entry points for configType[${configType}]`)
79
- }
80
- for (const entryName in result) {
81
- addHoneybadgerConfigToEntry(result, entryName, hbConfigFileRelativePath, configType)
82
- }
83
-
84
- return result
85
- }
86
-
87
- function addHoneybadgerConfigToEntry(entry, entryName: string, hbConfigFile: string, configType: NextJsRuntime) {
88
-
89
- log('debug', `adding entry[${entryName}] to configType[${configType}]`)
90
-
91
- switch (configType) {
92
- case 'server':
93
- if (!entryName.startsWith('pages/')) {
94
- return
95
- }
96
-
97
- break
98
- case 'browser':
99
- if (!['pages/_app', 'main-app'].includes(entryName)) {
100
- return
101
- }
102
-
103
- break
104
- case 'edge':
105
- // nothing?
106
-
107
- break
108
- }
109
-
110
- const currentEntryPoint = entry[entryName]
111
- let newEntryPoint = currentEntryPoint
112
-
113
- if (typeof currentEntryPoint === 'string') {
114
- newEntryPoint = [hbConfigFile, currentEntryPoint]
115
- } else if (Array.isArray(currentEntryPoint)) {
116
- newEntryPoint = [hbConfigFile, ...currentEntryPoint]
117
- } // descriptor object (webpack 5+)
118
- else if (typeof currentEntryPoint === 'object' && currentEntryPoint && 'import' in currentEntryPoint) {
119
- const currentImportValue = currentEntryPoint['import']
120
- const newImportValue = [hbConfigFile]
121
- if (typeof currentImportValue === 'string') {
122
- newImportValue.push(currentImportValue)
123
- } else {
124
- newImportValue.push(...(currentImportValue))
125
- }
126
- newEntryPoint = {
127
- ...currentEntryPoint,
128
- import: newImportValue,
129
- };
130
- } else {
131
- log('error', 'Could not inject Honeybadger config to entry point: ' + JSON.stringify(currentEntryPoint, null, 2))
132
- }
133
-
134
- entry[entryName] = newEntryPoint
135
- }
136
-
137
- function getHoneybadgerConfigFile(projectDir: string, configType: NextJsRuntime): string | null {
138
- const possibilities = [`honeybadger.${configType}.config.ts`, `honeybadger.${configType}.config.js`]
139
-
140
- for (const filename of possibilities) {
141
- if (fs.existsSync(path.resolve(projectDir, filename))) {
142
- return filename;
143
- }
144
- }
145
-
146
- log('debug', `could not find config file in ${projectDir} for ${configType}`)
147
- return null
148
- }
149
-
150
- function getWebpackPluginOptions(honeybadgerNextJsConfig: HoneybadgerNextJsConfig): HoneybadgerWebpackPluginOptions | null {
151
- const apiKey = honeybadgerNextJsConfig.webpackPluginOptions?.apiKey || process.env.NEXT_PUBLIC_HONEYBADGER_API_KEY
152
- const assetsUrl = honeybadgerNextJsConfig.webpackPluginOptions?.assetsUrl || process.env.NEXT_PUBLIC_HONEYBADGER_ASSETS_URL
153
- if (!apiKey || !assetsUrl) {
154
- log('error', 'Missing Honeybadger required configuration for webpack plugin. Source maps will not be uploaded to Honeybadger.')
155
-
156
- return null
157
- }
158
-
159
- return {
160
- ...honeybadgerNextJsConfig.webpackPluginOptions,
161
- apiKey,
162
- assetsUrl,
163
- revision: honeybadgerNextJsConfig.webpackPluginOptions?.revision || process.env.NEXT_PUBLIC_HONEYBADGER_REVISION,
164
- silent: _silent,
165
- }
166
- }
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) || +nextJsVersion[0] > 14) {
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
-
212
- export function setupHoneybadger(config, honeybadgerNextJsConfig?: HoneybadgerNextJsConfig) {
213
- if (!honeybadgerNextJsConfig) {
214
- honeybadgerNextJsConfig = {
215
- silent: true,
216
- disableSourceMapUpload: false,
217
- }
218
- }
219
-
220
- _silent = honeybadgerNextJsConfig.silent ?? true
221
-
222
- addServerExternalPackagesOption(config)
223
-
224
- return {
225
- ...config,
226
- webpack: mergeWithExistingWebpackConfig(config.webpack, honeybadgerNextJsConfig)
227
- }
228
- }