@equinor/fusion-framework-react-router

中文文档
2.4.8发布于 2026/11/12ISCTypeScriptES Module近 30 天下载 6672

DSL API for defining React Router v7 routes with type safety

原始使用文档
文档目录11 节
  1. Features
  2. Installation
  3. Quick start
  4. 1. Define page modules
  5. 2. Build the route tree
  6. 3. Mount the Router
  7. Passing custom context
  8. Generating route schemas
  9. Vite plugin
  10. Entry points
  11. Documentation

@equinor/fusion-framework-react-router

Opinionated React Router v7 integration for Fusion Framework applications. Rather than wiring up navigation history, basename, and request context by hand, this package does it for you and surfaces everything as a typed fusion object inside every loader, action, and component.

The package ships three things that work together:

  • Router component — drop-in replacement for RouterProvider. Reads history and basename from the Fusion navigation module and injects fusion.modules and fusion.context into every route.
  • File-route DSL — layout, index, route, and prefix helpers that build a RouteObject tree from file paths instead of raw objects. The Vite plugin transforms them into lazy-loaded data routes at build time.
  • Route schema generation — toRouteSchema converts handle.route metadata into a flat, machine-readable manifest. Useful for portal navigation trees and app documentation.

Features

  • Fusion navigation integration — reads history and basename from the Fusion navigation module automatically.
  • Typed fusion context everywhere — loaders, actions, components, and error elements receive fusion.modules and fusion.context without threading props or React contexts.
  • File-style route DSL — layout, index, route, prefix helpers replace hand-built RouteObject trees.
  • Route schema generation — handle.route metadata can be turned into a flat, machine-readable schema for manifests, portals, and documentation.
  • Module augmentation — extend RouterContext and RouterHandle to type your own context and handle fields.
  • Vite plugin — statically transforms DSL calls into optimised React Router data routes at build time.

Installation

pnpm add @equinor/fusion-framework-react-router

Prerequisites

  • React 18+ (react is a required peer dependency)
  • react-dom 18+ (optional peer dependency, required for browser rendering)

All other Fusion Framework packages (@equinor/fusion-framework-module-navigation, @equinor/fusion-framework-react-module) are bundled dependencies — they install automatically.

[!IMPORTANT] Installing @equinor/fusion-framework-module-navigation is not enough. It must also be enabled in your app's configurator via enableNavigation(configurator, ...). Router reads history and basename from the module at runtime and will throw if the module is not present. See the getting started guide for the full configurator setup.

Quick start

[!NOTE] The examples below skip the Fusion app configurator setup. See the getting started guide for full setup instructions.

1. Define page modules

A page module is a plain TypeScript or TSX file that React Router loads lazily when its route is matched. Export a default component to render the page. The Vite plugin scans each file for additional named exports and wires them in automatically — you do not need to register them anywhere.

The following named exports are recognised:

ExportDescription
defaultRequired. The page component.
clientLoaderRoute loader — called before render, receives fusion context.
actionRoute action — handles form submissions and mutations.
handleRoute handle metadata, available via useMatches().
ErrorElementError boundary component rendered when the route throws.
HydrateFallbackShown during hydration while clientLoader resolves.
shouldRevalidateControls whether the route re-runs its loader after navigation or an action. Return false to suppress revalidation.

[!NOTE] Any other named export is silently ignored by the plugin. You can co-locate utilities, hooks, and helpers in the same file without side effects.

// src/pages/ProductPage.tsx
import type {
  LoaderFunctionArgs,
  RouteComponentProps,
  RouterHandle,
} from '@equinor/fusion-framework-react-router';

export const handle = {
  route: {
    description: 'Product page',
    params: { id: 'Product identifier' },
  },
} satisfies RouterHandle;

export async function clientLoader({ params, fusion }: LoaderFunctionArgs<{ id: string | undefined }>) {
  const client = fusion.modules.http.createHttpClient('products');
  return client.json(`/products/${params.id}`);
}

export default function ProductPage({ loaderData }: RouteComponentProps<{ name: string }>) {
  return <h1>{loaderData.name}</h1>;
}

2. Build the route tree

The route DSL lets you describe a RouteObject tree using file paths instead of importing every component up front. layout wraps children in a shared component (header, nav, shell), index declares the default child route, route maps a path segment to a file, and prefix groups routes under a common segment without a layout component.

[!TIP] File paths are resolved relative to the file that calls them, not the project root. './ProductPage.tsx' in src/pages/index.ts resolves to src/pages/ProductPage.tsx.

[!CAUTION] Use prefix when you only need to group routes under a URL segment. Use layout only when those routes also share a wrapper component. Wrapping in a layout without a real layout file adds an unnecessary render boundary.

// src/pages/index.ts
import { layout, index, route, prefix } from '@equinor/fusion-framework-react-router/routes';

export const pages = layout('./MainLayout.tsx', [
  index('./HomePage.tsx'),
  prefix('products', [
    index('./ProductsPage.tsx'),
    route(':id', './ProductPage.tsx'),
  ]),
]);

3. Mount the Router

Router is a thin wrapper around React Router's RouterProvider. It reads history and basename from the Fusion navigation module so you do not have to configure them manually. Pass the route tree from step 2 as routes.

// src/Router.tsx
import { Router } from '@equinor/fusion-framework-react-router';
import { pages } from './pages';

export default function AppRouter() {
  return <Router routes={pages} />;
}

Passing custom context

The fusion object available in loaders, actions, and components always contains fusion.modules. If you also need to pass application-level dependencies — a query client, an API instance, environment config — add them to fusion.context using TypeScript module augmentation and the context prop on Router.

Declare additional properties on RouterContext in a .d.ts file:

[!TIP] Put the augmentation in a .d.ts file (e.g. router-context.d.ts) that TypeScript picks up via your tsconfig.json include glob, not inside a regular .ts file that imports from the package. Placing it in a .ts file can create circular type dependencies.

// router-context.d.ts
declare module '@equinor/fusion-framework-react-router' {
  interface RouterContext {
    queryClient: import('@tanstack/react-query').QueryClient;
  }
}
import { Router } from '@equinor/fusion-framework-react-router';
import { QueryClient } from '@tanstack/react-query';
import { pages } from './pages';

const queryClient = new QueryClient();
export default function AppRouter() {
  return <Router routes={pages} context={{ queryClient }} />;
}

Loaders and components then access fusion.context.queryClient with full type safety.

Generating route schemas

toRouteSchema walks the route tree and converts each handle.route object into a flat list of route descriptors. The output is a machine-readable manifest that portals and documentation tools can consume — for example to populate a navigation sidebar or register app routes with a portal registry.

import { toRouteSchema } from '@equinor/fusion-framework-react-router/schema';
import { pages } from './pages';

const schema = await toRouteSchema(pages);

Vite plugin

The Vite plugin is required when using the file-route DSL. Fusion Framework CLI applications enable it automatically. If you configure Vite yourself, install @equinor/fusion-framework-vite-plugin-react-router as a development dependency and add the plugin to your Vite configuration.

[!WARNING] Without the Vite plugin, the file-route DSL does not work. The DSL objects (layout, route, index, etc.) carry only a file path string — they have no Component, loader, or action wired in. The Vite plugin performs that transformation at build time. Without it, routes will render blank pages. Code-splitting is an additional benefit, not the primary one.

// vite.config.ts
import { reactRouterPlugin } from '@equinor/fusion-framework-vite-plugin-react-router';

export default defineConfig({
  plugins: [react(), reactRouterPlugin()],
});

The legacy @equinor/fusion-framework-react-router/vite-plugin entry point remains available as a compatibility re-export.

Entry points

Entry pointPurpose
@equinor/fusion-framework-react-routerMain — Router, hooks, context helpers, types, and React Router re-exports
/routesRoute DSL — layout, index, route, prefix
/schemaSchema generation — toRouteSchema
/contextDirect context imports — same routerContext, FusionRouterContextProvider, useRouterContext as main; use when you need to import without pulling in Router (e.g. tests, custom providers)
/vite-pluginCompatibility re-export of @equinor/fusion-framework-vite-plugin-react-router
/interopInterop re-exports for teams mid-migration — see docs/interop.md

Documentation

版本与发布记录

@equinor/fusion-framework-react-router 共发布 30 个版本,首次发布于 2025/12/04;过去 12 个月发布了 30 个版本。

版本发布日期距今
2.4.8latest2026/11/121 天前
2.4.8-next.1next2026/11/112 天前
2.4.8-next.02026/10/2816 天前
2.4.72026/10/2124 天前
2.4.62026/10/1727 天前
2.4.6-trusted-publishing-20261015-9dfe7602cd2a49a8a9df00d62c26e7304a82a85atrusted-publishing-202610152026/10/1629 天前

关于 @equinor/fusion-framework-react-router 的常见问题

@equinor/fusion-framework-react-router 是什么?

DSL API for defining React Router v7 routes with type safety

如何安装 @equinor/fusion-framework-react-router?

在项目目录运行 npm install @equinor/fusion-framework-react-router,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^18.0.0 || ^19.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@equinor/fusion-framework-react-router 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@equinor/fusion-framework-react-router 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@equinor/fusion-framework-react-router 可以商用吗?

@equinor/fusion-framework-react-router 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@equinor/fusion-framework-react-router 的下载量有多少?

根据 npm 官方下载统计,@equinor/fusion-framework-react-router 在 2026/10/14 – 2026/11/12 这 30 天内被下载 6,672 次,日均约 222 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@equinor/fusion-framework-react-router 还在维护吗?

最新版本 2.4.8 发布于 2026/11/12(1 天前);至今共发布 30 个版本,其中过去 12 个月发布了 30 个;首次发布于 2025/12/04。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。