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 ecc54f5
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 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: 3 additions & 2 deletions packages/theme-default/src/components/Link/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ 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';
Expand Down Expand Up @@ -68,7 +67,9 @@ export function Link(props: LinkProps) {
}
return;
}

const { routes } = process.env.__SSR__
? (require('virtual-routes-ssr') as typeof import('virtual-routes-ssr'))
: (require('virtual-routes') as typeof import('virtual-routes'));
// handle normal link
if (!process.env.__SSR__ && !inCurrentPage) {
const matchedRoutes = matchRoutes(
Expand Down
4 changes: 3 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 Down Expand Up @@ -136,6 +135,9 @@ export function Sidebar(props: Props) {
removeLangPrefix(path),
true,
);
const { routes } = process.env.__SSR__
? (require('virtual-routes-ssr') as typeof import('virtual-routes-ssr'))
: (require('virtual-routes') as typeof import('virtual-routes'));
const preloadLink = (link: string) => {
const match = matchRoutes(routes, link);
if (match?.length) {
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 ecc54f5

Please sign in to comment.