Fetchalo Plugin
fetchalo is a community plugin that loads
JS, CSS and media from the origins you choose through your own fetch. Every
request to those origins can then carry headers such as
Authorization: Bearer …, without a service worker.
Browsers cannot add headers to <script type="module">, import(), <link>,
<img> or CSS url() requests. Service workers can, but only in a secure
context (HTTPS or localhost) and one per scope. fetchalo also works on plain
HTTP hosts, for example remotes on a LAN behind an API key.
With Module Federation, the fetchalo/federation runtime plugin loads the
remotes' manifests, module/esm remote entries, chunks, CSS and media through
fetchalo.
Install
Peer dependencies (all optional): webpack 5, @rspack/core or vite 5-8.
Configuration
The plugin can be applied at compile-time or runtime
With Rspack or webpack, FetchaloPlugin prepends the fetchalo client, the
config file and es-module-shims to every entry, so they run
before the app. With Vite, the fetchalo plugin injects them at the top of
<head>. In both cases fetchalo/federation in runtimePlugins loads the
manifest and the remote entry through fetchalo.
With the Module Federation runtime API, pass the origins and headers to
fetchalo/federation directly: it installs fetchalo and es-module-shims in the
page when it is created. mf.registerPlugins([fetchaloFederation({ ... })])
also works, as long as it runs before the first remote loads. Several instances
share the first installation. In a webpack or Rspack host, use the build plugin
instead: it keeps the bundler from parsing es-module-shims.
With Vite, you can also install fetchalo through runtimePlugins alone,
without the Vite plugin: list a file of your own that returns
fetchaloFederation({ origins, headers }). Files the app requests before the
federation runtime starts are then not covered. See the
fetchalo README.
Config file
The build plugins take the path of a config file (.js or .ts) and load it in
the browser before the app. Its default export holds:
origins: the origins whose files get the headers.headers: called for every request, so it can return a refreshed token.
The config file must not import files from the origins. It is bundled with
the app, so it can read build-time values such as import.meta.env with Vite
or DefinePlugin constants with webpack/Rspack. For
values computed in the browser, export a function that returns the config, or a
promise of it:
fetchaloFederation() in the Module Federation Runtime tab also accepts a
promise of { origins, headers }. Until a config given as a promise resolves,
requests to the origins wait for it, and window.fetchalo.ready resolves when
it has.
API
CORS
The remote server must answer the CORS preflight for the header, with
Access-Control-Allow-Origin and Access-Control-Allow-Headers: Authorization.
Requests covered
For files on the configured origins:
<link rel="modulepreload|preload|prefetch"> hints to those origins are
dropped: es-module-shims fetches the modules, and other files are fetched when
used.
Limitations
- Browser only, no SSR.
- Only
module/esmremotes from built output.varremotes load through<script>tags, which cannot carry headers. - Not covered: elements written in the page's HTML, SVG
<image>,<iframe>,<object>, workers,adoptedStyleSheets, elements created withinnerHTML, andfetch()calls made by the loaded code itself. - Media is downloaded whole into a
blob:URL: no streaming or range requests.blob:URLs are kept for the page's lifetime. - It patches DOM prototypes (
appendChild,setAttribute,insertRule, media setters), for the configured origins only.
Examples
The fetchalo repository has
complete examples that
run in Chromium, Firefox and WebKit: a remote that serves its files only with
an Authorization header, and hosts built with webpack, Rspack, Vite and the
Module Federation runtime API.