-
Notifications
You must be signed in to change notification settings - Fork 95
Description
Describe the bug
When using @module-federation/vite with shared dependencies in dev mode, Vite throws "504 Outdated Optimize Dep" errors.
Root Cause: The issue is in src/utils/normalizeOptimizeDeps.ts line 17:
// todo: fix this workaround
optimizeDeps.force = true;The plugin sets optimizeDeps.force = true, which forces Vite to re-optimize dependencies on every server start. When combined with shared dependencies, this causes a race condition where the federation plugin receives a 504 error because the optimized dependency it was loading becomes "outdated".
Current Workaround: Disable shared dependencies in dev mode:
shared: isDev ? {} : { /* shared deps */ }Version
@module-federation/vite: 1.9.4vite: 7.3.1
Reproduction
https://github.com/user/analog-federation (Nx monorepo with Angular + React remotes)
Steps:
- Configure shared dependencies in federation config
- Run
vite dev - Observe 504 errors in console
Relevant log output
504 Outdated Optimize Dep
Suggested Fix
Instead of always forcing re-optimization, the plugin should use a smarter invalidation strategy:
Option 1: Hash-based invalidation
Only force re-optimization when the federation config actually changes:
// In normalizeOptimizeDeps.ts
import { createHash } from 'crypto';
function getFederationConfigHash(config: FederationConfig): string {
const configString = JSON.stringify({
name: config.name,
remotes: config.remotes,
exposes: config.exposes,
shared: config.shared,
});
return createHash('md5').update(configString).digest('hex');
}
// Instead of: optimizeDeps.force = true;
// Check if config changed since last run
const federationHash = getFederationConfigHash(federationConfig);
if (hasFederationConfigChanged(federationHash)) {
optimizeDeps.force = true;
saveFederationConfigHash(federationHash);
}Option 2: Use optimizeDeps.entries instead of force
Pre-specify federation modules without forcing full re-optimization:
// Don't force, instead explicitly include federation entries
// optimizeDeps.force = true; // REMOVE THIS
optimizeDeps.entries = [
...(optimizeDeps.entries || []),
'**/remoteEntry.js',
];
// Include shared deps explicitly
if (federationConfig.shared) {
optimizeDeps.include = [
...(optimizeDeps.include || []),
...Object.keys(federationConfig.shared),
];
}Validations
- Read the docs.
- Read the common issues list.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- The provided reproduction is a minimal reproducible example of the bug.