Skip to content

Commit bc01160

Browse files
committed
chore: revert noSSR option
1 parent db11904 commit bc01160

File tree

2 files changed

+69
-74
lines changed

2 files changed

+69
-74
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { getInstance } from '@module-federation/modern-js/runtime';
2-
import { NoSSR } from '@modern-js/runtime/ssr';
32

43
const CsrWithFetchDataFromServerComponent = getInstance()!.createLazyComponent({
4+
noSSR: true,
55
loader: () => {
6+
console.log('calling');
67
return import('provider-csr');
78
},
89
loading: 'loading...',
@@ -22,9 +23,7 @@ const Index = (): JSX.Element => {
2223
<h1>
2324
The component will be render in csr but <i>fetch data from server</i>
2425
</h1>
25-
<NoSSR>
26-
<CsrWithFetchDataFromServerComponent />
27-
</NoSSR>
26+
<CsrWithFetchDataFromServerComponent />
2827
</div>
2928
);
3029
};

packages/bridge/bridge-react/src/lazy/createLazyComponent.tsx

Lines changed: 66 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export type CreateLazyComponentOptions<T, E extends keyof T> = {
4242
fallback: ReactNode | ((errorInfo: ErrorInfo) => ReactNode);
4343
export?: E;
4444
dataFetchParams?: DataFetchParams;
45-
// noSSR?: boolean;
45+
noSSR?: boolean;
4646
cacheData?: boolean;
4747
};
4848

@@ -157,12 +157,11 @@ export function collectSSRAssets(options: IProps) {
157157
function getServerNeedRemoteInfo(
158158
loadedRemoteInfo: ReturnType<typeof getLoadedRemoteInfos>,
159159
id: string,
160-
// noSSR?: boolean,
160+
noSSR?: boolean,
161161
): NoSSRRemoteInfo | undefined {
162162
if (
163-
// noSSR ||
164-
typeof window !== 'undefined' &&
165-
window.location.href !== window[FS_HREF]
163+
noSSR ||
164+
(typeof window !== 'undefined' && window.location.href !== window[FS_HREF])
166165
) {
167166
if (!loadedRemoteInfo?.version) {
168167
throw new Error(`${loadedRemoteInfo?.name} version is empty`);
@@ -233,8 +232,7 @@ export function createLazyComponent<T, E extends keyof T>(
233232
return m;
234233
};
235234

236-
// const getData = async (noSSR?: boolean) => {
237-
const getData = async () => {
235+
const getData = async (noSSR?: boolean) => {
238236
let loadedRemoteInfo: ReturnType<typeof getLoadedRemoteInfos>;
239237
let moduleId: string;
240238
try {
@@ -273,8 +271,7 @@ export function createLazyComponent<T, E extends keyof T>(
273271
...options.dataFetchParams,
274272
isDowngrade: false,
275273
},
276-
// getServerNeedRemoteInfo(loadedRemoteInfo, dataFetchMapKey, noSSR),
277-
getServerNeedRemoteInfo(loadedRemoteInfo, dataFetchMapKey),
274+
getServerNeedRemoteInfo(loadedRemoteInfo, dataFetchMapKey, noSSR),
278275
);
279276
setDataFetchItemLoadedStatus(dataFetchMapKey);
280277
logger.debug('get data res: \n', data);
@@ -347,72 +344,71 @@ export function createLazyComponent<T, E extends keyof T>(
347344
// @ts-expect-error ignore
348345
return <LazyComponent {...args} mfData={dataCache} />;
349346
}
350-
return (
351-
<AwaitDataFetch
352-
// resolve={getData(options.noSSR)}
353-
resolve={getData()}
354-
loading={options.loading}
355-
delayLoading={options.delayLoading}
356-
errorElement={options.fallback}
357-
>
358-
{/* @ts-expect-error ignore */}
359-
{(data) => <LazyComponent {...args} mfData={data} />}
360-
</AwaitDataFetch>
361-
);
362-
// if (!options.noSSR) {
363347

364-
// } else {
365-
// // Client-side rendering logic
366-
// const [data, setData] = useState<unknown>(null);
367-
// const [loading, setLoading] = useState<boolean>(true);
368-
// const [error, setError] = useState<ErrorInfo | null>(null);
348+
if (!options.noSSR) {
349+
return (
350+
<AwaitDataFetch
351+
resolve={getData(options.noSSR)}
352+
loading={options.loading}
353+
delayLoading={options.delayLoading}
354+
errorElement={options.fallback}
355+
>
356+
{/* @ts-expect-error ignore */}
357+
{(data) => <LazyComponent {...args} mfData={data} />}
358+
</AwaitDataFetch>
359+
);
360+
} else {
361+
// Client-side rendering logic
362+
const [data, setData] = useState<unknown>(null);
363+
const [loading, setLoading] = useState<boolean>(true);
364+
const [error, setError] = useState<ErrorInfo | null>(null);
369365

370-
// useEffect(() => {
371-
// let isMounted = true;
372-
// const fetchDataAsync = async () => {
373-
// try {
374-
// setLoading(true);
375-
// const result = await getData(options.noSSR);
376-
// if (isMounted) {
377-
// setData(result);
378-
// }
379-
// } catch (e) {
380-
// if (isMounted) {
381-
// setError(transformError(e as Error));
382-
// }
383-
// } finally {
384-
// if (isMounted) {
385-
// setLoading(false);
386-
// }
387-
// }
388-
// };
366+
useEffect(() => {
367+
let isMounted = true;
368+
const fetchDataAsync = async () => {
369+
try {
370+
setLoading(true);
371+
const result = await getData(options.noSSR);
372+
if (isMounted) {
373+
setData(result);
374+
}
375+
} catch (e) {
376+
if (isMounted) {
377+
setError(transformError(e as Error));
378+
}
379+
} finally {
380+
if (isMounted) {
381+
setLoading(false);
382+
}
383+
}
384+
};
389385

390-
// fetchDataAsync();
386+
fetchDataAsync();
391387

392-
// return () => {
393-
// isMounted = false;
394-
// };
395-
// }, []);
388+
return () => {
389+
isMounted = false;
390+
};
391+
}, []);
396392

397-
// if (loading) {
398-
// return (
399-
// <DelayedLoading delayLoading={options.delayLoading}>
400-
// {options.loading}
401-
// </DelayedLoading>
402-
// );
403-
// }
393+
if (loading) {
394+
return (
395+
<DelayedLoading delayLoading={options.delayLoading}>
396+
{options.loading}
397+
</DelayedLoading>
398+
);
399+
}
404400

405-
// if (error) {
406-
// return (
407-
// <>
408-
// {typeof options.fallback === 'function'
409-
// ? options.fallback(error)
410-
// : options.fallback}
411-
// </>
412-
// );
413-
// }
414-
// // @ts-expect-error ignore
415-
// return <LazyComponent {...args} mfData={data} />;
416-
// }
401+
if (error) {
402+
return (
403+
<>
404+
{typeof options.fallback === 'function'
405+
? options.fallback(error)
406+
: options.fallback}
407+
</>
408+
);
409+
}
410+
// @ts-expect-error ignore
411+
return <LazyComponent {...args} mfData={data} />;
412+
}
417413
};
418414
}

0 commit comments

Comments
 (0)