Skip to content

Commit 461ad48

Browse files
committed
chore: wip
1 parent 23a29a6 commit 461ad48

File tree

5 files changed

+69
-27
lines changed

5 files changed

+69
-27
lines changed

apps/modern-component-data-fetch/provider/modern.config.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ export default defineConfig({
66
runtime: {
77
router: true,
88
},
9-
server: {
10-
ssr: {
11-
mode: 'stream',
12-
},
13-
port: 5002,
14-
},
9+
// server: {
10+
// ssr: {
11+
// mode: 'stream',
12+
// },
13+
// port: 5002,
14+
// },
1515
plugins: [
1616
appTools({
1717
bundler: 'rspack',

packages/modernjs/src/cli/server/data-fetch-server-plugin.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { DATA_FETCH_QUERY } from '../../constant';
2+
import logger from '../../runtime/logger';
13
import type { ServerPluginLegacy } from '@modern-js/server-core';
24

35
export default (): ServerPluginLegacy => ({
@@ -16,8 +18,30 @@ export default (): ServerPluginLegacy => ({
1618
config.render!.middleware!.push(
1719
async (ctx: { request: any }, next: () => any) => {
1820
const { request } = ctx;
19-
console.log('request.url: ', request.url);
20-
return next();
21+
try {
22+
const url = new URL(request.url);
23+
const dataFetchId = url.searchParams.get(DATA_FETCH_QUERY);
24+
if (!dataFetchId) {
25+
return next();
26+
}
27+
logger.debug('dataFetchId: ', dataFetchId);
28+
29+
const fetchDataPromise =
30+
globalThis.nativeGlobal.__FEDERATION__.__DATA_FETCH_MAP__.get(
31+
dataFetchId,
32+
);
33+
if (!fetchDataPromise) {
34+
return next();
35+
}
36+
37+
const fetchDataFn = await fetchDataPromise;
38+
if (!fetchDataFn) {
39+
return next();
40+
}
41+
return fetchDataFn();
42+
} catch (e) {
43+
return next();
44+
}
2145
},
2246
);
2347
return config;

packages/modernjs/src/constant.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export const LOCALHOST = 'localhost';
22
export const PLUGIN_IDENTIFIER = '[ Modern.js Module Federation ]';
3+
export const DATA_FETCH_QUERY = 'x-mf-data-fetch';

packages/modernjs/src/runtime/dataFetch.ts

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,47 @@
11
import { isBrowserEnv, composeKeyWithSeparator } from '@module-federation/sdk';
22
import helpers from '@module-federation/runtime/helpers';
3-
import { getDataFetchInfo, isSSRDowngrade } from './utils';
3+
import { getDataFetchInfo, isCSROnly, isSSRDowngrade } from './utils';
4+
import logger from './logger';
5+
import { DATA_FETCH_QUERY } from '../constant';
46

57
export async function fetchData(id: string): Promise<unknown | undefined> {
8+
const fn = async () => {
9+
const fetchDataPromise =
10+
helpers.global.nativeGlobal.__FEDERATION__.__DATA_FETCH_MAP__.get(id);
11+
if (!fetchDataPromise) {
12+
return;
13+
}
14+
15+
const fetchDataFn = await fetchDataPromise;
16+
if (!fetchDataFn) {
17+
return;
18+
}
19+
return fetchDataFn();
20+
};
621
if (isBrowserEnv()) {
722
if (!globalThis._MF__DATA_FETCH_ID_MAP__) {
823
globalThis._MF__DATA_FETCH_ID_MAP__ = {};
924
}
25+
1026
if (globalThis._MF__DATA_FETCH_ID_MAP__[id]) {
1127
return;
1228
}
29+
1330
if (isSSRDowngrade()) {
14-
console.log('==========ssr downgrade!');
15-
// wip...
16-
// fetch("host/server")
31+
logger.debug('==========ssr downgrade!');
32+
// const
33+
const currentUrl = new URL(window.location.href);
34+
currentUrl.searchParams.set(DATA_FETCH_QUERY, id);
35+
const fetchUrl = currentUrl.toString();
36+
const data = await fetch(fetchUrl).then((res) => res.json());
37+
return data;
1738
}
39+
40+
if (isCSROnly()) {
41+
logger.debug('==========csr only!');
42+
return fn();
43+
}
44+
1845
let res;
1946
let rej;
2047
const p = new Promise((resolve, reject) => {
@@ -25,17 +52,7 @@ export async function fetchData(id: string): Promise<unknown | undefined> {
2552
return globalThis._MF__DATA_FETCH_ID_MAP__[id][0];
2653
}
2754

28-
const fetchDataPromise =
29-
helpers.global.nativeGlobal.__FEDERATION__.__DATA_FETCH_MAP__.get(id);
30-
if (!fetchDataPromise) {
31-
return;
32-
}
33-
34-
const fetchDataFn = await fetchDataPromise;
35-
if (!fetchDataFn) {
36-
return;
37-
}
38-
return fetchDataFn();
55+
return fn();
3956
}
4057

4158
export function getDataFetchMapKey(

packages/modernjs/src/runtime/utils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ export function getLoadedRemoteInfos(
6363
}
6464

6565
export function isSSRDowngrade() {
66-
if (typeof window === 'undefined') {
67-
return true;
68-
}
69-
7066
return window._SSR_DATA?.renderLevel !== 2;
7167
}
68+
69+
export function isCSROnly() {
70+
return window._SSR_DATA === undefined;
71+
}

0 commit comments

Comments
 (0)