Skip to content

Commit fca1568

Browse files
committed
Patch fetch for modular apps
1 parent 6d2e3d8 commit fca1568

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

mobile/android/app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ android {
77
applicationId "com.pulse_editor.app"
88
minSdkVersion rootProject.ext.minSdkVersion
99
targetSdkVersion rootProject.ext.targetSdkVersion
10-
versionCode 2
10+
versionCode 4
1111
versionName "v0.1.1-beta"
1212
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1313
aaptOptions {

web/app/(extension-layout)/extension/page.tsx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 (/^https?:\/\//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

Comments
 (0)