@@ -43,6 +43,7 @@ export default function ExtensionPage({}) {
4343 setIsRegistered ( true ) ;
4444 }
4545
46+ // Load the remote module
4647 mfHost
4748 . loadRemote ( `${ moduleId } /main` )
4849 . then ( ( module ) => {
@@ -58,6 +59,46 @@ export default function ExtensionPage({}) {
5859 . catch ( ( error ) => {
5960 console . error ( "Error loading remote module:" , error ) ;
6061 } ) ;
62+
63+ // Patch fetch for Pulse App backend calling
64+ const originalFetch = window . fetch ;
65+
66+ const patchedFetch = async (
67+ ...args : Parameters < typeof fetch >
68+ ) : Promise < Response > => {
69+ const [ resource , config ] = args ;
70+ const url =
71+ resource instanceof Request ? resource . url : resource . toString ( ) ;
72+
73+ // Only patch relative URLs (not absolute http/https)
74+ if ( / ^ h t t p s ? : \/ \/ / i. test ( url ) ) {
75+ return originalFetch ( resource , config ) ;
76+ }
77+
78+ const newUrl = remoteOrigin . startsWith (
79+ process . env . NEXT_PUBLIC_CDN_URL ?? "https://cdn.pulse-editor.com" ,
80+ )
81+ ? `${ process . env . NEXT_PUBLIC_SERVER_FUNCTION_RUNNER_URL } /${ moduleId } /${ moduleVersion } /${ url . replace ( "/server-function/" , "" ) } `
82+ : remoteOrigin + url ;
83+
84+ console . log ( `[FETCH INTERCEPTED]: ${ url } → ${ newUrl } ` ) ;
85+ console . log ( `[App Info] ID: ${ moduleId } , Version: ${ moduleVersion } ` ) ;
86+
87+ const response = await originalFetch ( newUrl , config ) ;
88+
89+ if ( ! response . ok ) {
90+ console . warn ( `Fetch Error (${ response . status } ) for ${ url } ` ) ;
91+ }
92+
93+ return response ;
94+ } ;
95+
96+ window . fetch = patchedFetch ;
97+
98+ // // Cleanup on unmount or dependency change
99+ // return () => {
100+ // window.fetch = originalFetch;
101+ // };
61102 } , [ remoteOrigin , moduleId , moduleVersion , isMounted ] ) ;
62103
63104 if ( ! isMounted ) {
0 commit comments