@@ -448,27 +448,30 @@ function updateWrapperStates () {
448448
449449/**
450450 * Loads the core config from the server (already combined with the system defaults).
451+ * @returns {Promise<object> } The loaded config.
451452 */
452453async function loadConfig ( ) {
453- try {
454- const res = await fetch ( new URL ( "config/" , `${ location . origin } ${ config . basePath } ` ) ) ;
454+ const basePath = globalThis . config ?. basePath ?? "/" ;
455+ const res = await fetch ( new URL ( "config/" , `${ location . origin } ${ basePath } ` ) ) ;
456+ if ( ! res . ok ) {
457+ throw new Error ( `Config request failed with status ${ res . status } .` ) ;
458+ }
455459
456- // The server tags functions as { __mmFunction: "<source>" } because
457- // JSON.stringify can't serialise live functions. This reviver turns
458- // those tagged objects back into callable functions.
459- config = JSON . parse ( await res . text ( ) , ( key , value ) => {
460- if ( value && typeof value === "object" && typeof value . __mmFunction === "string" ) {
461- try {
462- return new Function ( `return (${ value . __mmFunction } )` ) ( ) ;
463- } catch {
464- Log . warn ( `Failed to revive function for config key "${ key } ".` ) ;
465- }
460+ // The server tags functions as { __mmFunction: "<source>" } because
461+ // JSON.stringify can't serialise live functions. This reviver turns
462+ // those tagged objects back into callable functions.
463+ const config = JSON . parse ( await res . text ( ) , ( key , value ) => {
464+ if ( value && typeof value === "object" && typeof value . __mmFunction === "string" ) {
465+ try {
466+ return new Function ( `return (${ value . __mmFunction } )` ) ( ) ;
467+ } catch {
468+ Log . warn ( `Failed to revive function for config key "${ key } ".` ) ;
466469 }
467- return value ;
468- } ) ;
469- } catch ( error ) {
470- Log . error ( "Unable to retrieve config" , error ) ;
471- }
470+ }
471+ return value ;
472+ } ) ;
473+ globalThis . config = config ;
474+ return config ;
472475}
473476
474477/**
@@ -570,10 +573,8 @@ export const MM = {
570573 */
571574 async init ( ) {
572575 Log . info ( "Initializing MagicMirror²." ) ;
573- await loadConfig ( ) ;
574-
576+ const config = await loadConfig ( ) ;
575577 Log . setLogLevel ( config . logLevel ) ;
576-
577578 await Translator . loadCoreTranslations ( config . language ) ;
578579 await loadModules ( ) ;
579580 } ,
@@ -595,20 +596,20 @@ export const MM = {
595596
596597 // Setup global socket listener for RELOAD event (watch mode)
597598 const socket = io ( "/" , {
598- path : `${ config . basePath || "/" } socket.io`
599+ path : `${ globalThis . config . basePath || "/" } socket.io`
599600 } ) ;
600601
601602 socket . on ( "RELOAD" , ( ) => {
602603 Log . warn ( "Reload notification received from server" ) ;
603604 window . location . reload ( true ) ;
604605 } ) ;
605606
606- if ( config . reloadAfterServerRestart ) {
607+ if ( globalThis . config . reloadAfterServerRestart ) {
607608 setInterval ( async ( ) => {
608609 // if server startup time has changed (which means server was restarted)
609610 // the client reloads the mm page
610611 try {
611- const res = await fetch ( `${ location . protocol } //${ location . host } ${ config . basePath } startup` ) ;
612+ const res = await fetch ( `${ location . protocol } //${ location . host } ${ globalThis . config . basePath } startup` ) ;
612613 const curr = await res . text ( ) ;
613614 if ( startUp === "" ) startUp = curr ;
614615 if ( startUp !== curr ) {
@@ -619,7 +620,7 @@ export const MM = {
619620 } catch ( err ) {
620621 Log . error ( `MagicMirror not reachable: ${ err } ` ) ;
621622 }
622- } , config . checkServerInterval ) ;
623+ } , globalThis . config . checkServerInterval ) ;
623624 }
624625 } ,
625626
@@ -711,4 +712,8 @@ export const MM = {
711712// Legacy global bridge for third-party modules that reference window.MM directly.
712713if ( ! globalThis . MM ) globalThis . MM = MM ;
713714
714- MM . init ( ) ;
715+ try {
716+ await MM . init ( ) ;
717+ } catch ( error ) {
718+ Log . error ( error ) ;
719+ }
0 commit comments