@@ -482,7 +482,7 @@ async function addSentryToEntryProperty(
482482 // we know is that it won't have gotten *simpler* in form, so we only need to worry about the object and function
483483 // options. See https://webpack.js.org/configuration/entry-context/#entry.
484484
485- const { isServer, dir : projectDir , nextRuntime } = buildContext ;
485+ const { isServer, dir : projectDir , nextRuntime, dev : isDevMode } = buildContext ;
486486 const runtime = isServer ? ( buildContext . nextRuntime === 'edge' ? 'edge' : 'node' ) : 'browser' ;
487487
488488 const newEntryProperty =
@@ -502,7 +502,7 @@ async function addSentryToEntryProperty(
502502 // inject into all entry points which might contain user's code
503503 for ( const entryPointName in newEntryProperty ) {
504504 if ( shouldAddSentryToEntryPoint ( entryPointName , runtime ) ) {
505- addFilesToExistingEntryPoint ( newEntryProperty , entryPointName , filesToInject ) ;
505+ addFilesToExistingEntryPoint ( newEntryProperty , entryPointName , filesToInject , isDevMode ) ;
506506 } else {
507507 if (
508508 isServer &&
@@ -570,31 +570,44 @@ export function getUserConfigFilePath(projectDir: string, platform: 'server' | '
570570 *
571571 * @param entryProperty The existing `entry` config object
572572 * @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
574574 */
575575function addFilesToExistingEntryPoint (
576576 entryProperty : EntryPropertyObject ,
577577 entryPointName : string ,
578- filepaths : string [ ] ,
578+ filesToInsert : string [ ] ,
579+ isDevMode : boolean ,
579580) : 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+
580585 // can be a string, array of strings, or object whose `import` property is one of those two
581586 const currentEntryPoint = entryProperty [ entryPointName ] ;
582587 let newEntryPoint = currentEntryPoint ;
583588
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+ }
588599 }
589600 // descriptor object (webpack 5+)
590601 else if ( typeof currentEntryPoint === 'object' && 'import' in currentEntryPoint ) {
591602 const currentImportValue = currentEntryPoint . import ;
592- let newImportValue ;
603+ const newImportValue = arrayify ( currentImportValue ) ;
593604
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 ) ;
596608 } 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 ) ;
598611 }
599612
600613 newEntryPoint = {
0 commit comments