Skip to content

Conversation

jacob-ebey
Copy link
Member

@jacob-ebey jacob-ebey commented Dec 25, 2024

Adds a new API for bundler plugin usage to take control over the loading of client modules:

// entry.client.tsx
import { startTransition, StrictMode } from "react";
import { hydrateRoot } from "react-dom/client";
import type { unstable_LoadRouteModuleFunction } from "react-router";
import { HydratedRouter } from "react-router/dom";

const loadRouteModule: unstable_LoadRouteModuleFunction = async (
  route,
  routeModulesCache
) => {
  if (route.id in routeModulesCache) {
    return routeModulesCache[route.id];
  }

  console.log("Running custom import logic for route module:", route.module);

  // Note: a proper implementation should handle an error here
  // We trigger a hard reload in RR's default implementation
  let routeModule = await import(route.module);

  routeModulesCache[route.id] = routeModule;
  return routeModule;
};

startTransition(() => {
  hydrateRoot(
    document,
    <StrictMode>
      <HydratedRouter unstable_loadRouteModule={loadRouteModule} />
    </StrictMode>
  );
});

Copy link

changeset-bot bot commented Dec 25, 2024

🦋 Changeset detected

Latest commit: 84753c0

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

This PR includes changesets to release 11 packages
Name Type
react-router Major
@react-router/architect Major
@react-router/cloudflare Major
@react-router/dev Major
react-router-dom Major
@react-router/express Major
@react-router/node Major
@react-router/serve Major
@react-router/fs-routes Major
@react-router/remix-routes-option-adapter Major
create-react-router 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

@brophdawg11 brophdawg11 added the review flag for team review label Apr 2, 2025
@brophdawg11
Copy link
Contributor

Note - this new API would need to be added to the new import() calls for split route modules

@brophdawg11 brophdawg11 self-assigned this Apr 3, 2025
@brophdawg11 brophdawg11 added pkg:react-router and removed review flag for team review labels Apr 3, 2025
@brophdawg11 brophdawg11 added the alpha-release Used by maintainers to trigger a Stage 2 (alpha) release on a PR label Jul 1, 2025
Copy link
Contributor

github-actions bot commented Jul 1, 2025

Alpha release created: 0.0.0-experimental-1d8f3ee

⚠️ Note: This release was created from the HEAD of this branch so it may contain commits that have landed in dev but have not been released yet depending on when this branch was created. You can run the following command to see the commits that may not have been released yet:

git log --pretty=oneline [email protected]

@github-actions github-actions bot removed the alpha-release Used by maintainers to trigger a Stage 2 (alpha) release on a PR label Jul 1, 2025
@@ -582,6 +582,9 @@ export function RSCHydratedRouter({
},
routeDiscovery: { mode: "lazy", manifestPath: "/__manifest" },
routeModules: {},
loadRouteModule: () => {
throw new Error("Not required in RSC Mode");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jacob-ebey Can you confirm this is correct?

@brophdawg11 brophdawg11 added the alpha-release Used by maintainers to trigger a Stage 2 (alpha) release on a PR label Jul 1, 2025
Copy link
Contributor

github-actions bot commented Jul 1, 2025

Alpha release created: 0.0.0-experimental-235eba2

⚠️ Note: This release was created from the HEAD of this branch so it may contain commits that have landed in dev but have not been released yet depending on when this branch was created. You can run the following command to see the commits that may not have been released yet:

git log --pretty=oneline [email protected]

@github-actions github-actions bot removed the alpha-release Used by maintainers to trigger a Stage 2 (alpha) release on a PR label Jul 1, 2025
@brophdawg11 brophdawg11 added the alpha-release Used by maintainers to trigger a Stage 2 (alpha) release on a PR label Jul 24, 2025
@brophdawg11 brophdawg11 removed their assignment Jul 24, 2025
Copy link
Contributor

Alpha release created: 0.0.0-experimental-84753c0

⚠️ Note: This release was created from the HEAD of this branch so it may contain commits that have landed in dev but have not been released yet depending on when this branch was created. You can run the following command to see the commits that may not have been released yet:

git log --pretty=oneline [email protected]

@github-actions github-actions bot removed the alpha-release Used by maintainers to trigger a Stage 2 (alpha) release on a PR label Jul 24, 2025
@brophdawg11
Copy link
Contributor

@ScriptedAlchemy - [email protected] has this included if you want to give it a shot:

function customLoadRouteModule(route, routeModulesCache) {
  if (route.id in routeModulesCache) {
    return routeModulesCache[route.id] as RouteModule;
  }

  try {
    // Load the module however you want
    let routeModule = loadModulehoweverYouWant(route.module); // 👈
    routeModulesCache[route.id] = routeModule;
    return routeModule;
  } catch (error: unknown) {
    // Handle errors - see default handling in
    // https://github.com/remix-run/react-router/blob/browser_module_loading/packages/react-router/lib/dom/ssr/routeModules.ts#L276 
  }
}

<HydratedRouter unstable_loadRouteModule={customLoadRouteModule} />

Would you want to take full control here including error handling or would you prefer letting RR continue to handle the errors in a default manner?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow configuration of how route modules are loaded in the browser
2 participants