Skip to content

Commit

Permalink
docs(core): show 404 content instead of redirecting to intro (#11788)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysoo authored Aug 30, 2022
1 parent c7249db commit 395fa4b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 34 deletions.
4 changes: 3 additions & 1 deletion nx-dev/nx-dev/pages/404.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Footer, Header } from '@nrwl/nx-dev/ui-common';
import { NextSeo } from 'next-seo';
import Link from 'next/link';

export default function FourOhFour(): JSX.Element {
export function FourOhFour(): JSX.Element {
return (
<>
<NextSeo title="Page not found" noindex={true} />
Expand Down Expand Up @@ -51,3 +51,5 @@ export default function FourOhFour(): JSX.Element {
</>
);
}

export default FourOhFour;
72 changes: 39 additions & 33 deletions nx-dev/nx-dev/pages/[...segments].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import { PackageMetadata } from '@nrwl/nx-dev/models-package';
import { Footer, Header } from '@nrwl/nx-dev/ui-common';
import cx from 'classnames';
import Router from 'next/router';
import type { GetStaticPaths, GetStaticProps } from 'next';
import { useCallback, useEffect, useState } from 'react';

import { FourOhFour } from './404';
import {
nxCloudDocumentsApi,
nxCloudMenuApi,
Expand All @@ -18,15 +21,18 @@ import {
packagesApi,
} from '../lib/api';

interface DocumentationPageProps {
menu: Menu;
document: DocumentData | null;
pkg: PackageMetadata | null;
schemaRequest: {
type: 'executors' | 'generators';
schemaName: string;
} | null;
}
type DocumentationPageProps =
| { statusCode: 404 }
| {
statusCode: 200;
menu: Menu;
document: DocumentData | null;
pkg: PackageMetadata | null;
schemaRequest: {
type: 'executors' | 'generators';
schemaName: string;
} | null;
};

// We may want to extract this to another lib.
function useNavToggle() {
Expand Down Expand Up @@ -100,14 +106,17 @@ function SidebarButton(props: { onClick: () => void; navIsOpen: boolean }) {
);
}

export default function DocumentationPage({
menu,
document,
pkg,
schemaRequest,
}: DocumentationPageProps): JSX.Element {
export default function DocumentationPage(
props: DocumentationPageProps
): JSX.Element {
const { toggleNav, navIsOpen } = useNavToggle();

if (props.statusCode === 404) {
return <FourOhFour />;
}

const { menu, document, pkg, schemaRequest } = props;

const vm: { entryComponent: JSX.Element } = {
entryComponent: (
<DocViewer
Expand Down Expand Up @@ -148,11 +157,11 @@ export default function DocumentationPage({
);
}

export async function getStaticProps({
export const getStaticProps: GetStaticProps = async ({
params,
}: {
params: { segments: string[] };
}) {
}) => {
// Set Document and Menu apis
let documentsApi = nxDocumentsApi;
let menuApi = nxMenuApi;
Expand All @@ -174,11 +183,9 @@ export async function getStaticProps({
// TODO@ben: handle packages view routes?
if (!pkg || (params.segments.length < 4 && 2 < params.segments.length)) {
return {
redirect: {
// If the menu is found, go to the first document, else go to homepage
destination:
menu?.sections[0].itemList?.[0].itemList?.[0].path ?? '/',
permanent: false,
notFound: true,
props: {
statusCode: 404,
},
};
}
Expand Down Expand Up @@ -223,18 +230,17 @@ export async function getStaticProps({
schemaRequest: null,
},
};
} else {
return {
redirect: {
// If the menu is found, go to the first document, else go to homepage
destination: menu?.sections[0].itemList?.[0].itemList?.[0].path ?? '/',
permanent: false,
},
};
}
}

export async function getStaticPaths() {
return {
notFound: true,
props: {
statusCode: 404,
},
};
};

export const getStaticPaths: GetStaticPaths = () => {
return {
paths: [
...packagesApi.getStaticPackagePaths(),
Expand All @@ -243,4 +249,4 @@ export async function getStaticPaths() {
],
fallback: 'blocking',
};
}
};

1 comment on commit 395fa4b

@vercel
Copy link

@vercel vercel bot commented on 395fa4b Aug 30, 2022

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-git-master-nrwl.vercel.app
nx-dev-nrwl.vercel.app
nx-five.vercel.app
nx.dev

Please sign in to comment.