Skip to content

feat: support MF preloadRemote through PrefetchPlugin#1136

Merged
jbroma merged 10 commits intomainfrom
feat/prefetch-plugin
Apr 30, 2025
Merged

feat: support MF preloadRemote through PrefetchPlugin#1136
jbroma merged 10 commits intomainfrom
feat/prefetch-plugin

Conversation

@jbroma
Copy link
Copy Markdown
Contributor

@jbroma jbroma commented Apr 28, 2025

Summary

This PR introduces prefetching capabilities for Module Federation in React Native with Re.Pack. The changes allow for eager loading of remote modules and their assets, improving the user experience by reducing loading times when navigating to federated components.

Requirements

  • @module-federation/runtime installed
  • persistent storage is configured (e.g. @react-native-async-storage/async-storage)

Usage Example

In your index.js, do:

import { ScriptManager } from '@callstack/repack/client';
import { preloadRemote } from '@module-federation/runtime';
import AsyncStorage from '@react-native-async-storage/async-storage';

// enable caching of scripts in the AsyncStorage
ScriptManager.shared.setStorage(AsyncStorage);

// optionally setup a listener for prefetching events
ScriptManager.shared.on('prefetching', (script) => {
  console.debug('prefetching', script.locator.uniqueId);
});

// preload eagerly on startup
// prefer to use resourceCategory: 'sync' to prevent over-fetching 
preloadRemote([
  { nameOrAlias: 'MiniApp', resourceCategory: 'sync', depsRemote: false },
])
  .then(() => {
    console.log('preloaded MiniApp assets');
  })
  .catch((e) => {
    // preloadRemote will fail if the remote entry is not a manifest
    console.error('error preloading MiniApp assets');
    console.error(e);
  });

Configuration Options

See https://module-federation.io/guide/basic/runtime.html#preloadremote

Test Plan

  • - testers work
  • - tests pass

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Apr 28, 2025

🦋 Changeset detected

Latest commit: 4b6a928

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 6 packages
Name Type
@callstack/repack Major
@callstack/repack-plugin-expo-modules Major
@callstack/repack-plugin-nativewind Major
@callstack/repack-plugin-reanimated Major
@callstack/repack-dev-server Major
@callstack/repack-init Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 28, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
repack-website ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 30, 2025 10:19am

@jbroma jbroma marked this pull request as ready for review April 30, 2025 09:13
@jbroma jbroma changed the title feat: MF PrefetchPlugin feat: support MF preloadRemote through PrefetchPlugin Apr 30, 2025
@jbroma jbroma merged commit 55a56c3 into main Apr 30, 2025
5 checks passed
@jbroma jbroma deleted the feat/prefetch-plugin branch April 30, 2025 10:22
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