Skip to content

Commit 23a29a6

Browse files
committed
feat: replace data fetch file with temp file in client
1 parent 61e9f70 commit 23a29a6

File tree

4 files changed

+42
-16
lines changed

4 files changed

+42
-16
lines changed

apps/modern-component-data-fetch/host/src/routes/page.tsx

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { kit } from '@module-federation/modern-js/runtime';
22
import './index.css';
3-
import RemoteSSRComponent2 from 'remote/Content2';
3+
// import RemoteSSRComponent2 from 'remote/Content2';
44

55
const { createRemoteSSRComponent } = kit;
66

@@ -18,19 +18,19 @@ const RemoteSSRComponent = createRemoteSSRComponent({
1818
},
1919
});
2020

21-
// const RemoteSSRComponent2 = createRemoteSSRComponent({
22-
// loader: () => import('remote/Content2'),
23-
// loading: 'loading...',
24-
// export: 'default',
25-
// fallback: ({ error }) => {
26-
// console.log(33333333333);
27-
// console.error(error);
28-
// if (error instanceof Error && error.message.includes('not exist')) {
29-
// return <div>fallback - not existed id</div>;
30-
// }
31-
// return <div>fallback</div>;
32-
// },
33-
// });
21+
const RemoteSSRComponent2 = createRemoteSSRComponent({
22+
loader: () => import('remote/Content2'),
23+
loading: 'loading...',
24+
export: 'default',
25+
fallback: ({ error }) => {
26+
console.log(33333333333);
27+
console.error(error);
28+
if (error instanceof Error && error.message.includes('not exist')) {
29+
return <div>fallback - not existed id</div>;
30+
}
31+
return <div>fallback</div>;
32+
},
33+
});
3434

3535
const Index = () => (
3636
<>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export type Data = {
2+
data: string;
3+
};
4+
5+
export const fetchData = async (): Promise<Data> => {
6+
console.log('Content.data.client');
7+
return new Promise((resolve) => {
8+
setTimeout(() => {
9+
resolve({
10+
data: `fetch data from provider ${new Date()}`,
11+
});
12+
}, 1000);
13+
});
14+
};

apps/modern-component-data-fetch/provider/src/components/Content.data.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ export type Data = {
33
};
44

55
export const fetchData = async (): Promise<Data> => {
6+
console.log('Content.data.server');
7+
68
return new Promise((resolve) => {
79
setTimeout(() => {
810
resolve({

packages/rsbuild-plugin/src/utils/addDataFetchExposes.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fs from 'fs';
22
import path from 'path';
3+
import { TEMP_DIR } from '@module-federation/sdk';
34

45
import type { moduleFederationPlugin } from '@module-federation/sdk';
56

@@ -21,7 +22,7 @@ const addDataFetchExpose = (
2122
if (!exposes[dataFetchKey]) {
2223
exposes[dataFetchKey] = filepath;
2324
}
24-
return true;
25+
return dataFetchKey;
2526
};
2627

2728
export function addDataFetchExposes(
@@ -46,7 +47,16 @@ export function addDataFetchExposes(
4647
return;
4748
}
4849

49-
addDataFetchExpose(exposes, key, dataFetchPath);
50+
const dataFetchKey = addDataFetchExpose(exposes, key, dataFetchPath);
51+
if (!isServer && dataFetchKey) {
52+
const tempDataFetchFilepath = path.resolve(
53+
process.cwd(),
54+
`node_modules/${TEMP_DIR}/data-fetch.ts`,
55+
);
56+
const content = `export const fetchData=()=>{throw new Error('should not be called')};`;
57+
fs.writeFileSync(tempDataFetchFilepath, content);
58+
exposes[dataFetchKey] = tempDataFetchFilepath;
59+
}
5060
return;
5161
});
5262
}

0 commit comments

Comments
 (0)