diff --git a/.changeset/nice-bags-exist.md b/.changeset/nice-bags-exist.md new file mode 100644 index 000000000000..3dbc98bd638f --- /dev/null +++ b/.changeset/nice-bags-exist.md @@ -0,0 +1,5 @@ +--- +'@modern-js/runtime': patch +--- + +feat: consume MODERN_ROUTER_ID_PREFIX diff --git a/packages/runtime/plugin-runtime/src/router/runtime/index.ts b/packages/runtime/plugin-runtime/src/router/runtime/index.ts index 2329be7635c5..c66dc7039bdf 100644 --- a/packages/runtime/plugin-runtime/src/router/runtime/index.ts +++ b/packages/runtime/plugin-runtime/src/router/runtime/index.ts @@ -2,6 +2,7 @@ import { useRouteLoaderData as useRouteData } from '@modern-js/runtime-utils/rou import { routerPlugin } from './plugin'; import type { SingleRouteConfig, RouterConfig } from './types'; +declare const MODERN_ROUTER_ID_PREFIX: string | undefined; export * from '@modern-js/runtime-utils/router'; export type { SingleRouteConfig, RouterConfig }; @@ -18,7 +19,13 @@ export { Link, NavLink } from './PrefetchLink'; export type { LinkProps, NavLinkProps } from './PrefetchLink'; export const useRouteLoaderData: typeof useRouteData = (routeId: string) => { - const realRouteId = routeId.replace(/\[(.*?)\]/g, '($1)'); + const routerIdPrefix = + typeof MODERN_ROUTER_ID_PREFIX === 'string' ? MODERN_ROUTER_ID_PREFIX : ''; + let routeIdWithOutPrefix = routeId; + if (routeId.includes(routerIdPrefix)) { + routeIdWithOutPrefix = routeId.replace(routerIdPrefix, ''); + } + const realRouteId = routeIdWithOutPrefix.replace(/\[(.*?)\]/g, '($1)'); return useRouteData(realRouteId); };