Does a Nuxt host leak memory when it server-renders a microfrontend that releases more often than the host,
using the vm strategy of @module-federation/vite?
The trick: the remote's SSR entry writes a property with a Symbol key and per-release data onto
globalThis when it is evaluated. If the host evaluated remote code in an isolated realm, that property
would never show up on the host's globalThis. If it shows up and stays after newer releases, the host realm
holds every release.
| folder | what |
|---|---|
remote/ |
Vue 3.5.45 app, @module-federation/vite 1.24.1, port 4380. Standalone: Vue is bundled into both the browser and the SSR build (shared: {}, ssr.noExternal: vue). Exposes an app, not a component: ./app/server → render(props) HTML, ./app/client → mount(el, props) (hydrates). |
remote/src/app/server.ts |
on evaluation: globalThis[Symbol('federation-vite-memory/remote@<release>')] = { release, builtAt, evaluatedAt, renders, payload } (payload = 256 KB string so a retained release is visible in the heap) |
remote/scripts/release.mjs |
yarn release: a real vite build into releases/<id>/ (own base URL, buildVersion = <id>), then marks it current |
remote/scripts/serve.mjs |
yarn serve: the "CDN". /mf-manifest.json → current release (no-store), /releases/<id>/… immutable |
host/ |
Nuxt 4.5.2 + @module-federation/nuxt 0.2.0, port 4390. Home page = own content + the remote (SSR + hydration). SSR loader: @module-federation/vite/ssrEntryLoader with strategy: 'vm', manifest re-checked on every request (ssrManifestMaxAgeMs: 0). @module-federation/vite is forced to 1.24.1 via resolutions (the module pins 1.20.0). |
host/server/api/memory.get.ts |
GET /api/memory[?gc=1|last-resort]: process.memoryUsage() + heap spaces (MB), every remote Symbol on globalThis (remoteReleases), every other global added since startup (addedGlobals: __VUE_*_SETTERS__, __mf_module_cache__, __FEDERATION__, loader state…) |
scripts/releases.mjs |
cuts N releases while the host runs, waits until the host renders each one, prints memory after GC |
cd remote && yarn && yarn release && yarn servecd host && yarn && yarn build && yarn startOpen http://localhost:4390 and http://localhost:4390/api/memory?gc=1. Then, in another terminal:
cd remote && yarn releaseReload the page: it shows the new release without restarting the host, and /api/memory lists one more Symbol.
Or run a whole series (both servers running):
node scripts/releases.mjs --count 30yarn start runs node --experimental-vm-modules --expose-gc; without --experimental-vm-modules the loader
silently falls back to temp-file (then files appear in host/node_modules/.ssr-cache). yarn dev in host/
works too (sets NODE_OPTIONS=--experimental-vm-modules).
start | heapUsed 13.98 MB | rss 94.4 MB | remote globals 1 | __VUE_INSTANCE_SETTERS__ 2
r0011 | heapUsed 15.28 MB | rss 96.3 MB | remote globals 2 | __VUE_INSTANCE_SETTERS__ 3
...
r0040 | heapUsed 47.96 MB | rss 132.6 MB | remote globals 31 | __VUE_INSTANCE_SETTERS__ 32
heapUsed growth: 1159 KB per release
Full log: results/30-releases.txt.
- The remote's Symbol lands on the host's
globalThisand stays there for every release. Thevmstrategy evaluatesvm.SourceTextModules in the host's own context; there is no realm isolation. Anything a remote release writes to a global lives as long as the host process. - Linear growth, ~1.16 MB per release. 256 KB of it is the deliberate payload; the other ~900 KB is the release's
code, kept alive by the remote's bundled Vue: every Vue copy pushes a setter into
globalThis.__VUE_INSTANCE_SETTERS__/__VUE_SSR_SETTERS__(32 entries after 31 releases = host Vue + one per release). - Release switching itself works: the new release is rendered on the first request after the manifest changes,
and nothing is written to
.ssr-cache(noimport()of remote code).
- MF Vite 1.24.1 no longer emits
remoteEntry.ssr.jsby default. The remote needs anssrenvironment (builder: {}+environments.ssrinremote/vite.config.ts); thenmf-manifest.jsongetsmetaData.ssrRemoteEntry. buildVersioncomes frompackage.json(1.0.0) unlessMF_BUILD_VERSIONis set. The loader detects new releases bybuildVersion+ manifest hash;release.mjssets it to the release id.- With MF Vite 1.24.1 the
@module-federation/nuxt0.2.0 loader wrapper is bypassed. 1.24.1 resolves@module-federation/vite/ssrEntryLoaderitself, so the host runtime gets the plain upstream loader (optionsstrategy,maxAgeMsare still passed). Consequences:- the module's SSR bridge (
Symbol.for('@module-federation/nuxt:ssr-runtime')) does not exist, and a plainimport('fvm_remote/app/server')keeps the first release forever.FvmRemoteApp.vuetherefore loads through the host runtime (__FEDERATION__.__INSTANCES__→loadRemote()), whosebeforeRequesthook re-checks the manifest (it falls back to the bridge if one exists, i.e. with the module's pinned 1.20.0); - the
vmstrategy resolves the remote's bare@module-federation/runtimewith ESM conditions (dist/index.js) but the module traces only the CJS files into.output→ERR_MODULE_NOT_FOUNDand a silent fallback totemp-file. Workaround:nitro.externals.traceIncludeinhost/nuxt.config.ts; - all
@module-federation/{runtime,runtime-core,sdk,error-codes}are pinned to 2.9.3 inresolutionsso there is one runtime copy; - every release logs
[ Federation Runtime ]: The remote "…fvm_remote" is already registered(expected: the loader re-registers withforce: true).
- the module's SSR bridge (
- The remote's
vite.config.tsis evaluated once per environment: anew Date()indefinediffers between the client and SSR bundles → hydration mismatch. The build timestamp is pinned viaprocess.env.MF_BUILT_AT. - The remote's CSS files are not added to the host's SSR HTML (and not injected on client mount), so the remote uses inline styles.