Skip to content

[Bug]: 504 Outdated Optimize Dep errors when using shared dependencies in dev mode #376

@AlonMiz

Description

@AlonMiz

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.4
  • vite: 7.3.1

Reproduction

https://github.com/user/analog-federation (Nx monorepo with Angular + React remotes)

Steps:

  1. Configure shared dependencies in federation config
  2. Run vite dev
  3. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions