Skip to content

Commit

Permalink
fix: generate route runtime module by isSSR
Browse files Browse the repository at this point in the history
  • Loading branch information
Timeless0911 committed May 17, 2024
1 parent 75fbb8e commit d459c42
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
10 changes: 8 additions & 2 deletions packages/core/src/node/runtimeModule/routeData.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { FactoryContext, RuntimeModuleID } from '.';

export async function routeVMPlugin(context: FactoryContext) {
const { routeService } = context;
const { routeService, isSSR } = context;

if (isSSR) {
return {
[RuntimeModuleID.RouteForSSR]: routeService.generateRoutesCode(true),
};
}

// client: The components of route is lazy loaded
return {
[RuntimeModuleID.RouteForClient]: routeService.generateRoutesCode(false),
[RuntimeModuleID.RouteForSSR]: routeService.generateRoutesCode(true),
};
}
5 changes: 4 additions & 1 deletion packages/theme-default/src/components/Link/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ import {
isEqualPath,
} from '@rspress/runtime';
import nprogress from 'nprogress';
import { routes } from 'virtual-routes';
import { isExternalUrl } from '@rspress/shared';
import styles from './index.module.scss';
import { scrollToTarget } from '../../logic';

const { routes } = process.env.__SSR__
? (require('virtual-routes-ssr') as typeof import('virtual-routes-ssr'))
: (require('virtual-routes') as typeof import('virtual-routes'));

export interface LinkProps extends ComponentProps<'a'> {
href?: string;
children?: React.ReactNode;
Expand Down
5 changes: 4 additions & 1 deletion packages/theme-default/src/components/Sidebar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
SidebarSectionHeader as ISidebarSectionHeader,
normalizeSlash,
} from '@rspress/shared';
import { routes } from 'virtual-routes';
import { matchRoutes, useLocation, removeBase } from '@rspress/runtime';
import { isActive, useLocaleSiteData, useSidebarData } from '../../logic';
import { NavBarTitle } from '../Nav/NavBarTitle';
Expand All @@ -16,6 +15,10 @@ import { SidebarDivider } from './SidebarDivider';
import { UISwitchResult } from '../../logic/useUISwitch';
import { SidebarSectionHeader } from './SidebarSectionHeader';

const { routes } = process.env.__SSR__
? (require('virtual-routes-ssr') as typeof import('virtual-routes-ssr'))
: (require('virtual-routes') as typeof import('virtual-routes'));

const isSidebarDivider = (
item:
| NormalizedSidebarGroup
Expand Down
6 changes: 6 additions & 0 deletions packages/theme-default/src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ declare module 'virtual-routes' {
export const routes: Route[];
}

declare module 'virtual-routes-ssr' {
export { Route } from 'node/route/RouteService';

export const routes: Route[];
}

declare module 'virtual-search-hooks' {
import type {
BeforeSearch,
Expand Down

0 comments on commit d459c42

Please sign in to comment.