Skip to content

feat: load remotes from a URL with an offline cache - #12

Merged
Nsttt merged 1 commit into
mainfrom
feat/remote-offline-cache
Nov 6, 2026
Merged

Nsttt merged 1 commit into
mainfrom
feat/remote-offline-cache

Conversation

@Nsttt

@Nsttt Nsttt commented Nov 5, 2026

Copy link
Copy Markdown
Member

Refs #11 (step 1: offline cache; last-known-good rollback follows separately)

What

serveFederatedApps accepts an http(s) URL as an app location:

serveFederatedApps({
  apps: {
    host: '/path/to/host/dist',
    remote: 'https://cdn.example.com/remote/',
  },
});

mf://local/<app-id>/<path> is fetched from <url>/<path> in the main process. The renderer still sees one origin, so this needs no runtime plugin and no CORS setup, and mf-manifest.json remotes are covered (they aren't in @module-federation/tauri).

Offline cache (ported from @module-federation/tauri)

  • Network first. Successful GET responses are cached on disk with their headers.
  • When the network fails, times out or answers an error status, the cached copy is served with x-mf-cache: stale. Error responses are never cached.
  • Connect timeout and read inactivity timeout, 10 s each. After a failure, cached requests to that origin skip the network for 30 s.
  • Query strings are ignored in cache keys. Each entry is one file (a JSON line of status and headers, then the body), written through a rename.
  • Not replayed: hop-by-hop headers, content-encoding / content-length (the fetch already decoded the body) and set-cookie. Conditional and Origin request headers aren't forwarded.
  • Options: cache: { dir, connectTimeout, readTimeout }, or cache: false to proxy without caching. The default directory is <sessionData>/module-federation.

The proxy and cache live in src/remote.ts, which doesn't import electron (the fetch function is injected), so they are unit tested in Node.

Tests

  • test/remote.test.ts: 18 tests against a local HTTP server, covering offline fallback, restart, refresh, broken deploys, connect and stalled-body timeouts, truncated bodies, the 30 s window, gzip, header forwarding, non-GET requests and path traversal.
  • pnpm smoke now loads each host three times: from dist, with the remotes fetched from a local HTTP server, and after that server stops. The last pass also checks that the manifests are served from the cache.

serveFederatedApps accepts an http(s) URL as an app location. Requests for
mf://local/<app-id>/ are fetched from it in the main process, network first,
with an on-disk copy served when the network fails, times out or answers an
error status. Ported from @module-federation/tauri.

The smoke test now also loads the remotes over HTTP, then again from the
cache after the server stops.

Refs #11
@pkg-pr-new

pkg-pr-new Bot commented Nov 5, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/module-federation/electron/@module-federation/electron@12

commit: 8a4e04c

@Nsttt Nsttt assigned Nsttt and unassigned Nsttt Nov 6, 2026
@Nsttt
Nsttt merged commit fbd816a into main Nov 6, 2026
7 checks passed
@Nsttt
Nsttt deleted the feat/remote-offline-cache branch November 6, 2026 10:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant