Skip to content

Commit

Permalink
fix(modern-js-plugin): prevent components render multiple times if pr…
Browse files Browse the repository at this point in the history
…ops change (#3139)
  • Loading branch information
2heal1 authored Nov 15, 2024
1 parent b00aff2 commit a50b000
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 47 deletions.
5 changes: 5 additions & 0 deletions .changeset/tender-insects-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@module-federation/modern-js': patch
---

fix(modern-js-plugin): prevent components render multiple times if props change
95 changes: 48 additions & 47 deletions packages/modernjs/src/runtime/createRemoteSSRComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,62 +143,63 @@ export function createRemoteSSRComponent<T, E extends keyof T>(info: {
? ReactKey
: Parameters<T[E]>[0] & ReactKey
: ReactKey;
const exportName = info?.export || 'default';

return (props: ComponentType) => {
const exportName = info?.export || 'default';
const LazyComponent = React.lazy(async () => {
try {
const m = (await info.loader()) as Record<string, React.FC> &
Record<symbol, string>;
if (!m) {
throw new Error('load remote failed');
}
const moduleId = m && m[Symbol.for('mf_module_id')];
const LazyComponent = React.lazy(async () => {
try {
const m = (await info.loader()) as Record<string, React.FC> &
Record<symbol, string>;
if (!m) {
throw new Error('load remote failed');
}
const moduleId = m && m[Symbol.for('mf_module_id')];

const assets = collectSSRAssets({
id: moduleId,
});
const assets = collectSSRAssets({
id: moduleId,
});

const Com = m[exportName] as React.FC;
if (exportName in m && typeof Com === 'function') {
return {
default: () => (
<>
{assets}
<Com {...props} />
</>
),
};
} else {
throw Error(
`Make sure that ${moduleId} has the correct export when export is ${String(
exportName,
)}`,
);
}
} catch (err) {
if (!info.fallback) {
throw err;
}
const FallbackFunctionComponent = info.fallback;
const FallbackNode = (
<FallbackFunctionComponent
error={err}
resetErrorBoundary={() => {
console.log('SSR mode not support "resetErrorBoundary" !');
}}
/>
);
const Com = m[exportName] as React.FC;
if (exportName in m && typeof Com === 'function') {
return {
default: () => FallbackNode,
default: (props: Omit<ComponentType, 'key'>) => (
<>
{assets}
<Com {...props} />
</>
),
};
} else {
throw Error(
`Make sure that ${moduleId} has the correct export when export is ${String(
exportName,
)}`,
);
}
});

} catch (err) {
if (!info.fallback) {
throw err;
}
const FallbackFunctionComponent = info.fallback;
const FallbackNode = (
<FallbackFunctionComponent
error={err}
resetErrorBoundary={() => {
console.log('SSR mode not support "resetErrorBoundary" !');
}}
/>
);
return {
default: () => FallbackNode,
};
}
});
return (props: ComponentType) => {
const { key, ...args } = props;
return (
<ErrorBoundary FallbackComponent={info.fallback}>
<React.Suspense fallback={info.loading}>
<LazyComponent />
{/* @ts-ignore */}
<LazyComponent {...args} />
</React.Suspense>
</ErrorBoundary>
);
Expand Down

0 comments on commit a50b000

Please sign in to comment.