Skip to content

Commit

Permalink
feat: consume MODERN_ROUTER_ID_PREFIX
Browse files Browse the repository at this point in the history
  • Loading branch information
2heal1 committed Aug 27, 2024
1 parent fde3ab7 commit e0696d3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/nice-bags-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@modern-js/runtime': patch
---

feat: consume MODERN_ROUTER_ID_PREFIX
9 changes: 8 additions & 1 deletion packages/runtime/plugin-runtime/src/router/runtime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand All @@ -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);
};

Expand Down

0 comments on commit e0696d3

Please sign in to comment.