From 84cb5f516a3ca31b7afb12bd2abaa8473c66e867 Mon Sep 17 00:00:00 2001 From: 2heal1 Date: Wed, 30 Oct 2024 15:39:04 +0800 Subject: [PATCH] fix(modern-js-plugin): prevent components render multiple times if props change --- .changeset/tender-insects-agree.md | 5 + .../src/runtime/createRemoteSSRComponent.tsx | 95 ++++++++++--------- 2 files changed, 53 insertions(+), 47 deletions(-) create mode 100644 .changeset/tender-insects-agree.md diff --git a/.changeset/tender-insects-agree.md b/.changeset/tender-insects-agree.md new file mode 100644 index 0000000000..669651dae7 --- /dev/null +++ b/.changeset/tender-insects-agree.md @@ -0,0 +1,5 @@ +--- +'@module-federation/modern-js': patch +--- + +fix(modern-js-plugin): prevent components render multiple times if props change diff --git a/packages/modernjs/src/runtime/createRemoteSSRComponent.tsx b/packages/modernjs/src/runtime/createRemoteSSRComponent.tsx index 9dad1de8c0..fa85c1786d 100644 --- a/packages/modernjs/src/runtime/createRemoteSSRComponent.tsx +++ b/packages/modernjs/src/runtime/createRemoteSSRComponent.tsx @@ -143,62 +143,63 @@ export function createRemoteSSRComponent(info: { ? ReactKey : Parameters[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 & - Record; - 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 & + Record; + 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} - - - ), - }; - } 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 = ( - { - 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) => ( + <> + {assets} + + + ), }; + } 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 = ( + { + console.log('SSR mode not support "resetErrorBoundary" !'); + }} + /> + ); + return { + default: () => FallbackNode, + }; + } + }); + return (props: ComponentType) => { + const { key, ...args } = props; return ( - + {/* @ts-ignore */} + );