Skip to content

Commit a50b000

Browse files
authored
fix(modern-js-plugin): prevent components render multiple times if props change (#3139)
1 parent b00aff2 commit a50b000

File tree

2 files changed

+53
-47
lines changed

2 files changed

+53
-47
lines changed

.changeset/tender-insects-agree.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/modern-js': patch
3+
---
4+
5+
fix(modern-js-plugin): prevent components render multiple times if props change

packages/modernjs/src/runtime/createRemoteSSRComponent.tsx

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -143,62 +143,63 @@ export function createRemoteSSRComponent<T, E extends keyof T>(info: {
143143
? ReactKey
144144
: Parameters<T[E]>[0] & ReactKey
145145
: ReactKey;
146+
const exportName = info?.export || 'default';
146147

147-
return (props: ComponentType) => {
148-
const exportName = info?.export || 'default';
149-
const LazyComponent = React.lazy(async () => {
150-
try {
151-
const m = (await info.loader()) as Record<string, React.FC> &
152-
Record<symbol, string>;
153-
if (!m) {
154-
throw new Error('load remote failed');
155-
}
156-
const moduleId = m && m[Symbol.for('mf_module_id')];
148+
const LazyComponent = React.lazy(async () => {
149+
try {
150+
const m = (await info.loader()) as Record<string, React.FC> &
151+
Record<symbol, string>;
152+
if (!m) {
153+
throw new Error('load remote failed');
154+
}
155+
const moduleId = m && m[Symbol.for('mf_module_id')];
157156

158-
const assets = collectSSRAssets({
159-
id: moduleId,
160-
});
157+
const assets = collectSSRAssets({
158+
id: moduleId,
159+
});
161160

162-
const Com = m[exportName] as React.FC;
163-
if (exportName in m && typeof Com === 'function') {
164-
return {
165-
default: () => (
166-
<>
167-
{assets}
168-
<Com {...props} />
169-
</>
170-
),
171-
};
172-
} else {
173-
throw Error(
174-
`Make sure that ${moduleId} has the correct export when export is ${String(
175-
exportName,
176-
)}`,
177-
);
178-
}
179-
} catch (err) {
180-
if (!info.fallback) {
181-
throw err;
182-
}
183-
const FallbackFunctionComponent = info.fallback;
184-
const FallbackNode = (
185-
<FallbackFunctionComponent
186-
error={err}
187-
resetErrorBoundary={() => {
188-
console.log('SSR mode not support "resetErrorBoundary" !');
189-
}}
190-
/>
191-
);
161+
const Com = m[exportName] as React.FC;
162+
if (exportName in m && typeof Com === 'function') {
192163
return {
193-
default: () => FallbackNode,
164+
default: (props: Omit<ComponentType, 'key'>) => (
165+
<>
166+
{assets}
167+
<Com {...props} />
168+
</>
169+
),
194170
};
171+
} else {
172+
throw Error(
173+
`Make sure that ${moduleId} has the correct export when export is ${String(
174+
exportName,
175+
)}`,
176+
);
195177
}
196-
});
197-
178+
} catch (err) {
179+
if (!info.fallback) {
180+
throw err;
181+
}
182+
const FallbackFunctionComponent = info.fallback;
183+
const FallbackNode = (
184+
<FallbackFunctionComponent
185+
error={err}
186+
resetErrorBoundary={() => {
187+
console.log('SSR mode not support "resetErrorBoundary" !');
188+
}}
189+
/>
190+
);
191+
return {
192+
default: () => FallbackNode,
193+
};
194+
}
195+
});
196+
return (props: ComponentType) => {
197+
const { key, ...args } = props;
198198
return (
199199
<ErrorBoundary FallbackComponent={info.fallback}>
200200
<React.Suspense fallback={info.loading}>
201-
<LazyComponent />
201+
{/* @ts-ignore */}
202+
<LazyComponent {...args} />
202203
</React.Suspense>
203204
</ErrorBoundary>
204205
);

0 commit comments

Comments
 (0)