@@ -482,7 +482,7 @@ async function addSentryToEntryProperty(
482
482
// we know is that it won't have gotten *simpler* in form, so we only need to worry about the object and function
483
483
// options. See https://webpack.js.org/configuration/entry-context/#entry.
484
484
485
- const { isServer, dir : projectDir , nextRuntime } = buildContext ;
485
+ const { isServer, dir : projectDir , nextRuntime, dev : isDevMode } = buildContext ;
486
486
const runtime = isServer ? ( buildContext . nextRuntime === 'edge' ? 'edge' : 'node' ) : 'browser' ;
487
487
488
488
const newEntryProperty =
@@ -502,7 +502,7 @@ async function addSentryToEntryProperty(
502
502
// inject into all entry points which might contain user's code
503
503
for ( const entryPointName in newEntryProperty ) {
504
504
if ( shouldAddSentryToEntryPoint ( entryPointName , runtime ) ) {
505
- addFilesToExistingEntryPoint ( newEntryProperty , entryPointName , filesToInject ) ;
505
+ addFilesToExistingEntryPoint ( newEntryProperty , entryPointName , filesToInject , isDevMode ) ;
506
506
} else {
507
507
if (
508
508
isServer &&
@@ -570,31 +570,44 @@ export function getUserConfigFilePath(projectDir: string, platform: 'server' | '
570
570
*
571
571
* @param entryProperty The existing `entry` config object
572
572
* @param entryPointName The key where the file should be injected
573
- * @param filepaths An array of paths to the injected files
573
+ * @param filesToInsert An array of paths to the injected files
574
574
*/
575
575
function addFilesToExistingEntryPoint (
576
576
entryProperty : EntryPropertyObject ,
577
577
entryPointName : string ,
578
- filepaths : string [ ] ,
578
+ filesToInsert : string [ ] ,
579
+ isDevMode : boolean ,
579
580
) : void {
581
+ // BIG FAT NOTE: Order of insertion seems to matter here. If we insert the new files before the `currentEntrypoint`s,
582
+ // the Next.js dev server breaks. Because we generally still want the SDK to be initialized as early as possible we
583
+ // still keep it at the start of the entrypoints if we are not in dev mode.
584
+
580
585
// can be a string, array of strings, or object whose `import` property is one of those two
581
586
const currentEntryPoint = entryProperty [ entryPointName ] ;
582
587
let newEntryPoint = currentEntryPoint ;
583
588
584
- if ( typeof currentEntryPoint === 'string' ) {
585
- newEntryPoint = [ ...filepaths , currentEntryPoint ] ;
586
- } else if ( Array . isArray ( currentEntryPoint ) ) {
587
- newEntryPoint = [ ...filepaths , ...currentEntryPoint ] ;
589
+ if ( typeof currentEntryPoint === 'string' || Array . isArray ( currentEntryPoint ) ) {
590
+ newEntryPoint = arrayify ( currentEntryPoint ) ;
591
+
592
+ if ( isDevMode ) {
593
+ // Inserting at beginning breaks dev mode so we insert at the end
594
+ newEntryPoint . push ( ...filesToInsert ) ;
595
+ } else {
596
+ // In other modes we insert at the beginning so that the SDK initializes as early as possible
597
+ newEntryPoint . unshift ( ...filesToInsert ) ;
598
+ }
588
599
}
589
600
// descriptor object (webpack 5+)
590
601
else if ( typeof currentEntryPoint === 'object' && 'import' in currentEntryPoint ) {
591
602
const currentImportValue = currentEntryPoint . import ;
592
- let newImportValue ;
603
+ const newImportValue = arrayify ( currentImportValue ) ;
593
604
594
- if ( typeof currentImportValue === 'string' ) {
595
- newImportValue = [ ...filepaths , currentImportValue ] ;
605
+ if ( isDevMode ) {
606
+ // Inserting at beginning breaks dev mode so we insert at the end
607
+ newImportValue . push ( ...filesToInsert ) ;
596
608
} else {
597
- newImportValue = [ ...filepaths , ...currentImportValue ] ;
609
+ // In other modes we insert at the beginning so that the SDK initializes as early as possible
610
+ newImportValue . unshift ( ...filesToInsert ) ;
598
611
}
599
612
600
613
newEntryPoint = {
0 commit comments