Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core, plugin-content-docs): Render NotFoundContent via route if routeBasePath is / #9719

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/docusaurus-module-type-aliases/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ declare module '@theme/NotFound' {
export default function NotFound(): JSX.Element;
}

declare module '@theme/NotFound/Content' {
export default function NotFoundContent(): JSX.Element;
}

declare module '@theme/Root' {
import type {ReactNode} from 'react';

Expand Down
6 changes: 6 additions & 0 deletions packages/docusaurus-plugin-content-docs/src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,12 @@ export async function buildAllRoutes(
}),
),
);
if (param.options.routeBasePath === '/') {
subRoutes.push({
thejcannon marked this conversation as resolved.
Show resolved Hide resolved
path: '/*',
component: '@docusaurus/ComponentCreator',
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the benefit of using ComponentCreator here instead of NotFound/Content? Theoretically, couldn't the ComponentCreator("*") at the end of the routes just use @theme/NotFound?

});
}

// all docs routes are wrapped under a single parent route, this ensures
// the theme layout never unmounts/remounts when navigating between versions
Expand Down
18 changes: 18 additions & 0 deletions packages/docusaurus/src/client/exports/ComponentCreator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,24 @@ export default function ComponentCreator(
},
});
}
if (path === '/*') {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels hacky, but I'm just not familiar enough with the ecosystem or Docusaurus code to make that call. So here goes 😄

return Loadable({
loading: Loading,
loader: () => import('@theme/NotFound/Content'),
modules: ['@theme/NotFound/Content'],
webpack: () => [require.resolveWeak('@theme/NotFound/Content')],
render(loaded, props) {
const NotFoundContent = loaded.default;
return (
<RouteContextProvider
// Do we want a better name than native-default?
value={{plugin: {name: 'native', id: 'default'}}}>
<NotFoundContent {...(props as JSX.IntrinsicAttributes)} />
</RouteContextProvider>
);
},
});
}

const chunkNames = routesChunkNames[`${path}-${hash}`]!;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
8 changes: 8 additions & 0 deletions packages/docusaurus/src/server/plugins/routeConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ export function sortConfig(
return -1;
}

// Wildcard also should get placed last
if (a.path.endsWith('/*')) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't decide between a.path == "/*" or .endsWith and decided to go with endsWith since it seemed less magical.

return 1;
}
if (b.path.endsWith('/*')) {
return -1;
}

if (a.routes && !b.routes) {
return 1;
}
Expand Down