@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.
- package/README.md +5 -42
- package/dist/capture-request-error.d.ts +35 -0
- package/dist/capture-request-error.test.d.ts +2 -0
- package/dist/capture-router-transition-start.d.ts +21 -0
- package/dist/client.d.ts +2 -0
- package/dist/copy-config-files.js +106 -34
- package/dist/copy-config-files.js.map +1 -1
- package/dist/edge.d.ts +2 -1
- package/dist/flush.d.ts +24 -0
- package/dist/honeybadger-nextjs-client.cjs.js +35 -0
- package/dist/honeybadger-nextjs-client.cjs.js.map +1 -0
- package/dist/honeybadger-nextjs-client.esm.js +27 -0
- package/dist/honeybadger-nextjs-client.esm.js.map +1 -0
- package/dist/honeybadger-nextjs-edge.cjs.js +320 -240
- package/dist/honeybadger-nextjs-edge.cjs.js.map +1 -1
- package/dist/honeybadger-nextjs-edge.esm.js +317 -240
- package/dist/honeybadger-nextjs-edge.esm.js.map +1 -1
- package/dist/honeybadger-nextjs.cjs.js +835 -387
- package/dist/honeybadger-nextjs.cjs.js.map +1 -1
- package/dist/honeybadger-nextjs.esm.js +829 -386
- package/dist/honeybadger-nextjs.esm.js.map +1 -1
- package/dist/index.d.ts +5 -2
- package/dist/insights-instrumentation.d.ts +30 -2
- package/dist/insights.d.ts +72 -0
- package/dist/insights.test.d.ts +2 -0
- package/dist/setup.d.ts +11 -0
- package/dist/setup.test.d.ts +2 -0
- package/dist/source-maps.d.ts +55 -0
- package/dist/source-maps.test.d.ts +2 -0
- package/dist/types.d.ts +20 -8
- package/package.json +33 -11
- package/src/capture-request-error.test.ts +245 -0
- package/src/capture-request-error.ts +92 -0
- package/src/capture-router-transition-start.ts +33 -0
- package/src/client.ts +4 -0
- package/src/copy-config-files.test.ts +250 -16
- package/src/copy-config-files.ts +117 -34
- package/src/edge.ts +12 -1
- package/src/flush.ts +58 -0
- package/src/index.ts +7 -2
- package/src/insights-instrumentation.ts +104 -11
- package/src/insights.test.ts +192 -0
- package/src/insights.ts +225 -0
- package/src/setup.test.ts +214 -0
- package/src/setup.ts +172 -0
- package/src/source-maps.test.ts +803 -0
- package/src/source-maps.ts +515 -0
- package/src/types.ts +38 -21
- package/templates/_error.js +14 -10
- package/templates/_error_app_router.js +1 -1
- package/templates/_error_app_router.tsx +1 -1
- package/templates/honeybadger.server.config.js +5 -16
- package/templates/instrumentation-client.js +21 -0
- package/templates/instrumentation.js +29 -0
- package/dist/webpack.d.ts +0 -3
- package/dist/with-honeybadger.d.ts +0 -32
- package/dist/with-honeybadger.test.d.ts +0 -2
- package/src/webpack.ts +0 -228
- package/src/with-honeybadger.test.ts +0 -527
- package/src/with-honeybadger.ts +0 -286
- package/templates/honeybadger.browser.config.js +0 -13
- package/templates/honeybadger.edge.config.js +0 -13
|
@@ -27,8 +27,11 @@ describe('copy-config-files', () => {
|
|
|
27
27
|
|
|
28
28
|
await copyConfigFiles()
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
expect(fs.existsSync('honeybadger.
|
|
30
|
+
// replaced by instrumentation-client, which runs before hydration
|
|
31
|
+
expect(fs.existsSync('honeybadger.browser.config.js')).toBe(false)
|
|
32
|
+
expect(fs.existsSync('instrumentation.js')).toBe(true)
|
|
33
|
+
expect(fs.existsSync('instrumentation-client.js')).toBe(true)
|
|
34
|
+
expect(fs.existsSync('honeybadger.edge.config.js')).toBe(false)
|
|
32
35
|
expect(fs.existsSync('honeybadger.server.config.js')).toBe(true)
|
|
33
36
|
expect(fs.existsSync('pages/_error.js')).toBe(true)
|
|
34
37
|
expect(fs.existsSync('app/error.js')).toBe(false)
|
|
@@ -44,8 +47,11 @@ describe('copy-config-files', () => {
|
|
|
44
47
|
|
|
45
48
|
await copyConfigFiles()
|
|
46
49
|
|
|
47
|
-
|
|
48
|
-
expect(fs.existsSync('honeybadger.
|
|
50
|
+
// replaced by instrumentation-client, which runs before hydration
|
|
51
|
+
expect(fs.existsSync('honeybadger.browser.config.js')).toBe(false)
|
|
52
|
+
expect(fs.existsSync('instrumentation.js')).toBe(true)
|
|
53
|
+
expect(fs.existsSync('instrumentation-client.js')).toBe(true)
|
|
54
|
+
expect(fs.existsSync('honeybadger.edge.config.js')).toBe(false)
|
|
49
55
|
expect(fs.existsSync('honeybadger.server.config.js')).toBe(true)
|
|
50
56
|
expect(fs.existsSync('app/error.js')).toBe(true)
|
|
51
57
|
expect(fs.existsSync('app/global-error.js')).toBe(true)
|
|
@@ -64,8 +70,11 @@ describe('copy-config-files', () => {
|
|
|
64
70
|
|
|
65
71
|
await copyConfigFiles()
|
|
66
72
|
|
|
67
|
-
|
|
68
|
-
expect(fs.existsSync('honeybadger.
|
|
73
|
+
// replaced by instrumentation-client, which runs before hydration
|
|
74
|
+
expect(fs.existsSync('honeybadger.browser.config.js')).toBe(false)
|
|
75
|
+
expect(fs.existsSync('src/instrumentation.js')).toBe(true)
|
|
76
|
+
expect(fs.existsSync('src/instrumentation-client.js')).toBe(true)
|
|
77
|
+
expect(fs.existsSync('honeybadger.edge.config.js')).toBe(false)
|
|
69
78
|
expect(fs.existsSync('honeybadger.server.config.js')).toBe(true)
|
|
70
79
|
expect(fs.existsSync('src/pages/_error.js')).toBe(true)
|
|
71
80
|
expect(fs.existsSync('pages/_error.js')).toBe(false)
|
|
@@ -83,8 +92,11 @@ describe('copy-config-files', () => {
|
|
|
83
92
|
|
|
84
93
|
await copyConfigFiles()
|
|
85
94
|
|
|
86
|
-
|
|
87
|
-
expect(fs.existsSync('honeybadger.
|
|
95
|
+
// replaced by instrumentation-client, which runs before hydration
|
|
96
|
+
expect(fs.existsSync('honeybadger.browser.config.js')).toBe(false)
|
|
97
|
+
expect(fs.existsSync('src/instrumentation.js')).toBe(true)
|
|
98
|
+
expect(fs.existsSync('src/instrumentation-client.js')).toBe(true)
|
|
99
|
+
expect(fs.existsSync('honeybadger.edge.config.js')).toBe(false)
|
|
88
100
|
expect(fs.existsSync('honeybadger.server.config.js')).toBe(true)
|
|
89
101
|
expect(fs.existsSync('src/app/error.js')).toBe(true)
|
|
90
102
|
expect(fs.existsSync('src/app/global-error.js')).toBe(true)
|
|
@@ -103,8 +115,11 @@ describe('copy-config-files', () => {
|
|
|
103
115
|
|
|
104
116
|
await copyConfigFiles()
|
|
105
117
|
|
|
106
|
-
|
|
107
|
-
expect(fs.existsSync('honeybadger.
|
|
118
|
+
// replaced by instrumentation-client, which runs before hydration
|
|
119
|
+
expect(fs.existsSync('honeybadger.browser.config.js')).toBe(false)
|
|
120
|
+
expect(fs.existsSync('instrumentation.ts')).toBe(true)
|
|
121
|
+
expect(fs.existsSync('instrumentation-client.ts')).toBe(true)
|
|
122
|
+
expect(fs.existsSync('honeybadger.edge.config.js')).toBe(false)
|
|
108
123
|
expect(fs.existsSync('honeybadger.server.config.js')).toBe(true)
|
|
109
124
|
expect(fs.existsSync('pages/_error.tsx')).toBe(true)
|
|
110
125
|
expect(fs.existsSync('pages/_error.js')).toBe(false)
|
|
@@ -122,8 +137,11 @@ describe('copy-config-files', () => {
|
|
|
122
137
|
|
|
123
138
|
await copyConfigFiles()
|
|
124
139
|
|
|
125
|
-
|
|
126
|
-
expect(fs.existsSync('honeybadger.
|
|
140
|
+
// replaced by instrumentation-client, which runs before hydration
|
|
141
|
+
expect(fs.existsSync('honeybadger.browser.config.js')).toBe(false)
|
|
142
|
+
expect(fs.existsSync('instrumentation.ts')).toBe(true)
|
|
143
|
+
expect(fs.existsSync('instrumentation-client.ts')).toBe(true)
|
|
144
|
+
expect(fs.existsSync('honeybadger.edge.config.js')).toBe(false)
|
|
127
145
|
expect(fs.existsSync('honeybadger.server.config.js')).toBe(true)
|
|
128
146
|
expect(fs.existsSync('app/error.tsx')).toBe(true)
|
|
129
147
|
expect(fs.existsSync('app/global-error.tsx')).toBe(true)
|
|
@@ -145,8 +163,11 @@ describe('copy-config-files', () => {
|
|
|
145
163
|
|
|
146
164
|
await copyConfigFiles()
|
|
147
165
|
|
|
148
|
-
|
|
149
|
-
expect(fs.existsSync('honeybadger.
|
|
166
|
+
// replaced by instrumentation-client, which runs before hydration
|
|
167
|
+
expect(fs.existsSync('honeybadger.browser.config.js')).toBe(false)
|
|
168
|
+
expect(fs.existsSync('src/instrumentation.ts')).toBe(true)
|
|
169
|
+
expect(fs.existsSync('src/instrumentation-client.ts')).toBe(true)
|
|
170
|
+
expect(fs.existsSync('honeybadger.edge.config.js')).toBe(false)
|
|
150
171
|
expect(fs.existsSync('honeybadger.server.config.js')).toBe(true)
|
|
151
172
|
expect(fs.existsSync('src/pages/_error.tsx')).toBe(true)
|
|
152
173
|
expect(fs.existsSync('src/pages/_error.js')).toBe(false)
|
|
@@ -167,8 +188,11 @@ describe('copy-config-files', () => {
|
|
|
167
188
|
|
|
168
189
|
await copyConfigFiles()
|
|
169
190
|
|
|
170
|
-
|
|
171
|
-
expect(fs.existsSync('honeybadger.
|
|
191
|
+
// replaced by instrumentation-client, which runs before hydration
|
|
192
|
+
expect(fs.existsSync('honeybadger.browser.config.js')).toBe(false)
|
|
193
|
+
expect(fs.existsSync('src/instrumentation.ts')).toBe(true)
|
|
194
|
+
expect(fs.existsSync('src/instrumentation-client.ts')).toBe(true)
|
|
195
|
+
expect(fs.existsSync('honeybadger.edge.config.js')).toBe(false)
|
|
172
196
|
expect(fs.existsSync('honeybadger.server.config.js')).toBe(true)
|
|
173
197
|
expect(fs.existsSync('src/app/error.tsx')).toBe(true)
|
|
174
198
|
expect(fs.existsSync('src/app/global-error.tsx')).toBe(true)
|
|
@@ -178,4 +202,214 @@ describe('copy-config-files', () => {
|
|
|
178
202
|
expect(fs.existsSync('app/error.js')).toBe(false)
|
|
179
203
|
})
|
|
180
204
|
|
|
205
|
+
it('should point the instrumentation import at the project root when using a src folder', async () => {
|
|
206
|
+
mock({
|
|
207
|
+
'templates': mock.load(path.resolve(__dirname, '..', 'templates')),
|
|
208
|
+
'src': {
|
|
209
|
+
'app': {
|
|
210
|
+
'index.ts': 'dummy content'
|
|
211
|
+
},
|
|
212
|
+
},
|
|
213
|
+
'tsconfig.json': 'dummy content'
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
await copyConfigFiles()
|
|
217
|
+
|
|
218
|
+
// The config files stay at the project root while instrumentation.ts lives in src/,
|
|
219
|
+
// so the generated import has to climb one level or it resolves to nothing.
|
|
220
|
+
const contents = fs.readFileSync('src/instrumentation.ts', 'utf8')
|
|
221
|
+
expect(contents).toContain("import('../honeybadger.server.config')")
|
|
222
|
+
expect(contents).not.toContain("'./honeybadger.")
|
|
223
|
+
})
|
|
224
|
+
|
|
225
|
+
it('should keep the instrumentation import relative to the root when there is no src folder', async () => {
|
|
226
|
+
mock({
|
|
227
|
+
'templates': mock.load(path.resolve(__dirname, '..', 'templates')),
|
|
228
|
+
'app': {
|
|
229
|
+
'index.js': 'dummy content'
|
|
230
|
+
}
|
|
231
|
+
})
|
|
232
|
+
|
|
233
|
+
await copyConfigFiles()
|
|
234
|
+
|
|
235
|
+
const contents = fs.readFileSync('instrumentation.js', 'utf8')
|
|
236
|
+
expect(contents).toContain("import('./honeybadger.server.config')")
|
|
237
|
+
expect(contents).not.toContain("'../honeybadger.")
|
|
238
|
+
})
|
|
239
|
+
|
|
240
|
+
describe('existing instrumentation files', () => {
|
|
241
|
+
// instrumentation.ts is a shared Next.js convention - other tools register their
|
|
242
|
+
// register()/onRequestError hooks there. Overwriting it would silently stop them.
|
|
243
|
+
it('does not overwrite an existing instrumentation file', async () => {
|
|
244
|
+
mock({
|
|
245
|
+
'templates': mock.load(path.resolve(__dirname, '..', 'templates')),
|
|
246
|
+
'app': { 'index.ts': 'dummy content' },
|
|
247
|
+
'tsconfig.json': 'dummy content',
|
|
248
|
+
'instrumentation.ts': 'export function register() { /* someone else got here first */ }'
|
|
249
|
+
})
|
|
250
|
+
|
|
251
|
+
await copyConfigFiles()
|
|
252
|
+
|
|
253
|
+
expect(fs.readFileSync('instrumentation.ts', 'utf8')).toContain('someone else got here first')
|
|
254
|
+
// and it is left in place rather than shuffled into a .bak nobody reads
|
|
255
|
+
expect(fs.existsSync('instrumentation.ts.bak')).toBe(false)
|
|
256
|
+
// the client half is untouched by the server half's presence
|
|
257
|
+
expect(fs.existsSync('instrumentation-client.ts')).toBe(true)
|
|
258
|
+
})
|
|
259
|
+
|
|
260
|
+
it('detects an existing file with a different extension', async () => {
|
|
261
|
+
mock({
|
|
262
|
+
'templates': mock.load(path.resolve(__dirname, '..', 'templates')),
|
|
263
|
+
'app': { 'index.ts': 'dummy content' },
|
|
264
|
+
'tsconfig.json': 'dummy content',
|
|
265
|
+
'instrumentation.js': 'export function register() { /* js flavour */ }'
|
|
266
|
+
})
|
|
267
|
+
|
|
268
|
+
await copyConfigFiles()
|
|
269
|
+
|
|
270
|
+
// Would otherwise write instrumentation.ts alongside it, and Next.js would resolve
|
|
271
|
+
// only one of the two.
|
|
272
|
+
expect(fs.existsSync('instrumentation.ts')).toBe(false)
|
|
273
|
+
expect(fs.readFileSync('instrumentation.js', 'utf8')).toContain('js flavour')
|
|
274
|
+
})
|
|
275
|
+
|
|
276
|
+
it('does not overwrite an existing instrumentation-client file', async () => {
|
|
277
|
+
// instrumentation-client is a shared convention too: other tools initialise their
|
|
278
|
+
// browser SDK there, and next.config's instrumentationClientInject exists precisely
|
|
279
|
+
// because more than one tool expects to run in it.
|
|
280
|
+
mock({
|
|
281
|
+
'templates': mock.load(path.resolve(__dirname, '..', 'templates')),
|
|
282
|
+
'app': { 'index.ts': 'dummy content' },
|
|
283
|
+
'tsconfig.json': 'dummy content',
|
|
284
|
+
'instrumentation-client.ts': '/* another tool initialises the browser here */'
|
|
285
|
+
})
|
|
286
|
+
|
|
287
|
+
await copyConfigFiles()
|
|
288
|
+
|
|
289
|
+
expect(fs.readFileSync('instrumentation-client.ts', 'utf8')).toContain('another tool')
|
|
290
|
+
expect(fs.existsSync('instrumentation-client.ts.bak')).toBe(false)
|
|
291
|
+
// the server half is unaffected by the client half already existing
|
|
292
|
+
expect(fs.existsSync('instrumentation.ts')).toBe(true)
|
|
293
|
+
})
|
|
294
|
+
|
|
295
|
+
it('leaves both alone when both already exist', async () => {
|
|
296
|
+
mock({
|
|
297
|
+
'templates': mock.load(path.resolve(__dirname, '..', 'templates')),
|
|
298
|
+
'tsconfig.json': 'dummy content',
|
|
299
|
+
'src': {
|
|
300
|
+
'app': { 'index.ts': 'dummy content' },
|
|
301
|
+
'instrumentation.ts': '/* existing server hooks */',
|
|
302
|
+
'instrumentation-client.ts': '/* existing client init */'
|
|
303
|
+
}
|
|
304
|
+
})
|
|
305
|
+
|
|
306
|
+
await copyConfigFiles()
|
|
307
|
+
|
|
308
|
+
expect(fs.readFileSync('src/instrumentation.ts', 'utf8')).toContain('existing server hooks')
|
|
309
|
+
expect(fs.readFileSync('src/instrumentation-client.ts', 'utf8')).toContain('existing client init')
|
|
310
|
+
})
|
|
311
|
+
|
|
312
|
+
it('still writes both files when neither exists', async () => {
|
|
313
|
+
mock({
|
|
314
|
+
'templates': mock.load(path.resolve(__dirname, '..', 'templates')),
|
|
315
|
+
'app': { 'index.ts': 'dummy content' },
|
|
316
|
+
'tsconfig.json': 'dummy content'
|
|
317
|
+
})
|
|
318
|
+
|
|
319
|
+
await copyConfigFiles()
|
|
320
|
+
|
|
321
|
+
expect(fs.existsSync('instrumentation.ts')).toBe(true)
|
|
322
|
+
expect(fs.existsSync('instrumentation-client.ts')).toBe(true)
|
|
323
|
+
})
|
|
324
|
+
})
|
|
325
|
+
|
|
326
|
+
describe('a root router directory beside an unrelated src folder', () => {
|
|
327
|
+
// The layout that used to break: `src` exists, so everything was looked for under
|
|
328
|
+
// src/ — src/app was missing, so NO error components were written and the command
|
|
329
|
+
// still reported success. Next.js also resolves instrumentation at the root here,
|
|
330
|
+
// so writing it to src/ meant nothing was instrumented at all.
|
|
331
|
+
it('writes to the root when app/ is at the root', async () => {
|
|
332
|
+
mock({
|
|
333
|
+
'templates': mock.load(path.resolve(__dirname, '..', 'templates')),
|
|
334
|
+
'app': { 'index.tsx': 'dummy content' },
|
|
335
|
+
'src': { 'lib': { 'utils.ts': 'unrelated code' } },
|
|
336
|
+
'tsconfig.json': 'dummy content'
|
|
337
|
+
})
|
|
338
|
+
|
|
339
|
+
await copyConfigFiles()
|
|
340
|
+
|
|
341
|
+
expect(fs.existsSync('instrumentation.ts')).toBe(true)
|
|
342
|
+
expect(fs.existsSync('src/instrumentation.ts')).toBe(false)
|
|
343
|
+
expect(fs.existsSync('app/error.tsx')).toBe(true)
|
|
344
|
+
expect(fs.existsSync('app/global-error.tsx')).toBe(true)
|
|
345
|
+
expect(fs.existsSync('src/app/error.tsx')).toBe(false)
|
|
346
|
+
})
|
|
347
|
+
|
|
348
|
+
it('writes to the root when pages/ is at the root', async () => {
|
|
349
|
+
mock({
|
|
350
|
+
'templates': mock.load(path.resolve(__dirname, '..', 'templates')),
|
|
351
|
+
'pages': { 'index.js': 'dummy content' },
|
|
352
|
+
'src': { 'lib': { 'utils.js': 'unrelated code' } }
|
|
353
|
+
})
|
|
354
|
+
|
|
355
|
+
await copyConfigFiles()
|
|
356
|
+
|
|
357
|
+
expect(fs.existsSync('instrumentation.js')).toBe(true)
|
|
358
|
+
expect(fs.existsSync('src/instrumentation.js')).toBe(false)
|
|
359
|
+
expect(fs.existsSync('pages/_error.js')).toBe(true)
|
|
360
|
+
})
|
|
361
|
+
|
|
362
|
+
it('keeps the config import relative to the root when the file lands there', async () => {
|
|
363
|
+
mock({
|
|
364
|
+
'templates': mock.load(path.resolve(__dirname, '..', 'templates')),
|
|
365
|
+
'app': { 'index.tsx': 'dummy content' },
|
|
366
|
+
'src': { 'lib': { 'utils.ts': 'unrelated code' } },
|
|
367
|
+
'tsconfig.json': 'dummy content'
|
|
368
|
+
})
|
|
369
|
+
|
|
370
|
+
await copyConfigFiles()
|
|
371
|
+
|
|
372
|
+
// src/ exists, but the file is at the root, so the import must not climb a level.
|
|
373
|
+
const contents = fs.readFileSync('instrumentation.ts', 'utf8')
|
|
374
|
+
expect(contents).toContain("import('./honeybadger.server.config')")
|
|
375
|
+
expect(contents).not.toContain("'../honeybadger.")
|
|
376
|
+
})
|
|
377
|
+
|
|
378
|
+
it('follows the Pages Router when app/ is at the root and pages/ is under src', async () => {
|
|
379
|
+
// Next derives the instrumentation directory as the parent of `pagesDir || appDir`,
|
|
380
|
+
// so the Pages Router wins: it looks under src/, not at the root.
|
|
381
|
+
mock({
|
|
382
|
+
'templates': mock.load(path.resolve(__dirname, '..', 'templates')),
|
|
383
|
+
'app': { 'index.tsx': 'dummy content' },
|
|
384
|
+
'src': { 'pages': { 'index.tsx': 'dummy content' } },
|
|
385
|
+
'tsconfig.json': 'dummy content'
|
|
386
|
+
})
|
|
387
|
+
|
|
388
|
+
await copyConfigFiles()
|
|
389
|
+
|
|
390
|
+
expect(fs.existsSync('src/instrumentation.ts')).toBe(true)
|
|
391
|
+
expect(fs.existsSync('instrumentation.ts')).toBe(false)
|
|
392
|
+
// and the config import has to climb out of src/ to reach the project root
|
|
393
|
+
expect(fs.readFileSync('src/instrumentation.ts', 'utf8'))
|
|
394
|
+
.toContain("import('../honeybadger.server.config')")
|
|
395
|
+
// the error components still follow their own router's location
|
|
396
|
+
expect(fs.existsSync('app/error.tsx')).toBe(true)
|
|
397
|
+
expect(fs.existsSync('src/pages/_error.tsx')).toBe(true)
|
|
398
|
+
})
|
|
399
|
+
|
|
400
|
+
it('still uses src/ when the router lives there', async () => {
|
|
401
|
+
mock({
|
|
402
|
+
'templates': mock.load(path.resolve(__dirname, '..', 'templates')),
|
|
403
|
+
'src': { 'app': { 'index.tsx': 'dummy content' } },
|
|
404
|
+
'tsconfig.json': 'dummy content'
|
|
405
|
+
})
|
|
406
|
+
|
|
407
|
+
await copyConfigFiles()
|
|
408
|
+
|
|
409
|
+
expect(fs.existsSync('src/instrumentation.ts')).toBe(true)
|
|
410
|
+
expect(fs.existsSync('instrumentation.ts')).toBe(false)
|
|
411
|
+
expect(fs.existsSync('src/app/error.tsx')).toBe(true)
|
|
412
|
+
})
|
|
413
|
+
})
|
|
414
|
+
|
|
181
415
|
})
|
package/src/copy-config-files.ts
CHANGED
|
@@ -7,30 +7,56 @@ function usesTypescript() {
|
|
|
7
7
|
return fs.existsSync('tsconfig.json')
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
/**
|
|
11
|
+
* Where a router directory actually lives: `''` for the project root, `'src'`, or `null`
|
|
12
|
+
* when the project does not use that router.
|
|
13
|
+
*
|
|
14
|
+
* A root `app/` (or `pages/`) can sit beside a `src/` folder used for something else, so
|
|
15
|
+
* this locates each directory rather than assuming a layout. Root wins when a directory
|
|
16
|
+
* somehow exists in both places, matching Next.js.
|
|
17
|
+
*/
|
|
18
|
+
function locateRouterDir(router: 'app' | 'pages'): string | null {
|
|
19
|
+
if (fs.existsSync(router)) {
|
|
20
|
+
return ''
|
|
21
|
+
}
|
|
13
22
|
|
|
14
|
-
|
|
15
|
-
|
|
23
|
+
if (fs.existsSync(path.join('src', router))) {
|
|
24
|
+
return 'src'
|
|
25
|
+
}
|
|
16
26
|
|
|
17
|
-
return
|
|
27
|
+
return null
|
|
18
28
|
}
|
|
19
29
|
|
|
20
|
-
|
|
21
|
-
|
|
30
|
+
/**
|
|
31
|
+
* Where Next.js resolves the `instrumentation` files. It derives that directory as the
|
|
32
|
+
* parent of `pagesDir || appDir`:
|
|
33
|
+
*
|
|
34
|
+
* const rootDir = path.join((pagesDir || appDir)!, '..')
|
|
35
|
+
* — packages/next/src/build/index.ts
|
|
36
|
+
*
|
|
37
|
+
* So it is the parent of whichever router directory is found, and the Pages Router wins
|
|
38
|
+
* when a project has both. A project with a root `app/` and a `src/pages/` therefore
|
|
39
|
+
* resolves instrumentation under `src/`, not at the root.
|
|
40
|
+
*
|
|
41
|
+
* Getting this wrong is silent — the file is written somewhere Next.js never reads, so
|
|
42
|
+
* nothing is instrumented and nothing complains.
|
|
43
|
+
*/
|
|
44
|
+
function instrumentationDir(): string {
|
|
45
|
+
const pagesDir = locateRouterDir('pages')
|
|
46
|
+
if (pagesDir !== null) {
|
|
47
|
+
return pagesDir
|
|
48
|
+
}
|
|
22
49
|
|
|
23
|
-
return
|
|
50
|
+
return locateRouterDir('app') ?? ''
|
|
24
51
|
}
|
|
25
52
|
|
|
26
|
-
function getTargetPath(
|
|
53
|
+
function getTargetPath(routerDir: string, isAppRouter = false, isGlobalErrorComponent = false) {
|
|
27
54
|
if (!isAppRouter && isGlobalErrorComponent) {
|
|
28
55
|
throw new Error('invalid arguments: isGlobalErrorComponent can only be true when isAppRouter is true')
|
|
29
56
|
}
|
|
30
57
|
|
|
31
58
|
const extension = usesTypescript() ? 'tsx' : 'js'
|
|
32
|
-
|
|
33
|
-
srcFolder = path.join(srcFolder, isAppRouter ? 'app' : 'pages')
|
|
59
|
+
const srcFolder = path.join(routerDir, isAppRouter ? 'app' : 'pages')
|
|
34
60
|
|
|
35
61
|
let fileName = ''
|
|
36
62
|
if (isAppRouter) {
|
|
@@ -48,37 +74,90 @@ function getTemplate(isAppRouter = false, isGlobalErrorComponent = false) {
|
|
|
48
74
|
throw new Error('invalid arguments: isGlobalErrorComponent can only be true when isAppRouter is true')
|
|
49
75
|
}
|
|
50
76
|
|
|
51
|
-
const extension = isGlobalErrorComponent ? 'tsx' : 'js'
|
|
52
77
|
const templateName = isAppRouter ? '_error_app_router' : '_error'
|
|
53
78
|
|
|
79
|
+
// `_error_app_router` ships as a JS/TS pair that differs only by the prop type
|
|
80
|
+
// annotation, so pick the one matching the target file's extension. `_error` is JS-only,
|
|
81
|
+
// so it has no pair.
|
|
82
|
+
const hasTypedVariant = isAppRouter
|
|
83
|
+
const extension = hasTypedVariant && usesTypescript() ? 'tsx' : 'js'
|
|
84
|
+
|
|
54
85
|
return path.resolve(__dirname, '../templates', templateName + '.' + extension)
|
|
55
86
|
}
|
|
56
87
|
|
|
57
|
-
async function copyErrorJs(
|
|
88
|
+
async function copyErrorJs(routerDir: string, isAppRouter = false) {
|
|
58
89
|
const sourcePath = getTemplate(isAppRouter)
|
|
59
|
-
const targetPath = getTargetPath(
|
|
90
|
+
const targetPath = getTargetPath(routerDir, isAppRouter)
|
|
60
91
|
|
|
61
92
|
return copyFileWithBackup(sourcePath, targetPath)
|
|
62
93
|
}
|
|
63
94
|
|
|
64
|
-
function copyGlobalErrorJs(
|
|
95
|
+
function copyGlobalErrorJs(routerDir: string) {
|
|
65
96
|
const sourcePath = getTemplate(true, true)
|
|
66
|
-
const targetPath = getTargetPath(
|
|
97
|
+
const targetPath = getTargetPath(routerDir, true, true)
|
|
67
98
|
|
|
68
99
|
return copyFileWithBackup(sourcePath, targetPath)
|
|
69
100
|
}
|
|
70
101
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
102
|
+
/**
|
|
103
|
+
* `instrumentation` and `instrumentation-client` are Next.js file conventions, so unlike
|
|
104
|
+
* the `honeybadger.*.config` files their location is dictated by Next.js — see
|
|
105
|
+
* `instrumentationDir` for the rule.
|
|
106
|
+
*
|
|
107
|
+
* The config files always stay at the project root, so when the instrumentation file
|
|
108
|
+
* lands in `src/` instead its relative import has to climb one level.
|
|
109
|
+
*/
|
|
110
|
+
async function copyInstrumentationFile(name: string, srcFolder: string) {
|
|
111
|
+
const sourcePath = path.resolve(__dirname, '../templates', name + '.js')
|
|
112
|
+
const extension = usesTypescript() ? 'ts' : 'js'
|
|
113
|
+
const targetPath = path.join(srcFolder, name + '.' + extension)
|
|
114
|
+
|
|
115
|
+
// Unlike the honeybadger.* files, `instrumentation` is a shared Next.js convention:
|
|
116
|
+
// other tools register their own hooks in it. Overwriting it would silently stop them
|
|
117
|
+
// running, and a .bak file is no help if nobody notices, so leave an existing file
|
|
118
|
+
// alone and print what to add instead.
|
|
119
|
+
const existingPath = ['ts', 'js', 'mjs']
|
|
120
|
+
.map((ext) => path.join(srcFolder, name + '.' + ext))
|
|
121
|
+
.find((candidate) => fs.existsSync(candidate))
|
|
122
|
+
|
|
123
|
+
if (existingPath) {
|
|
124
|
+
console.log(
|
|
125
|
+
`\nSkipped ${existingPath} because it already exists — it may register hooks for ` +
|
|
126
|
+
'other tools.\nAdd Honeybadger to it by hand; see ' +
|
|
127
|
+
`${path.relative(process.cwd(), sourcePath)} for the contents to merge in.`
|
|
128
|
+
)
|
|
129
|
+
return
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
let contents = await fs.promises.readFile(sourcePath, 'utf8')
|
|
133
|
+
if (srcFolder) {
|
|
134
|
+
contents = contents.replace(/(['"])\.\/honeybadger\./g, '$1../honeybadger.')
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (debug) {
|
|
138
|
+
console.debug('writing', targetPath)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return fs.promises.writeFile(targetPath, contents)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
async function backup(targetPath) {
|
|
145
|
+
if (!fs.existsSync(targetPath)) {
|
|
146
|
+
return
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Don't overwrite an existing file without creating a backup first
|
|
150
|
+
const backupPath = targetPath + '.bak'
|
|
151
|
+
if (debug) {
|
|
152
|
+
console.debug('backing up', targetPath, 'to', backupPath)
|
|
80
153
|
}
|
|
81
154
|
|
|
155
|
+
return fs.promises.copyFile(targetPath, backupPath)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
async function copyFileWithBackup(sourcePath, targetPath) {
|
|
159
|
+
await backup(targetPath)
|
|
160
|
+
|
|
82
161
|
if (debug) {
|
|
83
162
|
console.debug('copying', sourcePath, 'to', targetPath)
|
|
84
163
|
}
|
|
@@ -92,9 +171,9 @@ export async function copyConfigFiles() {
|
|
|
92
171
|
}
|
|
93
172
|
|
|
94
173
|
const templateDir = path.resolve(__dirname, '../templates')
|
|
174
|
+
// The browser config is gone: `instrumentation-client` now configures the browser,
|
|
175
|
+
// and it runs before hydration rather than via a bundler-injected entry point.
|
|
95
176
|
const configFiles = [
|
|
96
|
-
'honeybadger.browser.config.js',
|
|
97
|
-
'honeybadger.edge.config.js',
|
|
98
177
|
'honeybadger.server.config.js',
|
|
99
178
|
]
|
|
100
179
|
|
|
@@ -105,15 +184,19 @@ export async function copyConfigFiles() {
|
|
|
105
184
|
return fs.promises.copyFile(path.join(templateDir, file), file)
|
|
106
185
|
})
|
|
107
186
|
|
|
108
|
-
const
|
|
187
|
+
const instrumentationFolder = instrumentationDir()
|
|
188
|
+
copyPromises.push(copyInstrumentationFile('instrumentation', instrumentationFolder))
|
|
189
|
+
copyPromises.push(copyInstrumentationFile('instrumentation-client', instrumentationFolder))
|
|
109
190
|
|
|
110
|
-
|
|
111
|
-
|
|
191
|
+
const pagesDir = locateRouterDir('pages')
|
|
192
|
+
if (pagesDir !== null) {
|
|
193
|
+
copyPromises.push(copyErrorJs(pagesDir, false))
|
|
112
194
|
}
|
|
113
195
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
copyPromises.push(
|
|
196
|
+
const appDir = locateRouterDir('app')
|
|
197
|
+
if (appDir !== null) {
|
|
198
|
+
copyPromises.push(copyErrorJs(appDir, true))
|
|
199
|
+
copyPromises.push(copyGlobalErrorJs(appDir))
|
|
117
200
|
}
|
|
118
201
|
|
|
119
202
|
await Promise.all(copyPromises);
|
package/src/edge.ts
CHANGED
|
@@ -1 +1,12 @@
|
|
|
1
|
-
|
|
1
|
+
// The bundle Next.js resolves in its *edge compilation context* — the one it builds for
|
|
2
|
+
// middleware and for `instrumentation`. That context exists in every Next.js build, even
|
|
3
|
+
// with no middleware and no edge routes, and Node builtins are unavailable in it.
|
|
4
|
+
//
|
|
5
|
+
// This is why the `edge-light` exports condition has to stay: without it Next resolves
|
|
6
|
+
// this package to the browser bundle in that context, and the build fails with
|
|
7
|
+
// "Export captureRequestError doesn't exist".
|
|
8
|
+
//
|
|
9
|
+
// `setup` is deliberately excluded — next.config is evaluated on the Node side only, and it
|
|
10
|
+
// is what pulls in `fs`, `path` and the source map upload.
|
|
11
|
+
export * from './capture-request-error'
|
|
12
|
+
export * from './insights'
|
package/src/flush.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import Honeybadger from '@honeybadger-io/js';
|
|
2
|
+
import * as nextServer from 'next/server';
|
|
3
|
+
|
|
4
|
+
export type WaitUntil = (promise: Promise<unknown>) => void
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The `waitUntil` primitive the hosting platform injects per request. Next.js
|
|
8
|
+
* resolves `after()` through this same accessor, and it is the only channel
|
|
9
|
+
* available in Pages Router API routes, which are invoked as `(req, res)` with
|
|
10
|
+
* no context argument to read it from.
|
|
11
|
+
*/
|
|
12
|
+
export function requestContextWaitUntil(): WaitUntil | undefined {
|
|
13
|
+
const context = (globalThis as Record<symbol, unknown>)[Symbol.for('@next/request-context')] as
|
|
14
|
+
| { get?: () => { waitUntil?: WaitUntil } | undefined }
|
|
15
|
+
| undefined
|
|
16
|
+
const waitUntil = context?.get?.()?.waitUntil
|
|
17
|
+
return typeof waitUntil === 'function' ? waitUntil : undefined
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Ensure events are delivered before a serverless runtime freezes, without
|
|
22
|
+
* delaying the response where the runtime lets us avoid it.
|
|
23
|
+
*
|
|
24
|
+
* In order of preference: Next.js `after()` (App Router only — it needs App Router
|
|
25
|
+
* request context, so Pages Router must not call it), then a `waitUntil` from the
|
|
26
|
+
* platform request context, then a blocking `flushAsync()` when the runtime offers
|
|
27
|
+
* neither.
|
|
28
|
+
*
|
|
29
|
+
* Delivery failures are logged by the events worker and must not break the handler.
|
|
30
|
+
*/
|
|
31
|
+
export function scheduleFlush(options: { useAfter?: boolean; waitUntil?: WaitUntil } = {}): Promise<void> | void {
|
|
32
|
+
const flush = () => Honeybadger.flushAsync().catch(() => { /* logged by the events worker */ })
|
|
33
|
+
|
|
34
|
+
if (options.useAfter) {
|
|
35
|
+
const after = (nextServer as { after?: (cb: () => unknown) => void }).after
|
|
36
|
+
if (typeof after === 'function') {
|
|
37
|
+
// Exported but still refusable: `after()` throws outside a supported
|
|
38
|
+
// context. Fall through to the remaining strategies rather than failing
|
|
39
|
+
// the request.
|
|
40
|
+
try {
|
|
41
|
+
after(flush)
|
|
42
|
+
return
|
|
43
|
+
}
|
|
44
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
45
|
+
catch (error) {
|
|
46
|
+
// try waitUntil / blocking flush below
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const waitUntil = options.waitUntil ?? requestContextWaitUntil()
|
|
52
|
+
if (waitUntil) {
|
|
53
|
+
waitUntil(flush())
|
|
54
|
+
return
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return flush()
|
|
58
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,2 +1,7 @@
|
|
|
1
|
-
export * from './
|
|
2
|
-
export * from './
|
|
1
|
+
export * from './setup'
|
|
2
|
+
export * from './capture-request-error'
|
|
3
|
+
export * from './capture-router-transition-start'
|
|
4
|
+
export * from './insights'
|
|
5
|
+
|
|
6
|
+
// The config type was never exported, so `next.config.ts` could not be typed.
|
|
7
|
+
export type { HoneybadgerNextJsConfig } from './types'
|