Skip to content

Commit 6e21a2e

Browse files
authored
[Beta] Move from MDX loader to getStaticPaths (#4990)
* Move Markdown from src/pages to src/content * Use getStaticProps * Fix Fast Refresh * Clean up * rm unused * Fixes
1 parent fc4d44c commit 6e21a2e

File tree

254 files changed

+1130
-278
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

254 files changed

+1130
-278
lines changed

beta/next.config.js

-16
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,6 @@ const nextConfig = {
6464
})
6565
);
6666

67-
// Add our custom markdown loader in order to support frontmatter
68-
// and layout
69-
config.module.rules.push({
70-
test: /.mdx?$/, // load both .md and .mdx files
71-
use: [
72-
options.defaultLoaders.babel,
73-
{
74-
loader: '@mdx-js/loader',
75-
options: {
76-
remarkPlugins,
77-
},
78-
},
79-
path.join(__dirname, './plugins/md-layout-loader'),
80-
],
81-
});
82-
8367
return config;
8468
},
8569
};

beta/package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"license": "CC",
66
"scripts": {
77
"analyze": "ANALYZE=true next build",
8-
"dev": "next",
8+
"dev": "next-remote-watch ./src/content",
99
"build": "next build && node ./scripts/generateRSS.js && node ./scripts/generateRedirects.js && node ./scripts/downloadFonts.js",
1010
"lint": "next lint",
1111
"lint:fix": "next lint --fix",
@@ -34,14 +34,18 @@
3434
"ga-lite": "^2.1.4",
3535
"github-slugger": "^1.3.0",
3636
"next": "12.1.7-canary.11",
37+
"next-remote-watch": "^1.0.0",
3738
"parse-numeric-range": "^1.2.0",
3839
"react": "0.0.0-experimental-82c64e1a4-20220520",
3940
"react-collapsed": "3.1.0",
4041
"react-dom": "0.0.0-experimental-82c64e1a4-20220520",
4142
"scroll-into-view-if-needed": "^2.2.25"
4243
},
4344
"devDependencies": {
44-
"@mdx-js/loader": "^1.6.16",
45+
"@babel/core": "^7.12.9",
46+
"@babel/plugin-transform-modules-commonjs": "^7.18.6",
47+
"@babel/preset-react": "^7.18.6",
48+
"@mdx-js/mdx": "^1.6.22",
4549
"@types/body-scroll-lock": "^2.6.1",
4650
"@types/classnames": "^2.2.10",
4751
"@types/debounce": "^1.2.1",
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
diff --git a/node_modules/next-remote-watch/bin/next-remote-watch b/node_modules/next-remote-watch/bin/next-remote-watch
2+
index c055b66..a2f749c 100755
3+
--- a/node_modules/next-remote-watch/bin/next-remote-watch
4+
+++ b/node_modules/next-remote-watch/bin/next-remote-watch
5+
@@ -66,7 +66,10 @@ app.prepare().then(() => {
6+
}
7+
}
8+
9+
- app.server.hotReloader.send('reloadPage')
10+
+ app.server.hotReloader.send({
11+
+ event: 'serverOnlyChanges',
12+
+ pages: ['/[[...markdownPath]]']
13+
+ });
14+
}
15+
)
16+
}

beta/plugins/md-layout-loader.js

-30
This file was deleted.

beta/src/components/Layout/Feedback.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import {useRouter} from 'next/router';
77
import {ga} from '../../utils/analytics';
88

99
export function Feedback({onSubmit = () => {}}: {onSubmit?: () => void}) {
10-
const {pathname} = useRouter();
10+
const {asPath} = useRouter();
1111
// Reset on route changes.
12-
return <SendFeedback key={pathname} onSubmit={onSubmit} />;
12+
return <SendFeedback key={asPath} onSubmit={onSubmit} />;
1313
}
1414

1515
const thumbsUpIcon = (

beta/src/components/Layout/Nav/MobileNav.tsx

+11-22
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,22 @@ import * as React from 'react';
66
import cn from 'classnames';
77
import {RouteItem} from 'components/Layout/useRouteMeta';
88
import {useRouter} from 'next/router';
9+
import {useActiveSection} from 'hooks/useActiveSection';
910
import {SidebarRouteTree} from '../Sidebar';
1011
import sidebarHome from '../../../sidebarHome.json';
1112
import sidebarLearn from '../../../sidebarLearn.json';
1213
import sidebarReference from '../../../sidebarReference.json';
1314

14-
function inferSection(pathname: string): 'learn' | 'apis' | 'home' {
15-
if (pathname.startsWith('/learn')) {
16-
return 'learn';
17-
} else if (pathname.startsWith('/apis')) {
18-
return 'apis';
19-
} else {
20-
return 'home';
21-
}
22-
}
23-
2415
export function MobileNav() {
25-
const {pathname} = useRouter();
26-
const [section, setSection] = React.useState(() => inferSection(pathname));
16+
// This is where we actually are according to the router.
17+
const section = useActiveSection();
18+
19+
// Let the user switch tabs there and back without navigating.
20+
// Seed the tab state from the router, but keep it independent.
21+
const [tab, setTab] = React.useState(section);
2722

2823
let tree = null;
29-
switch (section) {
24+
switch (tab) {
3025
case 'home':
3126
tree = sidebarHome.routes[0];
3227
break;
@@ -41,19 +36,13 @@ export function MobileNav() {
4136
return (
4237
<>
4338
<div className="sticky top-0 px-5 mb-2 bg-wash dark:bg-wash-dark flex justify-end border-b border-border dark:border-border-dark items-center self-center w-full z-10">
44-
<TabButton
45-
isActive={section === 'home'}
46-
onClick={() => setSection('home')}>
39+
<TabButton isActive={tab === 'home'} onClick={() => setTab('home')}>
4740
Home
4841
</TabButton>
49-
<TabButton
50-
isActive={section === 'learn'}
51-
onClick={() => setSection('learn')}>
42+
<TabButton isActive={tab === 'learn'} onClick={() => setTab('learn')}>
5243
Learn
5344
</TabButton>
54-
<TabButton
55-
isActive={section === 'apis'}
56-
onClick={() => setSection('apis')}>
45+
<TabButton isActive={tab === 'apis'} onClick={() => setTab('apis')}>
5746
API
5847
</TabButton>
5948
</div>

beta/src/components/Layout/Nav/Nav.tsx

+2-12
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {IconClose} from 'components/Icon/IconClose';
1111
import {IconHamburger} from 'components/Icon/IconHamburger';
1212
import {Search} from 'components/Search';
1313
import {MenuContext} from 'components/useMenu';
14+
import {useActiveSection} from 'hooks/useActiveSection';
1415

1516
import {Logo} from '../../Logo';
1617
import {Feedback} from '../Feedback';
@@ -86,22 +87,11 @@ const lightIcon = (
8687
</svg>
8788
);
8889

89-
function inferSection(pathname: string): 'learn' | 'apis' | 'home' {
90-
if (pathname.startsWith('/learn')) {
91-
return 'learn';
92-
} else if (pathname.startsWith('/apis')) {
93-
return 'apis';
94-
} else {
95-
return 'home';
96-
}
97-
}
98-
9990
export default function Nav() {
100-
const {pathname} = useRouter();
10191
const {isOpen, toggleOpen} = React.useContext(MenuContext);
10292
const [showFeedback, setShowFeedback] = React.useState(false);
10393
const feedbackAutohideRef = React.useRef<any>(null);
104-
const section = inferSection(pathname);
94+
const section = useActiveSection();
10595
const feedbackPopupRef = React.useRef<null | HTMLDivElement>(null);
10696

10797
React.useEffect(() => {

beta/src/components/Layout/Page.tsx

+21-3
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,33 @@
44

55
import {MenuProvider} from 'components/useMenu';
66
import * as React from 'react';
7+
import {useRouter} from 'next/router';
78
import {Nav} from './Nav';
89
import {RouteItem, SidebarContext} from './useRouteMeta';
10+
import {useActiveSection} from 'hooks/useActiveSection';
911
import {Sidebar} from './Sidebar';
1012
import {Footer} from './Footer';
1113
import SocialBanner from '../SocialBanner';
14+
import sidebarHome from '../../sidebarHome.json';
15+
import sidebarLearn from '../../sidebarLearn.json';
16+
import sidebarReference from '../../sidebarReference.json';
17+
1218
interface PageProps {
1319
children: React.ReactNode;
14-
routeTree: RouteItem;
1520
}
1621

17-
export function Page({routeTree, children}: PageProps) {
22+
export function Page({children}: PageProps) {
23+
const {query, asPath} = useRouter();
24+
const section = useActiveSection();
25+
let routeTree = sidebarHome as RouteItem;
26+
switch (section) {
27+
case 'apis':
28+
routeTree = sidebarReference as RouteItem;
29+
break;
30+
case 'learn':
31+
routeTree = sidebarLearn as RouteItem;
32+
break;
33+
}
1834
return (
1935
<>
2036
<SocialBanner />
@@ -31,7 +47,9 @@ export function Page({routeTree, children}: PageProps) {
3147
<div className="flex flex-1 w-full h-full self-stretch">
3248
<div className="w-full min-w-0">
3349
<main className="flex flex-1 self-stretch mt-16 sm:mt-10 flex-col items-end justify-around">
34-
<article className="h-full mx-auto relative w-full min-w-0">
50+
<article
51+
key={asPath}
52+
className="h-full mx-auto relative w-full min-w-0">
3553
{children}
3654
</article>
3755
<Footer />

beta/src/components/Layout/Sidebar/SidebarRouteTree.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ export function SidebarRouteTree({
7777
level = 0,
7878
}: SidebarRouteTreeProps) {
7979
const {breadcrumbs} = useRouteMeta(routeTree);
80-
const {pathname} = useRouter();
80+
const cleanedPath = useRouter().asPath.split('?')[0];
8181
const pendingRoute = usePendingRoute();
8282

83-
const slug = pathname;
83+
const slug = cleanedPath;
8484
const currentRoutes = routeTree.routes as RouteItem[];
8585
const expandedPath = currentRoutes.reduce(
8686
(acc: string | undefined, curr: RouteItem) => {
@@ -89,8 +89,8 @@ export function SidebarRouteTree({
8989
if (breadcrumb) {
9090
return curr.path;
9191
}
92-
if (curr.path === pathname) {
93-
return pathname;
92+
if (curr.path === cleanedPath) {
93+
return cleanedPath;
9494
}
9595
return undefined;
9696
},

beta/src/components/Layout/useRouteMeta.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ export function useRouteMeta(rootRoute?: RouteItem) {
5757
const sidebarContext = React.useContext(SidebarContext);
5858
const routeTree = rootRoute || sidebarContext;
5959
const router = useRouter();
60-
const cleanedPath = router.pathname;
61-
if (cleanedPath === '/404') {
60+
if (router.pathname === '/404') {
6261
return {
6362
breadcrumbs: [],
6463
};
6564
}
65+
const cleanedPath = router.asPath.split('?')[0];
6666
const breadcrumbs = getBreadcrumbs(cleanedPath, routeTree);
6767
return {
6868
...getRouteMeta(cleanedPath, routeTree),

beta/src/components/MDX/MDXComponents.tsx

+6
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,12 @@ export const MDXComponents = {
297297
}) => <ExpandableExample {...props} type="DeepDive" />,
298298
Diagram,
299299
DiagramGroup,
300+
FullWidth({children}: {children: any}) {
301+
return children;
302+
},
303+
MaxWidth({children}: {children: any}) {
304+
return <div className="max-w-4xl ml-0 2xl:mx-auto">{children}</div>;
305+
},
300306
Gotcha,
301307
Wip,
302308
HomepageHero,

beta/src/components/Seo.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export const Seo = withRouter(
3939
<meta
4040
property="og:url"
4141
key="og:url"
42-
content={`https://beta.reactjs.org${router.pathname}`}
42+
content={`https://beta.reactjs.org${router.asPath.split('?')[0]}`}
4343
/>
4444
{title != null && (
4545
<meta property="og:title" content={title} key="og:title" />

beta/src/components/useMenu.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export const useMenu = () => {
4848
return () => {
4949
clearAllBodyScrollLocks();
5050
};
51-
}, [router.pathname, hideSidebar]);
51+
}, [router.asPath, hideSidebar]);
5252

5353
// Avoid top-level context re-renders
5454
return React.useMemo(

beta/src/pages/learn/state-a-components-memory.md renamed to beta/src/content/learn/state-a-components-memory.md

+1-1

beta/src/hooks/useActiveSection.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*/
4+
5+
import {useRouter} from 'next/router';
6+
7+
export function useActiveSection(): 'learn' | 'apis' | 'home' {
8+
const {asPath} = useRouter();
9+
if (asPath.startsWith('/learn')) {
10+
return 'learn';
11+
} else if (asPath.startsWith('/apis')) {
12+
return 'apis';
13+
} else {
14+
return 'home';
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*/
4+
5+
import {useRouter} from 'next/router';
6+
7+
export function useActiveSection(): 'learn' | 'apis' | 'home' {
8+
const {asPath} = useRouter();
9+
if (asPath.startsWith('/learn')) {
10+
return 'learn';
11+
} else if (asPath.startsWith('/apis')) {
12+
return 'apis';
13+
} else {
14+
return 'home';
15+
}
16+
}

beta/src/pages/404.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*/
4+
5+
import {Page} from 'components/Layout/Page';
6+
import {MarkdownPage} from 'components/Layout/MarkdownPage';
7+
import {MDXComponents} from 'components/MDX/MDXComponents';
8+
9+
const {Intro, MaxWidth, p: P, a: A} = MDXComponents;
10+
11+
export default function NotFound() {
12+
return (
13+
<Page>
14+
<MarkdownPage meta={{title: 'Not Found'}} toc={[]}>
15+
<MaxWidth>
16+
<Intro>
17+
<P>This page doesn’t exist.</P>
18+
<P>
19+
Quite possibly, it hasn’t been written yet. This beta is a{' '}
20+
<A href="/#how-much-content-is-ready">work in progress!</A>
21+
</P>
22+
<P>Please check back later.</P>
23+
</Intro>
24+
</MaxWidth>
25+
</MarkdownPage>
26+
</Page>
27+
);
28+
}

0 commit comments

Comments
 (0)