Skip to content

Commit

Permalink
refactor(core): refactor routes generation logic (#7054)
Browse files Browse the repository at this point in the history
* refactor(core): refactor routes generation logic

* fixes
  • Loading branch information
Josh-Cena authored Mar 29, 2022
1 parent e31e91e commit 7766226
Show file tree
Hide file tree
Showing 19 changed files with 545 additions and 500 deletions.
18 changes: 10 additions & 8 deletions packages/docusaurus-module-type-aliases/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,26 @@ declare module '@generated/site-metadata' {
}

declare module '@generated/registry' {
const registry: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
readonly [key: string]: [() => Promise<any>, string, string];
};
import type {Registry} from '@docusaurus/types';

const registry: Registry;
export default registry;
}

declare module '@generated/routes' {
import type {Route} from '@docusaurus/types';
import type {RouteConfig as RRRouteConfig} from 'react-router-config';

const routes: Route[];
type RouteConfig = RRRouteConfig & {
path: string;
};
const routes: RouteConfig[];
export default routes;
}

declare module '@generated/routesChunkNames' {
import type {RouteChunksTree} from '@docusaurus/types';
import type {RouteChunkNames} from '@docusaurus/types';

const routesChunkNames: {[route: string]: RouteChunksTree};
const routesChunkNames: RouteChunkNames;
export = routesChunkNames;
}

Expand Down
20 changes: 8 additions & 12 deletions packages/docusaurus-plugin-content-blog/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,19 +292,15 @@ export default async function pluginContentBlog(
exact: true,
modules: {
sidebar: aliasedSource(sidebarProp),
items: items.map((postID) =>
// To tell routes.js this is an import and not a nested object
// to recurse.
({
content: {
__import: true,
path: blogItemsToMetadata[postID]!.source,
query: {
truncated: true,
},
items: items.map((postID) => ({
content: {
__import: true,
path: blogItemsToMetadata[postID]!.source,
query: {
truncated: true,
},
}),
),
},
})),
metadata: aliasedSource(pageMetadataPath),
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import type {Route} from '@docusaurus/types';
import type {RouteConfig} from 'react-router-config';
import {findHomePageRoute, isSamePath} from '../routesUtils';

describe('isSamePath', () => {
Expand Down Expand Up @@ -41,7 +41,7 @@ describe('isSamePath', () => {
});

describe('findHomePageRoute', () => {
const homePage: Route = {
const homePage: RouteConfig = {
path: '/',
exact: true,
};
Expand Down
14 changes: 7 additions & 7 deletions packages/docusaurus-theme-common/src/utils/routesUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import {useMemo} from 'react';
import generatedRoutes from '@generated/routes';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
import type {Route} from '@docusaurus/types';
import type {RouteConfig} from 'react-router-config';

/**
* Compare the 2 paths, case insensitive and ignoring trailing slash
Expand All @@ -34,18 +34,18 @@ export function findHomePageRoute({
baseUrl,
routes: initialRoutes,
}: {
routes: Route[];
routes: RouteConfig[];
baseUrl: string;
}): Route | undefined {
function isHomePageRoute(route: Route): boolean {
}): RouteConfig | undefined {
function isHomePageRoute(route: RouteConfig): boolean {
return route.path === baseUrl && route.exact === true;
}

function isHomeParentRoute(route: Route): boolean {
function isHomeParentRoute(route: RouteConfig): boolean {
return route.path === baseUrl && !route.exact;
}

function doFindHomePageRoute(routes: Route[]): Route | undefined {
function doFindHomePageRoute(routes: RouteConfig[]): RouteConfig | undefined {
if (routes.length === 0) {
return undefined;
}
Expand All @@ -66,7 +66,7 @@ export function findHomePageRoute({
* Fetches the route that points to "/". Use this instead of the naive "/",
* because the homepage may not exist.
*/
export function useHomePageRoute(): Route | undefined {
export function useHomePageRoute(): RouteConfig | undefined {
const {baseUrl} = useDocusaurusContext().siteConfig;
return useMemo(
() => findHomePageRoute({routes: generatedRoutes, baseUrl}),
Expand Down
Loading

0 comments on commit 7766226

Please sign in to comment.